1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.awt.Image;
|
---|
4 | import java.io.FileOutputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import org.expeditee.gui.Frame;
|
---|
9 | import org.expeditee.gui.UserSettings;
|
---|
10 | import org.expeditee.items.Item;
|
---|
11 | import org.expeditee.items.Picture;
|
---|
12 | import org.expeditee.items.Text;
|
---|
13 |
|
---|
14 | import com.lowagie.text.Document;
|
---|
15 | import com.lowagie.text.DocumentException;
|
---|
16 | import com.lowagie.text.FontFactory;
|
---|
17 | import com.lowagie.text.Paragraph;
|
---|
18 | import com.lowagie.text.pdf.PdfWriter;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Writes out a tree in a linear format.
|
---|
22 | * @author root
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class PDFWriter extends DefaultTreeWriter {
|
---|
26 |
|
---|
27 | private Document _pdfDocument;
|
---|
28 |
|
---|
29 | public PDFWriter() {
|
---|
30 | _pdfDocument = new Document();
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | protected void initialise(Frame start) throws IOException {
|
---|
35 | _format = ".pdf";
|
---|
36 | super.initialise(start);
|
---|
37 | try {
|
---|
38 | PdfWriter
|
---|
39 | .getInstance(_pdfDocument, new FileOutputStream(_filename));
|
---|
40 | _pdfDocument.open();
|
---|
41 | _pdfDocument.addCreationDate();
|
---|
42 | _pdfDocument.addAuthor(UserSettings.Username);
|
---|
43 | _pdfDocument.addCreator("Expeditee");
|
---|
44 | } catch (DocumentException e) {
|
---|
45 | e.printStackTrace();
|
---|
46 | throw new IOException(e.getMessage());
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected void writeTitle(Text toWrite, List<Item> items)
|
---|
52 | throws IOException {
|
---|
53 | _pdfDocument.addTitle(toWrite.getText());
|
---|
54 | writeText(toWrite);
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | protected String finaliseTree() throws IOException {
|
---|
59 | _pdfDocument.close();
|
---|
60 | return super.finaliseTree();
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | protected void writeStartLink(Item linker) throws IOException {
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | protected void writeEndLink(Item linker) throws IOException {
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | protected void writeText(Text text) throws IOException {
|
---|
73 | try {
|
---|
74 |
|
---|
75 | _pdfDocument.add(new Paragraph(text.getText(), FontFactory.getFont(
|
---|
76 | Conversion.getPdfFont(text.getFamily()), text.getSize(),
|
---|
77 | text.getPaintFont().getStyle(), text.getColor())));
|
---|
78 | } catch (DocumentException e) {
|
---|
79 | // TODO Auto-generated catch block
|
---|
80 | e.printStackTrace();
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | protected void writePicture(Picture pic) throws IOException {
|
---|
86 | Image image = pic.getCroppedImage();
|
---|
87 | try {
|
---|
88 | _pdfDocument.add(com.lowagie.text.Image.getInstance(image, null));
|
---|
89 | } catch (DocumentException e) {
|
---|
90 | // TODO Auto-generated catch block
|
---|
91 | e.printStackTrace();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|