1 | /**
|
---|
2 | * PdfFramesetWriter.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 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.awt.Dimension;
|
---|
22 | import java.awt.Graphics2D;
|
---|
23 | import java.awt.Image;
|
---|
24 | import java.io.FileOutputStream;
|
---|
25 | import java.io.IOException;
|
---|
26 | import java.io.Writer;
|
---|
27 | import java.util.Collection;
|
---|
28 | import java.util.List;
|
---|
29 |
|
---|
30 | import org.expeditee.gui.Frame;
|
---|
31 | import org.expeditee.gui.FrameGraphics;
|
---|
32 | import org.expeditee.items.Circle;
|
---|
33 | import org.expeditee.items.Item;
|
---|
34 | import org.expeditee.items.Line;
|
---|
35 | import org.expeditee.items.Picture;
|
---|
36 | import org.expeditee.items.Text;
|
---|
37 | import org.expeditee.items.widgets.InteractiveWidget;
|
---|
38 | import org.expeditee.settings.UserSettings;
|
---|
39 |
|
---|
40 | import com.lowagie.text.Document;
|
---|
41 | import com.lowagie.text.DocumentException;
|
---|
42 | import com.lowagie.text.Font;
|
---|
43 | import com.lowagie.text.FontFactory;
|
---|
44 | import com.lowagie.text.Rectangle;
|
---|
45 | import com.lowagie.text.pdf.PdfContentByte;
|
---|
46 | import com.lowagie.text.pdf.PdfWriter;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | *
|
---|
50 | * @author root
|
---|
51 | *
|
---|
52 | */
|
---|
53 | public class PdfFramesetWriter extends DefaultFramesetWriter {
|
---|
54 |
|
---|
55 | private Document _pdfDocument;
|
---|
56 |
|
---|
57 | private PdfWriter _pdfWriter;
|
---|
58 |
|
---|
59 | private boolean _showFrameNames;
|
---|
60 |
|
---|
61 | public PdfFramesetWriter(long firstFrame, long maxFrame,
|
---|
62 | boolean showFrameNames) {
|
---|
63 | super(firstFrame, maxFrame);
|
---|
64 | Dimension d = FrameGraphics.getMaxSize();
|
---|
65 | _pdfDocument = new Document(new Rectangle(d.width, d.height));
|
---|
66 | _showFrameNames = showFrameNames;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public PdfFramesetWriter() {
|
---|
70 | this(1, Long.MAX_VALUE, false);
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | protected String getFileName(Frame start) {
|
---|
75 | String fileName = start.getFramesetName();
|
---|
76 | if (_maxFrame < Long.MAX_VALUE) {
|
---|
77 | fileName += "_" + _firstFrame + "-" + _maxFrame;
|
---|
78 | }
|
---|
79 | return fileName;
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
84 | _format = ".pdf";
|
---|
85 | super.initialise(start, writer);
|
---|
86 |
|
---|
87 | try {
|
---|
88 | _pdfWriter = PdfWriter.getInstance(_pdfDocument,
|
---|
89 | new FileOutputStream(_filename));
|
---|
90 | _pdfDocument.open();
|
---|
91 | _pdfDocument.addCreationDate();
|
---|
92 | _pdfDocument.addAuthor(UserSettings.UserName.get());
|
---|
93 | _pdfDocument.addCreator("Expeditee");
|
---|
94 | } catch (DocumentException e) {
|
---|
95 | e.printStackTrace();
|
---|
96 | throw new IOException(e.getMessage());
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | protected void writeTitle(Text toWrite, List<Item> items)
|
---|
102 | throws IOException {
|
---|
103 | _pdfDocument.addTitle(toWrite.getText());
|
---|
104 | writeText(toWrite);
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected void writeStartFrame(Frame starting) throws IOException {
|
---|
108 | // TODO color in the frame background color
|
---|
109 |
|
---|
110 | super.writeStartFrame(starting);
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected void writeEndFrame(Frame ending) throws IOException {
|
---|
114 | if (_showFrameNames)
|
---|
115 | writeText((Text) ending.getNameItem());
|
---|
116 | super.writeEndFrame(ending);
|
---|
117 | // Move to the next page
|
---|
118 | _pdfDocument.newPage();
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | protected String finalise() throws IOException {
|
---|
123 | _pdfDocument.close();
|
---|
124 | return super.finalise();
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | protected void writeText(Text text) throws IOException {
|
---|
129 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
130 | // we tell the ContentByte we're ready to draw text
|
---|
131 | cb.beginText();
|
---|
132 | Font font = FontFactory.getFont(
|
---|
133 | Conversion.getPdfFont(text.getFamily()), text.getSize(), text
|
---|
134 | .getPaintFont().getStyle(), text.getPaintColor());
|
---|
135 | cb.setFontAndSize(font.getBaseFont(), text.getSize());
|
---|
136 | // cb.setColorStroke(text.getPaintColor());
|
---|
137 | cb.setColorFill(text.getPaintColor());
|
---|
138 |
|
---|
139 | // we draw some text on a certain position
|
---|
140 | cb.setTextMatrix(text.getX(), _pdfWriter.getPageSize().getHeight()
|
---|
141 | - text.getY());
|
---|
142 | List<String> textLines = text.getTextList();
|
---|
143 | // cb.showText(textLines.get(0));
|
---|
144 | for (int i = 0; i < textLines.size(); i++) {
|
---|
145 | cb.showText(textLines.get(i));
|
---|
146 | cb.moveText(0, -text.getLineHeight());
|
---|
147 | }
|
---|
148 | // we tell the contentByte, we've finished drawing text
|
---|
149 | cb.endText();
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | protected void writePicture(Picture pic) throws IOException {
|
---|
154 | Image image = pic.getCroppedImage();
|
---|
155 | if (image == null)
|
---|
156 | return;
|
---|
157 | try {
|
---|
158 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
159 | com.lowagie.text.Image iTextImage = com.lowagie.text.Image
|
---|
160 | .getInstance(image, null);
|
---|
161 | iTextImage.setAbsolutePosition(pic.getX(), _pdfWriter.getPageSize()
|
---|
162 | .getHeight()
|
---|
163 | - pic.getY() - pic.getHeight());
|
---|
164 | cb.addImage(iTextImage);
|
---|
165 | } catch (DocumentException e) {
|
---|
166 | e.printStackTrace();
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Override
|
---|
171 | protected void writeLine(Line lineEnd) throws IOException {
|
---|
172 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
173 | // Color fill = lineEnd.getFillColor();
|
---|
174 | // if (fill != null) {
|
---|
175 | // cb.setColorFill(fill);
|
---|
176 | // }
|
---|
177 | Graphics2D g = cb.createGraphicsShapes(
|
---|
178 | FrameGraphics.getMaxSize().width,
|
---|
179 | FrameGraphics.getMaxSize().height);
|
---|
180 | lineEnd.paint(g);
|
---|
181 | // if (fill != null) {
|
---|
182 | // g.setPaint(fill);
|
---|
183 | // }
|
---|
184 | // lineEnd.paintFill(g);
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | protected Collection<Item> getItemsToWrite(Frame toWrite) {
|
---|
189 | return toWrite.getVisibleItems();
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | protected void writeCircle(Circle toWrite) throws IOException {
|
---|
194 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
195 | Graphics2D g = cb.createGraphicsShapes(
|
---|
196 | FrameGraphics.getMaxSize().width,
|
---|
197 | FrameGraphics.getMaxSize().height);
|
---|
198 | toWrite.paint(g);
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | @Override
|
---|
203 | protected void writeWidget(InteractiveWidget toWrite) throws IOException {
|
---|
204 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
205 | Graphics2D g = cb.createGraphicsShapes(
|
---|
206 | FrameGraphics.getMaxSize().width,
|
---|
207 | FrameGraphics.getMaxSize().height);
|
---|
208 | toWrite.paint(g);
|
---|
209 | }
|
---|
210 | }
|
---|