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

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

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

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