1 package org.simantics.document.linking.report.pdf;
4 import java.awt.FontMetrics;
5 import java.awt.Graphics2D;
6 import java.awt.font.FontRenderContext;
8 import java.io.FileOutputStream;
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;
16 public class PDFPage {
17 public PDFPageStream stream;
18 public Graphics2D g2d;
19 public Document document;
20 public PdfWriter writer;
22 public PdfContentByte cb;
23 public PdfTemplate template = null;
26 FontRenderContext frc;
31 int availableLines = 0;
34 public PDFPage(PDFPageStream stream) throws Exception{
36 Rectangle pageSize = stream.getPageSize();
37 document = new Document(pageSize);
38 tempFile = File.createTempFile("ReportGenerator", ".pdf");
39 writer = PdfWriter.getInstance(document, new FileOutputStream(tempFile));
41 this.cb = writer.getDirectContent();
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);
52 setFont(stream.getDefaultFont());
55 protected int getLineHeight() {
56 return fm.getHeight();
59 public boolean isOpen() {
60 return template != null;
63 public void setFont(Font font) {
66 fm = g2d.getFontMetrics();
67 frc = new FontRenderContext(g2d.getTransform(), true, true);
68 estimateAvailableLines();
71 protected void estimateAvailableLines() {
72 availableLines = (int)Math.floor((stream.contentHeight-currentPixel)/getLineHeight());
75 public Font getFont() {
79 public void writeLine(String line) throws Exception{
83 public void writeLine(String line, int x) throws Exception{
84 g2d.drawString(line, x, currentPixel+getLineHeight());
87 currentPixel += getLineHeight();
88 stream.checkNextPage();
93 cb.addTemplate(template, 0, 0);