]> gerrit.simantics Code Review - simantics/platform.git/blob
f4f376a415da820abe9121256d7713b2e83f0216
[simantics/platform.git] /
1 package org.simantics.document.linking.report.pdf;
2
3 import java.awt.Font;
4 import java.awt.FontMetrics;
5 import java.awt.Graphics2D;
6 import java.awt.font.FontRenderContext;
7 import java.io.File;
8 import java.io.FileOutputStream;
9
10 import com.lowagie.text.Document;
11 import com.lowagie.text.Rectangle;
12 import com.lowagie.text.pdf.PdfContentByte;
13 import com.lowagie.text.pdf.PdfTemplate;
14 import com.lowagie.text.pdf.PdfWriter;
15
16 public class PDFPage {
17         public PDFPageStream stream;
18         public Graphics2D g2d;
19         public Document document;
20         public PdfWriter writer;
21         public File tempFile;
22         public PdfContentByte cb;
23         public PdfTemplate template = null;
24         public Font font;
25         
26         FontRenderContext frc;
27         FontMetrics fm;
28         
29         int currentPixel = 0;
30         int currentLine = 0;
31         int availableLines = 0;
32         
33         
34         public PDFPage(PDFPageStream stream) throws Exception{
35                 this.stream = stream;
36                 Rectangle pageSize = stream.getPageSize();
37                 document = new Document(pageSize);
38                 tempFile = File.createTempFile("ReportGenerator", ".pdf");
39                 writer = PdfWriter.getInstance(document, new FileOutputStream(tempFile));
40                 document.open();
41                 this.cb = writer.getDirectContent();
42
43         
44         writer.flush();
45                 template = cb.createTemplate(pageSize.getWidth(), pageSize.getHeight());
46                 g2d = template.createGraphics(pageSize.getWidth(), pageSize.getHeight());
47                 g2d.translate(stream.marginLeft, stream.marginTop);
48         g2d.setClip(0, 0, stream.contentWidth, stream.contentHeight);
49         currentPixel = 0;
50         currentLine = 1;
51        
52         setFont(stream.getDefaultFont());
53         }
54         
55         protected int getLineHeight() {
56                 return fm.getHeight();
57         }
58         
59         public boolean isOpen() {
60                 return template != null;
61         }
62         
63         public void setFont(Font font) {
64                 this.font = font;
65                 g2d.setFont(font);
66         fm = g2d.getFontMetrics();
67         frc = new FontRenderContext(g2d.getTransform(), true, true);
68         estimateAvailableLines();
69         }
70         
71         protected void estimateAvailableLines() {
72                  availableLines = (int)Math.floor((stream.contentHeight-currentPixel)/getLineHeight());
73         }
74         
75         public Font getFont() {
76                 return font;
77         }
78         
79         public void writeLine(String line) throws Exception{
80                 writeLine(line, 0);
81         }
82         
83         public void writeLine(String line, int x) throws Exception{
84                 g2d.drawString(line, x, currentPixel+getLineHeight());
85                 currentLine++;
86                 availableLines--;
87                 currentPixel += getLineHeight();
88                 stream.checkNextPage();
89         }
90         
91         public void close() {
92                 g2d.dispose();
93                 cb.addTemplate(template, 0, 0);
94         template = null;
95         
96                 document.close();
97                 if (writer != null)
98                         writer.close();
99                 document = null;
100                 writer = null;
101                 cb = null;
102                 g2d = null;
103         }
104 }