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

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

Switch to using specialised objects for settings so they make more a bit more sense (now each setting is a single object instead of multiple, and setter functions and default values are less hackish)
Also added tooltips (help strings) to settings, will need to add a way of displaying these (maybe add the idea of a tooltip which is a text item which only appears when hovering over another item?)
Converted all settings over to new format, everything seems to be working fine

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