source: trunk/src/org/expeditee/io/PDFWriter.java@ 655

Last change on this file since 655 was 655, checked in by jts21, 10 years ago

Switch to using specialised objects for settings so they make more a bit more sense (now each setting is a single object instead of multiple, and setter functions and default values are less hackish)
Also added tooltips (help strings) to settings, will need to add a way of displaying these (maybe add the idea of a tooltip which is a text item which only appears when hovering over another item?)
Converted all settings over to new format, everything seems to be working fine

File size: 2.9 KB
Line 
1package org.expeditee.io;
2
3import java.awt.Image;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.Writer;
7import java.util.List;
8
9import org.expeditee.gui.Frame;
10import org.expeditee.items.Item;
11import org.expeditee.items.Picture;
12import org.expeditee.items.Text;
13import org.expeditee.settings.UserSettings;
14
15import com.lowagie.text.Document;
16import com.lowagie.text.DocumentException;
17import com.lowagie.text.Font;
18import com.lowagie.text.FontFactory;
19import com.lowagie.text.Paragraph;
20import com.lowagie.text.pdf.PdfWriter;
21
22/**
23 * Writes out a tree in a linear format.
24 *
25 * @author root
26 *
27 */
28public class PDFWriter extends DefaultTreeWriter {
29
30 private Document _pdfDocument;
31
32 private Font _bodyFont;
33
34 public PDFWriter() {
35 assert(UserSettings.Style.get() != null);
36 if (UserSettings.Style.get().size() > 0) {
37 Text text = UserSettings.Style.get().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.get());
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.get().size()) {
71 String text = toWrite.getText();
72 toWrite = UserSettings.Style.get().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}
Note: See TracBrowser for help on using the repository browser.