1 package org.simantics.document.linking.report.pdf;
4 import java.util.HashMap;
7 import org.simantics.document.linking.report.Document;
8 import org.simantics.document.linking.report.DocumentElement;
9 import org.simantics.document.linking.report.DocumentItem;
10 import org.simantics.document.linking.report.DocumentTitlePage;
11 import org.simantics.document.linking.report.Table;
12 import org.simantics.document.linking.report.TableOfContents;
13 import org.simantics.document.linking.report.TextItem;
14 import org.simantics.document.linking.report.URLItem;
15 import org.simantics.document.linking.report.base.TextItemImpl;
16 import org.simantics.document.linking.report.base.URLItemImpl;
17 import org.simantics.utils.datastructures.Arrays;
21 * PDF format reporter.
23 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
26 public class PDFDocument implements Document {
27 private String filename;
29 Map<TextSize, Font> fonts = new HashMap<TextSize, Font>();
31 PDFPageStream contentStream;
32 PDFTable currentTable;
33 PDFTitlePage titlePage;
36 public PDFDocument(String filename) throws Exception {
37 this.filename = filename;
38 contentStream = new PDFPageStream();
40 contentStream.setDefaultFont(fonts.get(TextSize.SMALL));
43 public PDFDocument(String filename, Font font) throws Exception {
44 this.filename = filename;
45 contentStream = new PDFPageStream();
47 fonts.put(TextSize.SMALL, font);
50 public PDFDocument(String filename, Font font, Font headerFont) throws Exception {
51 this.filename = filename;
52 contentStream = new PDFPageStream();
54 fonts.put(TextSize.SMALL, font);
55 fonts.put(TextSize.MEDIUM, headerFont);
58 private void defaultFonts() {
59 fonts.put(TextSize.TINY, new Font("Arial", Font.PLAIN, 6));
60 fonts.put(TextSize.SMALL, new Font("Arial", Font.PLAIN, 8));
61 fonts.put(TextSize.MEDIUM, new Font("Arial", Font.PLAIN, 10));
62 fonts.put(TextSize.LARGE, new Font("Arial", Font.PLAIN, 16));
63 fonts.put(TextSize.HUGE, new Font("Arial", Font.BOLD, 24));
64 contentStream.setDefaultFont(fonts.get(TextSize.SMALL));
67 Font getFont(TextSize size) {
68 return fonts.get(size);
72 public int getAvailableLines() {
73 return contentStream.getAvailableLines();
77 public int getCurrentLine() {
78 return contentStream.getCurrentLine();
81 public void close() throws Exception{
84 toc.create(contentStream, contentStream.getPages().get(0));
86 contentStream.close(filename);
90 * @see org.simantics.document.linking.report.Document#nextPage()
93 public void nextPage() throws Exception{
94 contentStream.nextPage();
97 @SuppressWarnings("unchecked")
99 public <T extends DocumentElement> T newElement(Class<T> cls, String...options) throws Exception {
100 if (cls == Table.class) {
101 return (T)newTable();
102 } else if (cls == TableOfContents.class) {
103 return (T)getOrCreateToc();
104 } else if (cls == DocumentTitlePage.class) {
105 if (titlePage != null)
106 throw new Exception("Report may contain only one title page");
107 titlePage = new PDFTitlePage(this);
113 @SuppressWarnings("unchecked")
115 public <T extends DocumentElement> T nextElement(Class<T> cls, String...options) throws Exception {
116 if (cls == Table.class) {
117 return (T)nextTable(Arrays.contains(options, Document.TOC));
118 } else if (cls == TableOfContents.class) {
119 return (T)getOrCreateToc();
124 @SuppressWarnings("unchecked")
126 public <T extends DocumentElement> T getCurrentElement(Class<T> cls) {
127 if (cls == Table.class) {
128 return (T)getCurrentTable();
129 } else if (cls == TableOfContents.class) {
131 } else if (cls == DocumentTitlePage.class) {
137 @SuppressWarnings("unchecked")
139 public <T extends DocumentItem> T newItem(Class<T> cls, String... options)
141 if (cls == TextItem.class)
142 return (T)new TextItemImpl();
143 if (cls == URLItem.class)
144 return (T)new URLItemImpl();
148 public Table nextTable(boolean id) throws Exception {
149 if (currentTable!= null) {
151 currentTable = new PDFTable(currentTable);
159 private void closeTable() throws Exception {
160 if (currentTable != null && currentTable.currentLine > 0) {
161 currentTable.setLinesVisible(false);
162 currentTable.setHeaderVisible(false);
163 currentTable.writeLine("");
168 public Table newTable() throws Exception {
170 currentTable = new PDFTable(this,contentStream);
175 Table getCurrentTable() {
179 TableOfContents getOrCreateToc() throws Exception{
181 toc = new PDFTocElement(this);