From 07a9ceecac85c3c0f329285ae113bfdd5f2971bd Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Fri, 22 May 2015 08:59:04 +0200 Subject: TTF font caching, Import cleanup, auto positioning in rotated pages --- .../egiz/pdfas/lib/impl/pdfbox/PDFBOXObject.java | 9 ++ .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 1 - .../lib/impl/stamping/pdfbox/FontInfoCache.java | 1 + .../impl/stamping/pdfbox/PDFAsTemplateCreator.java | 4 +- .../pdfbox/PDFAsVisualSignatureBuilder.java | 4 +- .../pdfbox/PDFAsVisualSignatureProperties.java | 7 +- .../pdfas/lib/impl/stamping/pdfbox/PDFBoxFont.java | 48 ++++--- .../lib/impl/stamping/pdfbox/PDFBoxTable.java | 45 +++--- .../lib/impl/stamping/pdfbox/PdfBoxStamper.java | 2 +- .../impl/stamping/pdfbox/PdfBoxVisualObject.java | 12 +- .../lib/impl/stamping/pdfbox/TableDrawUtils.java | 6 +- .../at/knowcenter/wag/egov/egiz/pdf/PDFPage.java | 157 ++++++++++++--------- 12 files changed, 166 insertions(+), 130 deletions(-) (limited to 'pdf-as-pdfbox/src/main/java/at') diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox/PDFBOXObject.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox/PDFBOXObject.java index f80df075..ef4c055c 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox/PDFBOXObject.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox/PDFBOXObject.java @@ -1,10 +1,13 @@ package at.gv.egiz.pdfas.lib.impl.pdfbox; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import javax.activation.DataSource; import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.font.PDFont; import at.gv.egiz.pdfas.lib.impl.status.OperationStatus; import at.gv.egiz.pdfas.lib.impl.status.PDFObject; @@ -13,6 +16,8 @@ public class PDFBOXObject extends PDFObject { private PDDocument doc; + private Map fontCache = new HashMap(); + public PDFBOXObject(OperationStatus operationStatus) { super(operationStatus); } @@ -56,4 +61,8 @@ public class PDFBOXObject extends PDFObject { public String getPDFVersion() { return String.valueOf(getDocument().getDocument().getVersion()); } + + public Map getFontCache() { + return fontCache; + } } diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index 8095911d..cacd39c0 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java @@ -53,7 +53,6 @@ import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageNode; import org.apache.pdfbox.pdmodel.PDResources; import org.apache.pdfbox.pdmodel.common.PDNumberTreeNode; -import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDAttributeObject; import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement; import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot; import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent; diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/FontInfoCache.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/FontInfoCache.java index 10a5c9f8..3f85e445 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/FontInfoCache.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/FontInfoCache.java @@ -4,4 +4,5 @@ public class FontInfoCache { String filename; String fontName; String fontFamily; + String fontPath; } diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsTemplateCreator.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsTemplateCreator.java index 8e03c4d4..0ceb6139 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsTemplateCreator.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsTemplateCreator.java @@ -55,7 +55,7 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { } - public InputStream buildPDF(PDFAsVisualSignatureDesigner properties) + public InputStream buildPDF(PDFAsVisualSignatureDesigner properties, PDDocument originalDocument) throws IOException, PdfAsException { logger.debug("pdf building has been started"); PDFTemplateStructure pdfStructure = pdfBuilder.getStructure(); @@ -108,7 +108,7 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { properties.getRotation() + properties.getPageRotation()); // inner formstream, form and resource (hlder form containts inner form) - this.pdfBuilder.createInnerFormStreamPdfAs(template); + this.pdfBuilder.createInnerFormStreamPdfAs(template, originalDocument); this.pdfBuilder.createInnerFormResource(); PDResources innerFormResource = pdfStructure.getInnerFormResources(); this.pdfBuilder.createInnerForm(innerFormResource, pdfStructure.getInnterFormStream(), formater); diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java index 33e4102d..cd3d1899 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java @@ -241,7 +241,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements } } - public void createInnerFormStreamPdfAs(PDDocument template) + public void createInnerFormStreamPdfAs(PDDocument template, PDDocument origDoc) throws PdfAsException { try { @@ -332,7 +332,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements appendRawCommands(getStructure().getHolderFormStream() .createOutputStream(), holderFormComment.trim().replace("\n", "").replace("\r", "")); appendRawCommands(getStructure().getInnterFormStream() - .createOutputStream(), innerFormComment.trim().replace("\n", "").replace("\r", "")); + .createOutputStream(), innerFormComment/*.trim().replace("\n", "").replace("\r", "")*/); // appendRawCommands(getStructure().getImageFormStream().createOutputStream(), // imgFormComment); logger.debug("Injected apereance stream to pdf"); diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java index 3fdc6b4c..0b00cac1 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java @@ -28,7 +28,6 @@ import java.util.List; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; -import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,6 +51,8 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { private float rotationAngle = 0; + private PDDocument origDoc; + private SignatureProfileSettings signatureProfileSettings; private String alternativeTableCaption=""; @@ -67,7 +68,7 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { } this.rotationAngle = pos.getRotation(); try { - PDDocument origDoc = object.getDocument(); + origDoc = object.getDocument(); designer = new PDFAsVisualSignatureDesigner(origDoc, pos.getPage(), this, pos.isMakeNewPage()); List pages = origDoc.getDocumentCatalog().getAllPages(); @@ -113,7 +114,7 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { PDFAsVisualSignatureBuilder builder = new PDFAsVisualSignatureBuilder(this, this.settings, designer); PDFAsTemplateCreator creator = new PDFAsTemplateCreator(builder); try { - setVisibleSignature(creator.buildPDF(designer)); + setVisibleSignature(creator.buildPDF(designer, this.origDoc)); } catch (PdfAsException e) { throw new PdfAsWrappedIOException(e); } diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxFont.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxFont.java index fdbf40e2..05c7eb2a 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxFont.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxFont.java @@ -40,7 +40,6 @@ import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSDictionary; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.cos.COSObject; -import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; @@ -48,6 +47,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.settings.ISettings; +import at.gv.egiz.pdfas.lib.impl.pdfbox.PDFBOXObject; public class PDFBoxFont { @@ -94,7 +94,6 @@ public class PDFBoxFont { float fontSize; String fontDesc; String ttfFontDesc; - PDDocument doc; ISettings settings; private FontInfoCache getFontInfo(String pathName) { @@ -128,6 +127,7 @@ public class PDFBoxFont { fontInfo.filename = pathName; fontInfo.fontFamily = fontFamilyToLoad; fontInfo.fontName = fontNameToLoad; + fontInfo.fontPath = pathName; fontInfoCache.put(pathName, fontInfo); return fontInfo; } catch (Throwable e) { @@ -138,9 +138,13 @@ public class PDFBoxFont { } } - private PDFont findCachedFont(PDDocument doc, FontInfoCache fontInfo) { + private PDFont findCachedFont(PDFBOXObject pdfObject, FontInfoCache fontInfo) { try { - List cosObjects = doc.getDocument().getObjectsByType( + if(pdfObject.getFontCache().containsKey(fontInfo.fontPath)) { + return pdfObject.getFontCache().get(fontInfo.fontPath); + } + + List cosObjects = pdfObject.getDocument().getDocument().getObjectsByType( COSName.FONT); //COSName cosFontName = COSName.getPDFName(fontInfo.fontName); @@ -182,17 +186,17 @@ public class PDFBoxFont { return null; } - private PDFont generateTTF(String fonttype, PDDocument doc) + private PDFont generateTTF(String fonttype, PDFBOXObject pdfObject) throws IOException { - boolean cacheNow = false; - if (doc == null) { + /*boolean cacheNow = true; + if (pdfObject == null) { if (this.doc == null) { this.doc = new PDDocument(); } doc = this.doc; } else { cacheNow = true; - } + }*/ ttfFontDesc = fonttype; String fontName = fonttype.replaceFirst("TTF:", ""); String fontPath = this.settings.getWorkingDirectory() + File.separator @@ -208,7 +212,7 @@ public class PDFBoxFont { if(fontInfo != null) { - PDFont font = findCachedFont(doc, fontInfo); + PDFont font = findCachedFont(pdfObject, fontInfo); if (font != null) { return font; @@ -218,7 +222,7 @@ public class PDFBoxFont { logger.debug("Instantiating font."); //if (cacheNow) { - cachedfont = PDTrueTypeFont.loadTTF(doc, fontPath); + cachedfont = PDTrueTypeFont.loadTTF(pdfObject.getDocument(), fontPath); fontStyleMap.put(fontPath, cachedfont); return cachedfont; //} else { @@ -228,10 +232,10 @@ public class PDFBoxFont { } private PDFont generateFont(String fonttype, String fontder, - PDDocument originalDoc) throws IOException { + PDFBOXObject pdfObject) throws IOException { if (fonttype.startsWith("TTF:")) { // Load TTF Font - return generateTTF(fonttype, originalDoc); + return generateTTF(fonttype, pdfObject); } else { if (fontder == null) { fontder = NORMAL; @@ -247,15 +251,15 @@ public class PDFBoxFont { } } - private void setFont(String desc, PDDocument originalDoc) + private void setFont(String desc, PDFBOXObject pdfObject) throws IOException { String[] fontArr = desc.split(","); if (fontArr.length == 3) { - font = generateFont(fontArr[0], fontArr[2], originalDoc); + font = generateFont(fontArr[0], fontArr[2], pdfObject); fontSize = Float.parseFloat(fontArr[1]); } else if (fontArr.length == 2 && fontArr[0].startsWith("TTF:")) { - font = generateFont(fontArr[0], null, originalDoc); + font = generateFont(fontArr[0], null, pdfObject); fontSize = Float.parseFloat(fontArr[1]); } else { logger.warn( @@ -268,22 +272,24 @@ public class PDFBoxFont { } public PDFBoxFont(String fontDesc, ISettings settings, - PDDocument originalDoc) throws IOException { + PDFBOXObject pdfObject) throws IOException { this.settings = settings; this.fontDesc = fontDesc; logger.debug("Creating Font: " + fontDesc); - this.setFont(fontDesc, originalDoc); + this.setFont(fontDesc, pdfObject); } - public PDFont getFont(PDDocument doc) throws IOException { + public PDFont getFont(/*PDFBOXObject pdfObject*/) throws IOException { if (cachedfont != null) { return cachedfont; } - if (font instanceof PDTrueTypeFont && doc != null) { - return generateTTF(ttfFontDesc, doc); + return font; + /* + if (font instanceof PDTrueTypeFont && pdfObject != null) { + return generateTTF(ttfFontDesc, pdfObject); } else { return font; - } + }*/ } public float getFontSize() { diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java index 8959d631..d5c5d683 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java @@ -25,8 +25,6 @@ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; import java.awt.Color; import java.awt.Dimension; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; @@ -42,6 +40,7 @@ import at.gv.egiz.pdfas.common.exceptions.PdfAsWrappedIOException; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.common.utils.ImageUtils; import at.gv.egiz.pdfas.common.utils.StringUtils; +import at.gv.egiz.pdfas.lib.impl.pdfbox.PDFBOXObject; import at.knowcenter.wag.egov.egiz.table.Entry; import at.knowcenter.wag.egov.egiz.table.Style; import at.knowcenter.wag.egov.egiz.table.Table; @@ -70,6 +69,8 @@ public class PDFBoxTable { PDDocument originalDoc; + PDFBOXObject pdfBoxObject; + private void normalizeContent(Table abstractTable) throws PdfAsException { try { int rows = abstractTable.getRows().size(); @@ -94,7 +95,7 @@ public class PDFBoxTable { } private void initializeStyle(Table abstractTable, PDFBoxTable parent, - PDDocument originalDoc) throws IOException { + PDFBOXObject pdfBoxObject) throws IOException { this.table = abstractTable; try { normalizeContent(abstractTable); @@ -130,7 +131,7 @@ public class PDFBoxTable { + abstractTable.getName()); } - font = new PDFBoxFont(fontString, settings, originalDoc); + font = new PDFBoxFont(fontString, settings, pdfBoxObject); if (vfontString == null && parent != null && parent.style != null) { vfontString = parent.style.getValueFont(); @@ -140,7 +141,7 @@ public class PDFBoxTable { + abstractTable.getName()); } - valueFont = new PDFBoxFont(vfontString, settings, originalDoc); + valueFont = new PDFBoxFont(vfontString, settings, pdfBoxObject); } padding = style.getPadding(); @@ -148,11 +149,12 @@ public class PDFBoxTable { } public PDFBoxTable(Table abstractTable, PDFBoxTable parent, float fixSize, - ISettings settings, PDDocument originalDoc) throws IOException, + ISettings settings, PDFBOXObject pdfBoxObject) throws IOException, PdfAsException { this.settings = settings; - this.originalDoc = originalDoc; - initializeStyle(abstractTable, parent, originalDoc); + this.pdfBoxObject = pdfBoxObject; + this.originalDoc = pdfBoxObject.getDocument(); + initializeStyle(abstractTable, parent, pdfBoxObject); float[] relativSizes = abstractTable.getColsRelativeWith(); if (relativSizes != null) { colWidths = new float[relativSizes.length]; @@ -184,11 +186,12 @@ public class PDFBoxTable { } public PDFBoxTable(Table abstractTable, PDFBoxTable parent, - ISettings settings, PDDocument originalDoc) throws IOException, + ISettings settings, PDFBOXObject pdfBoxObject) throws IOException, PdfAsException { this.settings = settings; - this.originalDoc = originalDoc; - initializeStyle(abstractTable, parent, originalDoc); + this.pdfBoxObject = pdfBoxObject; + this.originalDoc = pdfBoxObject.getDocument(); + initializeStyle(abstractTable, parent, pdfBoxObject); this.calculateWidthHeight(); } @@ -318,10 +321,10 @@ public class PDFBoxTable { float fontSize; String string = (String) cell.getValue(); if (isValue) { - c = valueFont.getFont(null); + c = valueFont.getFont();//null fontSize = valueFont.getFontSize(); } else { - c = font.getFont(null); + c = font.getFont();//null fontSize = font.getFontSize(); } if (string == null) { @@ -350,7 +353,7 @@ public class PDFBoxTable { PDFBoxTable pdfBoxTable = null; if (cell.getValue() instanceof Table) { pdfBoxTable = new PDFBoxTable((Table) cell.getValue(), this, - this.settings, originalDoc); + this.settings, pdfBoxObject); cell.setValue(pdfBoxTable); } else if (cell.getValue() instanceof PDFBoxTable) { pdfBoxTable = (PDFBoxTable) cell.getValue(); @@ -526,10 +529,10 @@ public class PDFBoxTable { float fontSize; String string = (String) cell.getValue(); if (isValue) { - c = valueFont.getFont(null); + c = valueFont.getFont();//null fontSize = valueFont.getFontSize(); } else { - c = font.getFont(null); + c = font.getFont();//null fontSize = font.getFontSize(); } @@ -554,13 +557,13 @@ public class PDFBoxTable { PDFBoxTable pdfBoxTable = null; if (cell.getValue() instanceof Table) { pdfBoxTable = new PDFBoxTable((Table) cell.getValue(), this, - width, this.settings, this.originalDoc); + width, this.settings, this.pdfBoxObject); cell.setValue(pdfBoxTable); } else if (cell.getValue() instanceof PDFBoxTable) { // recreate here beacuse of fixed width! pdfBoxTable = (PDFBoxTable) cell.getValue(); pdfBoxTable = new PDFBoxTable(pdfBoxTable.table, this, width, - this.settings, this.originalDoc); + this.settings, this.pdfBoxObject); cell.setValue(pdfBoxTable); } else { throw new IOException("Failed to build PDFBox Table"); @@ -582,10 +585,10 @@ public class PDFBoxTable { float fontSize; String string = (String) cell.getValue(); if (isValue) { - c = valueFont.getFont(null); + c = valueFont.getFont();//null fontSize = valueFont.getFontSize(); } else { - c = font.getFont(null); + c = font.getFont();//null fontSize = font.getFontSize(); } @@ -611,7 +614,7 @@ public class PDFBoxTable { PDFBoxTable pdfBoxTable = null; if (cell.getValue() instanceof Table) { pdfBoxTable = new PDFBoxTable((Table) cell.getValue(), this, - this.settings, originalDoc); + this.settings, pdfBoxObject); cell.setValue(pdfBoxTable); } else if (cell.getValue() instanceof PDFBoxTable) { pdfBoxTable = (PDFBoxTable) cell.getValue(); diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxStamper.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxStamper.java index 3dd1f0a4..6d1fccff 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxStamper.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxStamper.java @@ -48,7 +48,7 @@ public class PdfBoxStamper implements IPDFStamper { public IPDFVisualObject createVisualPDFObject(PDFObject pdf, Table table) throws IOException { try { PDFBOXObject pdfboxObject = (PDFBOXObject)pdf; - return new PdfBoxVisualObject(table, pdf.getStatus().getSettings(), pdfboxObject.getDocument()); + return new PdfBoxVisualObject(table, pdf.getStatus().getSettings(), pdfboxObject); } catch (PdfAsException e) { throw new PdfAsWrappedIOException(e); } diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java index 9fd8ed84..feacf3d1 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java @@ -25,12 +25,12 @@ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; import java.io.IOException; -import org.apache.pdfbox.pdmodel.PDDocument; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.settings.ISettings; +import at.gv.egiz.pdfas.lib.impl.pdfbox.PDFBOXObject; import at.gv.egiz.pdfas.lib.impl.stamping.IPDFVisualObject; import at.knowcenter.wag.egov.egiz.table.Table; @@ -46,13 +46,13 @@ public class PdfBoxVisualObject implements IPDFVisualObject { private float y; private int page; private ISettings settings; - private PDDocument originalDoc; + private PDFBOXObject pdfBoxObject; - public PdfBoxVisualObject(Table table, ISettings settings, PDDocument originalDoc) + public PdfBoxVisualObject(Table table, ISettings settings, PDFBOXObject pdfBoxObject) throws IOException, PdfAsException { this.abstractTable = table; - this.originalDoc = originalDoc; - this.table = new PDFBoxTable(table, null, settings, originalDoc); + this.pdfBoxObject = pdfBoxObject; + this.table = new PDFBoxTable(table, null, settings, pdfBoxObject); this.settings = settings; } @@ -62,7 +62,7 @@ public class PdfBoxVisualObject implements IPDFVisualObject { public void fixWidth() { try { - table = new PDFBoxTable(abstractTable, null, this.width, settings, this.originalDoc); + table = new PDFBoxTable(abstractTable, null, this.width, settings, this.pdfBoxObject); } catch (IOException e) { logger.warn("Failed to fix width of Table!", e); } catch (PdfAsException e) { diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java index 9b0f5ae1..7388090c 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java @@ -24,7 +24,6 @@ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; import java.awt.Color; -import java.beans.DesignMode; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; @@ -41,7 +40,6 @@ import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.settings.ISettings; -import at.gv.egiz.pdfas.lib.impl.signing.pdfbox.PADESPDFBOXSigner; import at.knowcenter.wag.egov.egiz.table.Entry; import at.knowcenter.wag.egov.egiz.table.Style; @@ -278,7 +276,7 @@ public class TableDrawUtils { float fontSize = PDFBoxFont.defaultFontSize; PDFont textFont = PDFBoxFont.defaultFont; - textFont = abstractTable.getFont().getFont(doc); + textFont = abstractTable.getFont().getFont();//doc); fontSize = abstractTable.getFont().getFontSize(); // get the cell Text @@ -312,7 +310,7 @@ public class TableDrawUtils { float fontSize = PDFBoxFont.defaultFontSize; PDFont textFont = PDFBoxFont.defaultFont; - textFont = abstractTable.getValueFont().getFont(doc); + textFont = abstractTable.getValueFont().getFont();//doc); fontSize = abstractTable.getValueFont().getFontSize(); // get the cell Text diff --git a/pdf-as-pdfbox/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFPage.java b/pdf-as-pdfbox/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFPage.java index a7aaf2df..48e6d3d2 100644 --- a/pdf-as-pdfbox/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFPage.java +++ b/pdf-as-pdfbox/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFPage.java @@ -93,7 +93,8 @@ import at.knowcenter.wag.egov.egiz.pdf.operator.path.painting.StrokePath; /** * PDFPage is an inner class that is used to calculate the page length of a PDF * Document page. It extends the PDFTextStripper class and implement one - * interested method: {@link at.knowcenter.wag.egov.egiz.pdf.PDFPage#showCharacter(TextPosition)}
+ * interested method: + * {@link at.knowcenter.wag.egov.egiz.pdf.PDFPage#showCharacter(TextPosition)}
* This method is called when processing the FileStream. By calling the method * {@link org.apache.pdfbox.util.PDFStreamEngine#processStream(org.apache.pdfbox.pdmodel.PDPage, org.apache.pdfbox.pdmodel.PDResources, org.pdfbox.cos.COSStream)} * the implemented method showCharacter is called. @@ -105,7 +106,7 @@ public class PDFPage extends PDFTextStripper { /** * The logger definition. */ - private static final Logger logger = LoggerFactory.getLogger(PDFPage.class); + private static final Logger logger = LoggerFactory.getLogger(PDFPage.class); /** * The maximum (lowest) y position of a character. @@ -141,15 +142,16 @@ public class PDFPage extends PDFTextStripper { * * @throws java.io.IOException */ - public PDFPage(float effectivePageHeight, boolean legacy32) throws IOException { + public PDFPage(float effectivePageHeight, boolean legacy32) + throws IOException { super(); this.effectivePageHeight = effectivePageHeight; OperatorProcessor newInvoke = new MyInvoke(this); newInvoke.setContext(this); - this.registerOperatorProcessor("Do", newInvoke); - + this.registerOperatorProcessor("Do", newInvoke); + if (!legacy32) { registerCustomPathOperators(); } @@ -165,26 +167,32 @@ public class PDFPage extends PDFTextStripper { // *** path construction - this.registerOperatorProcessor("m", new MoveTo(this)); - this.registerOperatorProcessor("l", new LineTo(this)); - this.registerOperatorProcessor("c", new CurveTo(this)); - this.registerOperatorProcessor("y", new CurveToReplicateFinalPoint(this)); - this.registerOperatorProcessor("v", new CurveToReplicateInitialPoint(this)); - this.registerOperatorProcessor("h", new ClosePath(this)); + this.registerOperatorProcessor("m", new MoveTo(this)); + this.registerOperatorProcessor("l", new LineTo(this)); + this.registerOperatorProcessor("c", new CurveTo(this)); + this.registerOperatorProcessor("y", + new CurveToReplicateFinalPoint(this)); + this.registerOperatorProcessor("v", new CurveToReplicateInitialPoint( + this)); + this.registerOperatorProcessor("h", new ClosePath(this)); // *** path painting // "S": stroke path - this.registerOperatorProcessor("S", new StrokePath(this)); - this.registerOperatorProcessor("s", new CloseAndStrokePath(this)); - this.registerOperatorProcessor("f", new FillPathNonZeroWindingNumberRule(this)); - this.registerOperatorProcessor("F", new FillPathNonZeroWindingNumberRule(this)); - this.registerOperatorProcessor("f*", new FillPathEvenOddRule(this)); - this.registerOperatorProcessor("b", new CloseFillNonZeroAndStrokePath(this)); - this.registerOperatorProcessor("B", new FillNonZeroAndStrokePath(this)); - this.registerOperatorProcessor("b*", new CloseFillEvenOddAndStrokePath(this)); - this.registerOperatorProcessor("B*", new FillEvenOddAndStrokePath(this)); - this.registerOperatorProcessor("n", new EndPath(this)); + this.registerOperatorProcessor("S", new StrokePath(this)); + this.registerOperatorProcessor("s", new CloseAndStrokePath(this)); + this.registerOperatorProcessor("f", + new FillPathNonZeroWindingNumberRule(this)); + this.registerOperatorProcessor("F", + new FillPathNonZeroWindingNumberRule(this)); + this.registerOperatorProcessor("f*", new FillPathEvenOddRule(this)); + this.registerOperatorProcessor("b", new CloseFillNonZeroAndStrokePath( + this)); + this.registerOperatorProcessor("B", new FillNonZeroAndStrokePath(this)); + this.registerOperatorProcessor("b*", new CloseFillEvenOddAndStrokePath( + this)); + this.registerOperatorProcessor("B*", new FillEvenOddAndStrokePath(this)); + this.registerOperatorProcessor("n", new EndPath(this)); // Note: The graphic context // (org.pdfbox.pdmodel.graphics.PDGraphicsState) of the underlying @@ -235,11 +243,11 @@ public class PDFPage extends PDFTextStripper { float lowerBoundYPositionFromTop; PDRectangle boundaryBox = this.getCurrentPage().findCropBox(); - - if(boundaryBox == null) { + + if (boundaryBox == null) { boundaryBox = this.getCurrentPage().findMediaBox(); } - + float pageHeight; switch (this.getCurrentPage().findRotation()) { @@ -298,7 +306,7 @@ public class PDFPage extends PDFTextStripper { protected void processTextPosition(TextPosition text) { showCharacter(text); } - + // exthex /** * A method provided as an event interface to allow a subclass to perform @@ -314,6 +322,11 @@ public class PDFPage extends PDFTextStripper { float current_y = text.getY(); final String character = text.getCharacter(); + if (at.gv.egiz.pdfas.common.utils.StringUtils.whiteSpaceTrim(character) + .isEmpty()) { + return; + } + int pageRotation = this.getCurrentPage().findRotation(); // logger_.debug("PageRotation = " + pageRotation); if (pageRotation == 0) { @@ -328,15 +341,14 @@ public class PDFPage extends PDFTextStripper { if (pageRotation == 270) { current_y = text.getY(); } - - if (current_y > this.effectivePageHeight) { - this.max_character_ypos=this.effectivePageHeight; + + if (current_y > this.effectivePageHeight) { + this.max_character_ypos = this.effectivePageHeight; return; } - + // store ypos of the char if it is not empty - if (!at.gv.egiz.pdfas.common.utils.StringUtils.whiteSpaceTrim(character).isEmpty() && - current_y > this.max_character_ypos) { + if (current_y > this.max_character_ypos) { this.max_character_ypos = current_y; } @@ -359,35 +371,39 @@ public class PDFPage extends PDFTextStripper { + ", path=" + maxPathRelatedYPositionFromTop); } return NumberUtils.max(max_character_ypos, max_image_ypos, - maxPathRelatedYPositionFromTop); + maxPathRelatedYPositionFromTop); } @Override public Map getFonts() { - + COSBase fontObj = null; - - if(getCurrentPage().getResources() != null && - getCurrentPage().getResources().getCOSDictionary() != null && - getCurrentPage().getResources().getCOSDictionary().getDictionaryObject(COSName.FONT) != null) { - fontObj = getCurrentPage().getResources().getCOSDictionary().getDictionaryObject(COSName.FONT); + + if (getCurrentPage().getResources() != null + && getCurrentPage().getResources().getCOSDictionary() != null + && getCurrentPage().getResources().getCOSDictionary() + .getDictionaryObject(COSName.FONT) != null) { + fontObj = getCurrentPage().getResources().getCOSDictionary() + .getDictionaryObject(COSName.FONT); } - Map fontMap = getCurrentPage().findResources().getFonts(); - - if(fontObj != null) { - getCurrentPage().getResources().getCOSDictionary().setItem(COSName.FONT, fontObj); + Map fontMap = getCurrentPage().findResources() + .getFonts(); + + if (fontObj != null) { + getCurrentPage().getResources().getCOSDictionary() + .setItem(COSName.FONT, fontObj); } - + return fontMap; } - + public class MyInvoke extends OperatorProcessor { - private PDFPage mypage; + private PDFPage mypage; - public MyInvoke(PDFPage page) { - this.mypage = page; - } + public MyInvoke(PDFPage page) { + this.mypage = page; + } public void process(PDFOperator operator, List arguments) throws IOException { @@ -408,7 +424,7 @@ public class PDFPage extends PDFTextStripper { Matrix ctm = context.getGraphicsState() .getCurrentTransformationMatrix(); - logger.debug("ctm = " + ctm); + logger.debug("ctm = " + ctm); Pos[] coordinates = new Pos[] { new Pos(0, 0, 1), new Pos(1, 0, 1), new Pos(0, 1, 1), new Pos(1, 1, 1) }; @@ -430,9 +446,10 @@ public class PDFPage extends PDFTextStripper { logger.debug("PageRotation = " + pageRotation); if (pageRotation == 0) { float min_y = findMinY(transformed_coordinates); - logger.debug("min_y = " + min_y); - float page_height = this.mypage.getCurrentPage().findMediaBox().getHeight(); - logger.debug("page_height = " + page_height); + logger.debug("min_y = " + min_y); + float page_height = this.mypage.getCurrentPage() + .findMediaBox().getHeight(); + logger.debug("page_height = " + page_height); actual_lowest_point = page_height - min_y; actual_starting_point = page_height @@ -440,36 +457,38 @@ public class PDFPage extends PDFTextStripper { } if (pageRotation == 90) { float max_x = findMaxX(transformed_coordinates); - logger.debug("max_x = " + max_x); - float page_width = this.mypage.getCurrentPage().findMediaBox().getWidth(); - logger.debug("page_width = " + page_width); + logger.debug("max_x = " + max_x); + float page_width = this.mypage.getCurrentPage() + .findMediaBox().getWidth(); + logger.debug("page_width = " + page_width); actual_lowest_point = max_x; actual_starting_point = findMinX(transformed_coordinates); } if (pageRotation == 180) { float min_y = findMinY(transformed_coordinates); - logger.debug("min_y = " + min_y); + logger.debug("min_y = " + min_y); actual_lowest_point = findMaxY(transformed_coordinates); actual_starting_point = actual_lowest_point + min_y; } if (pageRotation == 270) { float min_x = findMinX(transformed_coordinates); - logger.debug("min_x = " + min_x); + logger.debug("min_x = " + min_x); - float page_width = this.mypage.getCurrentPage().findMediaBox().getWidth(); - logger.debug("page_width = " + page_width); + float page_width = this.mypage.getCurrentPage() + .findMediaBox().getWidth(); + logger.debug("page_width = " + page_width); actual_lowest_point = page_width - min_x; actual_starting_point = page_width - findMaxX(transformed_coordinates); } - logger.debug("actual_lowest_point = " + actual_lowest_point); + logger.debug("actual_lowest_point = " + actual_lowest_point); if (actual_lowest_point > PDFPage.this.effectivePageHeight && actual_starting_point > PDFPage.this.effectivePageHeight) { - logger.debug("image is below footer_line"); + logger.debug("image is below footer_line"); return; } @@ -511,7 +530,7 @@ public class PDFPage extends PDFTextStripper { transformed.z = pos.x * m.getValue(0, 2) + pos.y * m.getValue(1, 2) + pos.z * m.getValue(2, 2); - logger.debug(" transformed " + pos + " --> " + transformed); + logger.debug(" transformed " + pos + " --> " + transformed); return transformed; } @@ -558,16 +577,16 @@ public class PDFPage extends PDFTextStripper { public void processAnnotation(PDAnnotation anno) { float current_y = anno.getRectangle().getLowerLeftY(); PDPage page = anno.getPage(); - - if(page == null) { + + if (page == null) { page = getCurrentPage(); } - - if(page == null) { + + if (page == null) { logger.warn("Annotation without page! The position might not be correct!"); return; } - + int pageRotation = page.findRotation(); // logger_.debug("PageRotation = " + pageRotation); if (pageRotation == 0) { @@ -582,7 +601,7 @@ public class PDFPage extends PDFTextStripper { } if (pageRotation == 270) { float page_width = page.findMediaBox().getWidth(); - current_y = page_width - anno.getRectangle().getLowerLeftX() ; + current_y = page_width - anno.getRectangle().getLowerLeftX(); } if (current_y > this.effectivePageHeight) { @@ -594,5 +613,5 @@ public class PDFPage extends PDFTextStripper { this.max_character_ypos = current_y; } } - + } -- cgit v1.2.3