]> gerrit.simantics Code Review - simantics/platform.git/blob - pdf/PDFPageStream.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / pdf / PDFPageStream.java
1 package org.simantics.document.linking.report.pdf;
2
3 import java.awt.Font;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import com.lowagie.text.Document;
10 import com.lowagie.text.PageSize;
11 import com.lowagie.text.Rectangle;
12 import com.lowagie.text.pdf.PdfCopy;
13 import com.lowagie.text.pdf.PdfImportedPage;
14 import com.lowagie.text.pdf.PdfReader;
15
16 public class PDFPageStream {
17         boolean modifiable = false;
18         Rectangle pageSize = PageSize.A4;
19         Font defaultFont;
20         List<PDFPage> pages = new ArrayList<PDFPage>();
21         
22         int contentWidth;
23         int contentHeight;
24         
25         int marginLeft = 20;
26     int marginRight = 20;
27     int marginTop = 10;
28     int marginBottom = 45;
29
30         int currentPage = 0;
31         
32         public PDFPageStream() {
33                 calculateContent();
34         }
35         
36         public PDFPageStream(Rectangle pageSize) {
37                 this.pageSize = pageSize;
38                 calculateContent();
39         }
40         
41         public PDFPageStream(boolean modifiable) {
42                 this.modifiable = modifiable;
43                 calculateContent();
44         }
45         
46         public PDFPageStream(Rectangle pageSize, boolean modifiable) {
47                 this.pageSize = pageSize;
48                 this.modifiable = modifiable;
49                 calculateContent();
50         }
51         
52         private void calculateContent() {
53                 contentWidth = (int)getPageSize().getWidth() - marginLeft - marginRight;
54         contentHeight = (int)getPageSize().getHeight() - marginTop - marginBottom;
55         }
56         
57
58         public Rectangle getPageSize() {
59                 return pageSize;
60         }
61         
62         public Font getDefaultFont() {
63                 return defaultFont;
64         }
65         
66         public void setDefaultFont(Font defaultFont) {
67                 this.defaultFont = defaultFont;
68         }
69         
70         public List<PDFPage> getPages() {
71                 return pages;
72         }
73         
74         public void nextPage() throws Exception{
75                 endPage();
76                 
77                 PDFPage page = new PDFPage(this);
78                 pages.add(page);
79                 currentPage++;
80                 
81         }
82
83         private void endPage() {
84                 if (pages.size() > 0) {
85                         PDFPage lastPage = pages.get(pages.size()-1);
86                         if (!modifiable && lastPage.isOpen())
87                                 lastPage.close();
88                 }
89         }
90         
91         
92         
93         public int getAvailableLines() {
94                 return getCurrentPage().availableLines;
95         }
96         
97 //      public int getTotalLines() {
98 //              return totalLines;
99 //      }
100         
101         public int getCurrentLine() {
102                 return getCurrentPage().currentLine;
103         }
104         
105         public int getContentWidth() {
106                 return contentWidth;
107         }
108         
109         public int getContentHeight() {
110                 return contentHeight;
111         }
112         
113         protected void checkNextPage() throws Exception{
114                 PDFPage page = getCurrentPage();
115                 if (contentHeight - page.currentPixel - page.getLineHeight() <= 0)
116                         nextPage();
117         }
118         
119         public int getCurrentPageIndex() {
120                 return currentPage;
121         }
122         
123         public PDFPage getCurrentPage() {
124                 return pages.get(currentPage-1);
125         }
126         
127         public void copy(PdfCopy pdfCopy) throws IOException{
128                 for (PDFPage page : pages) {
129                         if (page.isOpen())
130                                 page.close();
131                         PdfReader reader = new PdfReader(page.tempFile.getAbsolutePath());
132                         try {
133                         for (int i = 1; i <= reader.getNumberOfPages(); i++) {
134                                 PdfImportedPage ipage = pdfCopy.getImportedPage(reader, i);
135                             pdfCopy.addPage(ipage);
136                         }
137                         } catch (Exception e) {
138                                 e.printStackTrace();
139                 } finally {
140                         reader.close();
141                 }
142                 }
143         }
144         
145         public void close(String filename) throws Exception{
146                 if (pages.size() == 0) {
147                         return;
148                 }
149                 try {
150                         endPage();
151                         Document document = new Document(pageSize);
152                         PdfCopy pdfCopy = new PdfCopy(document, new FileOutputStream(filename));
153                         document.open();
154                         copy(pdfCopy);
155                         document.close();
156                         pdfCopy.close();
157                 } finally {
158                         close();
159                 }
160         }
161         
162         public void close() throws Exception{
163                 for (PDFPage page : pages) {
164                                 page.tempFile.delete();
165                 }
166                 pages.clear();
167                 defaultFont = null;
168         }
169 }