X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Freport%2Fpdf%2FPDFPageStream.java;fp=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Freport%2Fpdf%2FPDFPageStream.java;h=ae9ac704b710bcd668675f292611d17c0cf1a2e9;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/pdf/PDFPageStream.java b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/pdf/PDFPageStream.java new file mode 100644 index 000000000..ae9ac704b --- /dev/null +++ b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/pdf/PDFPageStream.java @@ -0,0 +1,169 @@ +package org.simantics.document.linking.report.pdf; + +import java.awt.Font; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.lowagie.text.Document; +import com.lowagie.text.PageSize; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.PdfCopy; +import com.lowagie.text.pdf.PdfImportedPage; +import com.lowagie.text.pdf.PdfReader; + +public class PDFPageStream { + boolean modifiable = false; + Rectangle pageSize = PageSize.A4; + Font defaultFont; + List pages = new ArrayList(); + + int contentWidth; + int contentHeight; + + int marginLeft = 20; + int marginRight = 20; + int marginTop = 10; + int marginBottom = 45; + + int currentPage = 0; + + public PDFPageStream() { + calculateContent(); + } + + public PDFPageStream(Rectangle pageSize) { + this.pageSize = pageSize; + calculateContent(); + } + + public PDFPageStream(boolean modifiable) { + this.modifiable = modifiable; + calculateContent(); + } + + public PDFPageStream(Rectangle pageSize, boolean modifiable) { + this.pageSize = pageSize; + this.modifiable = modifiable; + calculateContent(); + } + + private void calculateContent() { + contentWidth = (int)getPageSize().getWidth() - marginLeft - marginRight; + contentHeight = (int)getPageSize().getHeight() - marginTop - marginBottom; + } + + + public Rectangle getPageSize() { + return pageSize; + } + + public Font getDefaultFont() { + return defaultFont; + } + + public void setDefaultFont(Font defaultFont) { + this.defaultFont = defaultFont; + } + + public List getPages() { + return pages; + } + + public void nextPage() throws Exception{ + endPage(); + + PDFPage page = new PDFPage(this); + pages.add(page); + currentPage++; + + } + + private void endPage() { + if (pages.size() > 0) { + PDFPage lastPage = pages.get(pages.size()-1); + if (!modifiable && lastPage.isOpen()) + lastPage.close(); + } + } + + + + public int getAvailableLines() { + return getCurrentPage().availableLines; + } + +// public int getTotalLines() { +// return totalLines; +// } + + public int getCurrentLine() { + return getCurrentPage().currentLine; + } + + public int getContentWidth() { + return contentWidth; + } + + public int getContentHeight() { + return contentHeight; + } + + protected void checkNextPage() throws Exception{ + PDFPage page = getCurrentPage(); + if (contentHeight - page.currentPixel - page.getLineHeight() <= 0) + nextPage(); + } + + public int getCurrentPageIndex() { + return currentPage; + } + + public PDFPage getCurrentPage() { + return pages.get(currentPage-1); + } + + public void copy(PdfCopy pdfCopy) throws IOException{ + for (PDFPage page : pages) { + if (page.isOpen()) + page.close(); + PdfReader reader = new PdfReader(page.tempFile.getAbsolutePath()); + try { + for (int i = 1; i <= reader.getNumberOfPages(); i++) { + PdfImportedPage ipage = pdfCopy.getImportedPage(reader, i); + pdfCopy.addPage(ipage); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + reader.close(); + } + } + } + + public void close(String filename) throws Exception{ + if (pages.size() == 0) { + return; + } + try { + endPage(); + Document document = new Document(pageSize); + PdfCopy pdfCopy = new PdfCopy(document, new FileOutputStream(filename)); + document.open(); + copy(pdfCopy); + document.close(); + pdfCopy.close(); + } finally { + close(); + } + } + + public void close() throws Exception{ + for (PDFPage page : pages) { + page.tempFile.delete(); + } + pages.clear(); + defaultFont = null; + } +}