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

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 4.1 KB
Line 
1/**
2 * PDFWriter.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.io;
20
21import java.io.FileOutputStream;
22import java.io.IOException;
23import java.io.Writer;
24import java.util.List;
25
26import org.expeditee.core.Image;
27import org.expeditee.gio.swing.SwingConversions;
28import org.expeditee.gio.swing.SwingMiscManager;
29import org.expeditee.gui.Frame;
30import org.expeditee.items.Item;
31import org.expeditee.items.Picture;
32import org.expeditee.items.Text;
33import org.expeditee.settings.UserSettings;
34
35import com.lowagie.text.Document;
36import com.lowagie.text.DocumentException;
37import com.lowagie.text.Font;
38import com.lowagie.text.FontFactory;
39import com.lowagie.text.Paragraph;
40import com.lowagie.text.pdf.PdfWriter;
41
42/**
43 * Writes out a tree in a linear format.
44 *
45 * @author root
46 *
47 */
48public class PDFWriter extends DefaultTreeWriter {
49
50 private Document _pdfDocument;
51
52 private Font _bodyFont;
53
54 public PDFWriter() {
55 assert(UserSettings.Style.get() != null);
56 if (UserSettings.Style.get().size() > 0) {
57 Text text = UserSettings.Style.get().get(0);
58 _bodyFont = FontFactory.getFont(Conversion.getPdfFont(text.getFamily()),
59 text.getSize(),
60 SwingConversions.toSwingFontStyle(text.getPaintFont().getStyle()),
61 SwingConversions.toSwingColor(text.getColor()));
62 }
63 _pdfDocument = new Document();
64 }
65
66 @Override
67 protected void initialise(Frame start, Writer writer) throws IOException {
68 _format = ".pdf";
69 super.initialise(start, writer);
70 try {
71 PdfWriter
72 .getInstance(_pdfDocument, new FileOutputStream(_filename));
73 _pdfDocument.open();
74 _pdfDocument.addCreationDate();
75 _pdfDocument.addAuthor(UserSettings.UserName.get());
76 _pdfDocument.addCreator("Expeditee");
77 _pdfDocument.addTitle(start.getTitle());
78 } catch (DocumentException e) {
79 e.printStackTrace();
80 throw new IOException(e.getMessage());
81 }
82 }
83
84 @Override
85 protected void writeTitle(Text toWrite, List<Item> items)
86 throws IOException {
87 int indent = getIndent();
88 if (indent == 0)
89 return;
90
91 if (indent < UserSettings.Style.get().size()) {
92 String text = toWrite.getText();
93 toWrite = UserSettings.Style.get().get(indent).getTemplateForm();
94 toWrite.setText(text);
95 }
96
97 writeText(toWrite, false);
98 }
99
100 @Override
101 protected String finaliseTree() throws IOException {
102 _pdfDocument.close();
103 return super.finaliseTree();
104 }
105
106 @Override
107 protected void writeText(Text text) throws IOException {
108 writeText(text, true);
109 }
110
111 protected void writeText(Text text, boolean bodyText) throws IOException {
112 try {
113 Font font = null;
114 if (bodyText) {
115 font = _bodyFont;
116 }
117
118 if (font == null) {
119 font = FontFactory.getFont(Conversion.getPdfFont(text.getFamily()),
120 text.getSize(),
121 SwingConversions.toSwingFontStyle(text.getPaintFont().getStyle()),
122 SwingConversions.toSwingColor(text.getColor()));
123 }
124
125 _pdfDocument.add(new Paragraph(text.getText(), font));
126 } catch (DocumentException e) {
127 // TODO Auto-generated catch block
128 e.printStackTrace();
129 }
130 }
131
132 @Override
133 protected void writePicture(Picture pic) throws IOException {
134 Image image = pic.getCroppedImage();
135 try {
136 _pdfDocument.add(com.lowagie.text.Image.getInstance(SwingMiscManager.getIfUsingSwingImageManager().getInternalImage(image), null));
137 } catch (DocumentException e) {
138 // TODO Auto-generated catch block
139 e.printStackTrace();
140 }
141 }
142}
Note: See TracBrowser for help on using the repository browser.