1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.awt.Dimension;
|
---|
4 | import java.awt.Image;
|
---|
5 | import java.io.FileOutputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.Writer;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.expeditee.gui.Frame;
|
---|
12 | import org.expeditee.gui.FrameGraphics;
|
---|
13 | import org.expeditee.items.Item;
|
---|
14 | import org.expeditee.items.Picture;
|
---|
15 | import org.expeditee.items.Text;
|
---|
16 | import org.expeditee.settings.UserSettings;
|
---|
17 |
|
---|
18 | import com.lowagie.text.Document;
|
---|
19 | import com.lowagie.text.DocumentException;
|
---|
20 | import com.lowagie.text.Font;
|
---|
21 | import com.lowagie.text.FontFactory;
|
---|
22 | import com.lowagie.text.Rectangle;
|
---|
23 | import com.lowagie.text.pdf.PdfContentByte;
|
---|
24 | import com.lowagie.text.pdf.PdfWriter;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | *
|
---|
28 | * @author root
|
---|
29 | *
|
---|
30 | */
|
---|
31 | public class PDF2Writer extends DefaultFrameWriter {
|
---|
32 |
|
---|
33 | private Document _pdfDocument;
|
---|
34 |
|
---|
35 | private PdfWriter _pdfWriter;
|
---|
36 |
|
---|
37 | public PDF2Writer() {
|
---|
38 | Dimension d = FrameGraphics.getMaxSize();
|
---|
39 | _pdfDocument = new Document(new Rectangle(d.width, d.height));
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
44 | _format = ".pdf";
|
---|
45 | super.initialise(start, writer);
|
---|
46 | try {
|
---|
47 | _pdfWriter = PdfWriter.getInstance(_pdfDocument,
|
---|
48 | new FileOutputStream(_filename));
|
---|
49 | _pdfDocument.open();
|
---|
50 | _pdfDocument.addCreationDate();
|
---|
51 | _pdfDocument.addAuthor(UserSettings.UserName.get());
|
---|
52 | _pdfDocument.addCreator("Expeditee");
|
---|
53 | } catch (DocumentException e) {
|
---|
54 | e.printStackTrace();
|
---|
55 | throw new IOException(e.getMessage());
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | protected void writeTitle(Text toWrite, List<Item> items)
|
---|
61 | throws IOException {
|
---|
62 | _pdfDocument.addTitle(toWrite.getText());
|
---|
63 | writeText(toWrite);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | protected String finalise() throws IOException {
|
---|
68 | _pdfDocument.close();
|
---|
69 | return super.finalise();
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | protected void writeText(Text text) throws IOException {
|
---|
74 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
75 | // we tell the ContentByte we're ready to draw text
|
---|
76 | cb.beginText();
|
---|
77 | Font font = FontFactory.getFont(
|
---|
78 | Conversion.getPdfFont(text.getFamily()), text.getSize(), text
|
---|
79 | .getPaintFont().getStyle(), text.getPaintColor());
|
---|
80 | cb.setFontAndSize(font.getBaseFont(), text.getSize());
|
---|
81 | // cb.setColorStroke(text.getPaintColor());
|
---|
82 | cb.setColorFill(text.getPaintColor());
|
---|
83 |
|
---|
84 | // we draw some text on a certain position
|
---|
85 | cb.setTextMatrix(text.getX(), _pdfWriter.getPageSize().getHeight() - text.getY());
|
---|
86 | List<String> textLines = text.getTextList();
|
---|
87 | //cb.showText(textLines.get(0));
|
---|
88 | for(int i = 0; i < textLines.size(); i++){
|
---|
89 | cb.showText(textLines.get(i));
|
---|
90 | cb.moveText(0, -text.getLineHeight());
|
---|
91 | }
|
---|
92 |
|
---|
93 | // we tell the contentByte, we've finished drawing text
|
---|
94 | cb.endText();
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | protected void writePicture(Picture pic) throws IOException {
|
---|
99 | Image image = pic.getCroppedImage();
|
---|
100 | try {
|
---|
101 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
102 | com.lowagie.text.Image iTextImage = com.lowagie.text.Image
|
---|
103 | .getInstance(image, null);
|
---|
104 | iTextImage.setAbsolutePosition(pic.getX(), _pdfWriter.getPageSize().getHeight() - pic.getY() - pic.getHeight());
|
---|
105 | cb.addImage(iTextImage);
|
---|
106 | } catch (DocumentException e) {
|
---|
107 | e.printStackTrace();
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | protected Collection<Item> getItemsToWrite(Frame toWrite) {
|
---|
113 | return toWrite.getVisibleItems();
|
---|
114 | }
|
---|
115 | }
|
---|