1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.awt.Image;
|
---|
4 | import java.io.FileOutputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.Writer;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.expeditee.gui.Frame;
|
---|
10 | import org.expeditee.gui.UserSettings;
|
---|
11 | import org.expeditee.items.Item;
|
---|
12 | import org.expeditee.items.Picture;
|
---|
13 | import org.expeditee.items.Text;
|
---|
14 |
|
---|
15 | import com.lowagie.text.Document;
|
---|
16 | import com.lowagie.text.DocumentException;
|
---|
17 | import com.lowagie.text.Font;
|
---|
18 | import com.lowagie.text.FontFactory;
|
---|
19 | import com.lowagie.text.Paragraph;
|
---|
20 | import com.lowagie.text.pdf.PdfWriter;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Writes out a tree in a linear format.
|
---|
24 | *
|
---|
25 | * @author root
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class PDFWriter extends DefaultTreeWriter {
|
---|
29 |
|
---|
30 | private Document _pdfDocument;
|
---|
31 |
|
---|
32 | private Font _bodyFont;
|
---|
33 |
|
---|
34 | public PDFWriter() {
|
---|
35 | assert(UserSettings.Style != null);
|
---|
36 | if (UserSettings.Style.size() > 0) {
|
---|
37 | Text text = UserSettings.Style.get(0);
|
---|
38 | _bodyFont = FontFactory.getFont(Conversion.getPdfFont(text
|
---|
39 | .getFamily()), text.getSize(), text.getPaintFont()
|
---|
40 | .getStyle(), text.getColor());
|
---|
41 | }
|
---|
42 | _pdfDocument = new Document();
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
47 | _format = ".pdf";
|
---|
48 | super.initialise(start, writer);
|
---|
49 | try {
|
---|
50 | PdfWriter
|
---|
51 | .getInstance(_pdfDocument, new FileOutputStream(_filename));
|
---|
52 | _pdfDocument.open();
|
---|
53 | _pdfDocument.addCreationDate();
|
---|
54 | _pdfDocument.addAuthor(UserSettings.UserName);
|
---|
55 | _pdfDocument.addCreator("Expeditee");
|
---|
56 | _pdfDocument.addTitle(start.getTitle());
|
---|
57 | } catch (DocumentException e) {
|
---|
58 | e.printStackTrace();
|
---|
59 | throw new IOException(e.getMessage());
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | protected void writeTitle(Text toWrite, List<Item> items)
|
---|
65 | throws IOException {
|
---|
66 | int indent = getIndent();
|
---|
67 | if (indent == 0)
|
---|
68 | return;
|
---|
69 |
|
---|
70 | if (indent < UserSettings.Style.size()) {
|
---|
71 | String text = toWrite.getText();
|
---|
72 | toWrite = UserSettings.Style.get(indent).getTemplateForm();
|
---|
73 | toWrite.setText(text);
|
---|
74 | }
|
---|
75 |
|
---|
76 | writeText(toWrite, false);
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | protected String finaliseTree() throws IOException {
|
---|
81 | _pdfDocument.close();
|
---|
82 | return super.finaliseTree();
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | protected void writeText(Text text) throws IOException {
|
---|
87 | writeText(text, true);
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected void writeText(Text text, boolean bodyText) throws IOException {
|
---|
91 | try {
|
---|
92 | Font font = null;
|
---|
93 | if (bodyText) {
|
---|
94 | font = _bodyFont;
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (font == null) {
|
---|
98 | font = FontFactory.getFont(Conversion.getPdfFont(text
|
---|
99 | .getFamily()), text.getSize(), text.getPaintFont()
|
---|
100 | .getStyle(), text.getColor());
|
---|
101 | }
|
---|
102 |
|
---|
103 | _pdfDocument.add(new Paragraph(text.getText(), font));
|
---|
104 | } catch (DocumentException e) {
|
---|
105 | // TODO Auto-generated catch block
|
---|
106 | e.printStackTrace();
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | protected void writePicture(Picture pic) throws IOException {
|
---|
112 | Image image = pic.getCroppedImage();
|
---|
113 | try {
|
---|
114 | _pdfDocument.add(com.lowagie.text.Image.getInstance(image, null));
|
---|
115 | } catch (DocumentException e) {
|
---|
116 | // TODO Auto-generated catch block
|
---|
117 | e.printStackTrace();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|