source: trunk/src/org/expeditee/io/PDF2Writer.java@ 289

Last change on this file since 289 was 282, checked in by ra33, 16 years ago
File size: 3.1 KB
Line 
1package org.expeditee.io;
2
3import java.awt.Dimension;
4import java.awt.Image;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.util.Collection;
8import java.util.List;
9
10import org.expeditee.gui.Frame;
11import org.expeditee.gui.FrameGraphics;
12import org.expeditee.gui.UserSettings;
13import org.expeditee.items.Item;
14import org.expeditee.items.Picture;
15import org.expeditee.items.Text;
16
17import com.lowagie.text.Document;
18import com.lowagie.text.DocumentException;
19import com.lowagie.text.Font;
20import com.lowagie.text.FontFactory;
21import com.lowagie.text.Rectangle;
22import com.lowagie.text.pdf.PdfContentByte;
23import com.lowagie.text.pdf.PdfWriter;
24
25/**
26 *
27 * @author root
28 *
29 */
30public class PDF2Writer extends DefaultFrameWriter {
31
32 private Document _pdfDocument;
33
34 private PdfWriter _pdfWriter;
35
36 public PDF2Writer() {
37 Dimension d = FrameGraphics.getMaxSize();
38 _pdfDocument = new Document(new Rectangle(d.width, d.height));
39 }
40
41 @Override
42 protected void initialise(Frame start) throws IOException {
43 _format = ".pdf";
44 super.initialise(start);
45 try {
46 _pdfWriter = PdfWriter.getInstance(_pdfDocument,
47 new FileOutputStream(_filename));
48 _pdfDocument.open();
49 _pdfDocument.addCreationDate();
50 _pdfDocument.addAuthor(UserSettings.Username);
51 _pdfDocument.addCreator("Expeditee");
52 } catch (DocumentException e) {
53 e.printStackTrace();
54 throw new IOException(e.getMessage());
55 }
56 }
57
58 @Override
59 protected void writeTitle(Text toWrite, List<Item> items)
60 throws IOException {
61 _pdfDocument.addTitle(toWrite.getText());
62 writeText(toWrite);
63 }
64
65 @Override
66 protected String finalise() throws IOException {
67 _pdfDocument.close();
68 return super.finalise();
69 }
70
71 @Override
72 protected void writeText(Text text) throws IOException {
73 PdfContentByte cb = _pdfWriter.getDirectContent();
74 // we tell the ContentByte we're ready to draw text
75 cb.beginText();
76 Font font = FontFactory.getFont(
77 Conversion.getPdfFont(text.getFamily()), text.getSize(), text
78 .getPaintFont().getStyle(), text.getPaintColor());
79 cb.setFontAndSize(font.getBaseFont(), text.getSize());
80 // cb.setColorStroke(text.getPaintColor());
81 cb.setColorFill(text.getPaintColor());
82
83 // we draw some text on a certain position
84 cb.setTextMatrix(text.getX(), _pdfWriter.getPageSize().getHeight() - text.getY());
85 List<String> textLines = text.getTextList();
86 //cb.showText(textLines.get(0));
87 for(int i = 0; i < textLines.size(); i++){
88 cb.showText(textLines.get(i));
89 cb.moveText(0, -text.getLineHeight());
90 }
91
92 // we tell the contentByte, we've finished drawing text
93 cb.endText();
94 }
95
96 @Override
97 protected void writePicture(Picture pic) throws IOException {
98 Image image = pic.getCroppedImage();
99 try {
100 PdfContentByte cb = _pdfWriter.getDirectContent();
101 com.lowagie.text.Image iTextImage = com.lowagie.text.Image
102 .getInstance(image, null);
103 iTextImage.setAbsolutePosition(pic.getX(), _pdfWriter.getPageSize().getHeight() - pic.getY() - pic.getHeight());
104 cb.addImage(iTextImage);
105 } catch (DocumentException e) {
106 e.printStackTrace();
107 }
108 }
109
110 @Override
111 protected Collection<Item> getItemsToWrite(Frame toWrite) {
112 return toWrite.getVisibleItems();
113 }
114}
Note: See TracBrowser for help on using the repository browser.