source: trunk/src/org/expeditee/io/PdfFramesetWriter.java@ 457

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