1 package org.simantics.document.linking.report.pdf;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.ArrayList;
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;
16 public class PDFPageStream {
17 boolean modifiable = false;
18 Rectangle pageSize = PageSize.A4;
20 List<PDFPage> pages = new ArrayList<PDFPage>();
28 int marginBottom = 45;
32 public PDFPageStream() {
36 public PDFPageStream(Rectangle pageSize) {
37 this.pageSize = pageSize;
41 public PDFPageStream(boolean modifiable) {
42 this.modifiable = modifiable;
46 public PDFPageStream(Rectangle pageSize, boolean modifiable) {
47 this.pageSize = pageSize;
48 this.modifiable = modifiable;
52 private void calculateContent() {
53 contentWidth = (int)getPageSize().getWidth() - marginLeft - marginRight;
54 contentHeight = (int)getPageSize().getHeight() - marginTop - marginBottom;
58 public Rectangle getPageSize() {
62 public Font getDefaultFont() {
66 public void setDefaultFont(Font defaultFont) {
67 this.defaultFont = defaultFont;
70 public List<PDFPage> getPages() {
74 public void nextPage() throws Exception{
77 PDFPage page = new PDFPage(this);
83 private void endPage() {
84 if (pages.size() > 0) {
85 PDFPage lastPage = pages.get(pages.size()-1);
86 if (!modifiable && lastPage.isOpen())
93 public int getAvailableLines() {
94 return getCurrentPage().availableLines;
97 // public int getTotalLines() {
101 public int getCurrentLine() {
102 return getCurrentPage().currentLine;
105 public int getContentWidth() {
109 public int getContentHeight() {
110 return contentHeight;
113 protected void checkNextPage() throws Exception{
114 PDFPage page = getCurrentPage();
115 if (contentHeight - page.currentPixel - page.getLineHeight() <= 0)
119 public int getCurrentPageIndex() {
123 public PDFPage getCurrentPage() {
124 return pages.get(currentPage-1);
127 public void copy(PdfCopy pdfCopy) throws IOException{
128 for (PDFPage page : pages) {
131 PdfReader reader = new PdfReader(page.tempFile.getAbsolutePath());
133 for (int i = 1; i <= reader.getNumberOfPages(); i++) {
134 PdfImportedPage ipage = pdfCopy.getImportedPage(reader, i);
135 pdfCopy.addPage(ipage);
137 } catch (Exception e) {
145 public void close(String filename) throws Exception{
146 if (pages.size() == 0) {
151 Document document = new Document(pageSize);
152 PdfCopy pdfCopy = new PdfCopy(document, new FileOutputStream(filename));
162 public void close() throws Exception{
163 for (PDFPage page : pages) {
164 page.tempFile.delete();