From 24906c4daba2b9717a0db9ab616d1a9e8ba3939b Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Fri, 17 Apr 2015 09:57:30 +0200 Subject: started pdf/ua implementation --- .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 172 +++++++++++++++++---- .../pdfbox/PDFAsVisualSignatureBuilder.java | 2 +- .../pdfbox/PDFAsVisualSignatureProperties.java | 10 ++ .../lib/impl/stamping/pdfbox/TableDrawUtils.java | 27 +++- 4 files changed, 169 insertions(+), 42 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/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index 45dfbcc5..ce341023 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 @@ -39,8 +39,10 @@ import java.util.Calendar; import java.util.List; import org.apache.commons.io.IOUtils; +import org.apache.pdfbox.cos.COSArray; import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSDictionary; +import org.apache.pdfbox.cos.COSInteger; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.cos.COSString; import org.apache.pdfbox.exceptions.COSVisitorException; @@ -50,8 +52,12 @@ import org.apache.pdfbox.pdmodel.PDDocumentCatalog; 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.PDStructureElement; +import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot; import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent; import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg; +import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureOptions; import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; @@ -104,6 +110,8 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { RequestedSignature requestedSignature, PDFASSignatureInterface genericSigner) throws PdfAsException { String fisTmpFile = null; + + PDFAsVisualSignatureProperties properties=null; if (!(genericPdfObject instanceof PDFBOXObject)) { // tODO: @@ -152,8 +160,8 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { logger.debug("Placeholder Profile set to: " + signaturePlaceholderData.getProfile()); requestedSignature - .setSignatureProfileID(signaturePlaceholderData - .getProfile()); + .setSignatureProfileID(signaturePlaceholderData + .getProfile()); } tablePos = signaturePlaceholderData.getTablePos(); @@ -165,7 +173,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { PDSignature signature = new PDSignature(); signature.setFilter(COSName.getPDFName(signer.getPDFFilter())); // default - // filter + // filter signature.setSubFilter(COSName.getPDFName(signer .getPDFSubFilter())); @@ -313,7 +321,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { requestedSignature.setSignaturePosition(position); - PDFAsVisualSignatureProperties properties = new PDFAsVisualSignatureProperties( + properties = new PDFAsVisualSignatureProperties( pdfObject.getStatus().getSettings(), pdfObject, (PdfBoxVisualObject) visualObject, positioningInstruction); @@ -348,7 +356,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { logger.info("Placeholder name: " + signaturePlaceholderData - .getPlaceholderName()); + .getPlaceholderName()); COSDictionary xobjectsDictionary = (COSDictionary) page .findResources().getCOSDictionary() .getDictionaryObject(COSName.XOBJECT); @@ -356,10 +364,10 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { .getPlaceholderName(), img); xobjectsDictionary.setNeedToBeUpdate(true); page.findResources().getCOSObject() - .setNeedToBeUpdate(true); + .setNeedToBeUpdate(true); logger.info("Placeholder name: " + signaturePlaceholderData - .getPlaceholderName()); + .getPlaceholderName()); } finally { IOUtils.closeQuietly(is); } @@ -397,23 +405,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } } - // if (signatureProfileSettings.isPDFA()) { // Check for - // PDF-UA - // PDDocumentCatalog root = doc.getDocumentCatalog(); - // PDStructureTreeRoot treeRoot = - // root.getStructureTreeRoot(); - // if (treeRoot != null) { // Handle as PDF-UA - // logger.info("Tree Root: {}", treeRoot.toString()); - // PDStructureElement docElement = PDFBoxTaggingUtils - // .getDocumentElement(treeRoot); - // PDStructureElement sigBlock = new PDStructureElement( - // "Table", docElement); - // root.getCOSObject().setNeedToBeUpdate(true); - // docElement.getCOSObject().setNeedToBeUpdate(true); - // treeRoot.getCOSObject().setNeedToBeUpdate(true); - // sigBlock.setTitle("Signature Table"); - // } - // } + options.setPage(positioningInstruction.getPage()); options.setVisualSignature(properties.getVisibleSignature()); @@ -433,25 +425,35 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { sigFieldName = sigFieldName + count; PDAcroForm acroFormm = doc.getDocumentCatalog().getAcroForm(); + + // PDStructureTreeRoot pdstRoot = doc.getDocumentCatalog().getStructureTreeRoot(); + // COSDictionary dic = doc.getDocumentCatalog().getCOSDictionary(); + // PDStructureElement el = new PDStructureElement("Widget", pdstRoot); + + + + PDSignatureField signatureField = null; if (acroFormm != null) { @SuppressWarnings("unchecked") List fields = acroFormm.getFields(); - PDSignatureField signatureField = null; + if (fields != null) { for (PDField pdField : fields) { if (pdField != null) { if (pdField instanceof PDSignatureField) { PDSignatureField tmpSigField = (PDSignatureField) pdField; + if (tmpSigField.getSignature() != null && tmpSigField.getSignature() - .getDictionary() != null) { + .getDictionary() != null) { if (tmpSigField .getSignature() .getDictionary() .equals(signature .getDictionary())) { signatureField = (PDSignatureField) pdField; + } } } @@ -462,12 +464,114 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } if (signatureField != null) { - signatureField.setPartialName(sigFieldName); + signatureField.setPartialName(sigFieldName); + } + if(properties!=null){ + signatureField.setAlternateFieldName(properties.getAlternativeTableCaption()); + }else{ + signatureField.setAlternateFieldName(sigFieldName); } } else { logger.warn("Failed to name Signature Field! [Cannot find acroForm!]"); } + //marked sig + if (signatureProfileSettings.isPDFA()) { // Check for + // PDF-UA + PDDocumentCatalog root = doc.getDocumentCatalog(); + PDStructureTreeRoot treeRoot = + root.getStructureTreeRoot(); + if (treeRoot != null) { // Handle as PDF-UA + + logger.info("Tree Root: {}", treeRoot.toString()); + PDStructureTreeRoot rootElement = doc.getDocumentCatalog().getStructureTreeRoot(); + List kids = rootElement.getKids(); + + PDStructureElement docElement = null; + + for(Object k: kids){ + if(k instanceof PDStructureElement){ + if(((PDStructureElement) k).getStructureType().equals("Document")){ + docElement=(PDStructureElement) k; + } + } + } + + PDStructureElement sigBlock = new PDStructureElement( + "Form", docElement); + + COSDictionary objectdic= new COSDictionary(); + objectdic.setName("Type", "OBJR"); + objectdic.setItem("Pg", signatureField.getWidget().getPage()); + objectdic.setItem("Obj", signatureField.getWidget()); + + //page cannot be set here-->leads to pdfua syntax error + // PDObjectReference pr = new PDObjectReference(); + // pr.setReferencedObject(signatureField.getWidget()); + + + + sigBlock.setTitle("Signature Table"); + + // COSDictionary dict = new COSDictionary();//(COSDictionary) atto.getCOSObject(); + // dict.setName("O", "Layout"); + // dict.setName("Placement", "Block"); + // PDAttributeObject atto =PDAttributeObject.create(dict); + // + // sigBlock.addAttribute(atto); + // atto.getCOSObject().setNeedToBeUpdate(true); + + + + + //PDMarkedContent pdm = new PDMarkedContent("test", properties) + + List l = new ArrayList(); + l.add(objectdic); + sigBlock.setKids(l); + + + sigBlock.setParent(docElement); + //sigBlock.setPage(signatureField.getWidget().getPage()); + docElement.appendKid(sigBlock); + + PDNumberTreeNode ntn = treeRoot.getParentTree(); + if(ntn==null) + ntn = new PDNumberTreeNode(objectdic, null); + + //int index=ntn.getUpperLimit(); + + COSDictionary ntndic=ntn.getCOSDictionary(); + COSArray ntndicnumbersarray = (COSArray)ntndic.getDictionaryObject(COSName.NUMS); + int lastindex = ntndicnumbersarray.size(); + + ntndicnumbersarray.add(lastindex, new COSInteger(lastindex-1)); + ntndicnumbersarray.add(lastindex+1, sigBlock.getCOSObject()); + + treeRoot.setParentTree(ntn); + + treeRoot.setParentTreeNextKey(lastindex); + + + PDAnnotationWidget widg=signatureField.getWidget(); + widg.setStructParent(lastindex-1); + ntndicnumbersarray.setNeedToBeUpdate(true); + + ntndic.setNeedToBeUpdate(true); + docElement.getCOSObject().setNeedToBeUpdate(true); + sigBlock.getCOSObject().setNeedToBeUpdate(true); + docElement.getCOSObject().setNeedToBeUpdate(true); + rootElement.getCOSObject().setNeedToBeUpdate(true); + signatureField.getWidget().getPage().findResources().getCOSObject() + .setNeedToBeUpdate(true); + + + treeRoot.getCOSObject().setNeedToBeUpdate(true); + root.getCOSObject().setNeedToBeUpdate(true); + objectdic.getCOSObject().setNeedToBeUpdate(true); + } + } + if (requestedSignature.isVisual()) { // if(requestedSignature.getSignaturePosition().) @@ -559,7 +663,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { // Ignore } } - + if(fisTmpFile != null) { helper.deleteFile(fisTmpFile); } @@ -609,7 +713,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { public Image generateVisibleSignaturePreview(SignParameter parameter, java.security.cert.X509Certificate cert, int resolution, OperationStatus status, RequestedSignature requestedSignature) - throws PDFASError { + throws PDFASError { try { PDFBOXObject pdfObject = (PDFBOXObject) status.getPdfObject(); @@ -692,11 +796,11 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { graphics.drawImage(outputImage, 0, 0, cutOut.getWidth(), cutOut .getHeight(), (int) (1 * factor), (int) (outputImage - .getHeight() - ((position.getHeight() + 1) * factor)), - (int) ((1 + position.getWidth()) * factor), - (int) (outputImage.getHeight() - - ((position.getHeight() + 1) * factor) + (position - .getHeight() * factor)), null); + .getHeight() - ((position.getHeight() + 1) * factor)), + (int) ((1 + position.getWidth()) * factor), + (int) (outputImage.getHeight() + - ((position.getHeight() + 1) * factor) + (position + .getHeight() * factor)), null); return cutOut; } catch (PdfAsException e) { logger.warn("PDF-AS Exception", e); 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 d0b71fd0..e7672894 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 @@ -260,7 +260,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements TableDrawUtils.drawTable(getStructure().getPage(), stream, 1, 1, designer.getWidth(), designer.getHeight(), properties.getMainTable(), template, false, - innerFormResources, images, settings, this); + innerFormResources, images, settings, this, properties); stream.close(); PDStream innterFormStream = getStructure().getPage().getContents(); getStructure().setInnterFormStream(innterFormStream); 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 55a6d832..ea20858f 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 @@ -50,6 +50,8 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { private PDFAsVisualSignatureDesigner designer; private float rotationAngle = 0; + + private String alternativeTableCaption=""; public PDFAsVisualSignatureProperties(ISettings settings, PDFBOXObject object, PdfBoxVisualObject visObj, PositioningInstruction pos) { @@ -125,5 +127,13 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { public PDFAsVisualSignatureDesigner getDesigner() { return designer; } + + public String getAlternativeTableCaption() { + return alternativeTableCaption; + } + + public void setAlternativeTableCaption(String alternativeTableCaption) { + this.alternativeTableCaption = alternativeTableCaption; + } } 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 dff8e543..6074b4c9 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,6 +24,7 @@ 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; @@ -40,6 +41,7 @@ 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; @@ -49,12 +51,14 @@ public class TableDrawUtils { .getLogger(TableDrawUtils.class); public static final String TABLE_DEBUG = "debug.table"; - + + private static StringBuilder alternateTableCaption = new StringBuilder(); + public static void drawTable(PDPage page, PDPageContentStream contentStream, float x, float y, float width, float height, PDFBoxTable abstractTable, PDDocument doc, boolean subtable, PDResources formResources, - Map images, ISettings settings, IDGenerator generator) + Map images, ISettings settings, IDGenerator generator, PDFAsVisualSignatureProperties properties) throws PdfAsException { logger.debug("Drawing Table: X {} Y {} WIDTH {} HEIGHT {} \n{}", x, y, @@ -67,16 +71,16 @@ public class TableDrawUtils { drawBorder(page, contentStream, x, y, width, height, abstractTable, doc, subtable, settings); - +//append strings drawContent(page, contentStream, x, y, width, height, abstractTable, - doc, subtable, formResources, images, settings, generator); + doc, subtable, formResources, images, settings, generator, properties); } public static void drawContent(PDPage page, PDPageContentStream contentStream, float x, float y, float width, float height, PDFBoxTable abstractTable, PDDocument doc, boolean subtable, PDResources formResources, - Map images, ISettings settings, IDGenerator generator) + Map images, ISettings settings, IDGenerator generator, PDFAsVisualSignatureProperties properties) throws PdfAsException { float contentx = x; @@ -114,11 +118,13 @@ public class TableDrawUtils { drawCaption(page, contentStream, contentx, contenty, colWidth, abstractTable.getRowHeights()[i], padding, abstractTable, doc, cell, formResources, settings); + addToAlternateTableCaption(cell); break; case Entry.TYPE_VALUE: drawValue(page, contentStream, contentx, contenty, colWidth, abstractTable.getRowHeights()[i], padding, abstractTable, doc, cell, formResources, settings); + addToAlternateTableCaption(cell); break; case Entry.TYPE_IMAGE: drawImage(page, contentStream, contentx, contenty, @@ -137,7 +143,7 @@ public class TableDrawUtils { drawTable(page, contentStream, contentx, contenty - abstractTable.getRowHeights()[i], colWidth, abstractTable.getRowHeights()[i], tbl_value, doc, - true, formResources, images, settings, generator); + true, formResources, images, settings, generator,properties); break; default: logger.warn("Unknown Cell entry type: " + cell.getType()); @@ -155,6 +161,7 @@ public class TableDrawUtils { contenty -= abstractTable.getRowHeights()[i]; contentx = x; } + properties.setAlternativeTableCaption(alternateTableCaption.toString()); } private static void drawString(PDPage page, @@ -280,7 +287,7 @@ public class TableDrawUtils { Style cellStyle = cell.getStyle(); String valign = cellStyle.getVAlign(); String halign = cellStyle.getHAlign(); - + drawString(page, contentStream, contentx, contenty, width, height, padding, abstractTable, doc, cell, fontSize, textHeight, valign, halign, tlines, textFont, formResources, settings); @@ -592,5 +599,11 @@ public class TableDrawUtils { } return ""; } + + private static void addToAlternateTableCaption(Entry cell){ + alternateTableCaption.append(cell.getValue()); + alternateTableCaption.append("\n");//better for screen reader + } + } -- cgit v1.2.3 From 4d5019da1d4c1f3a3cfa7097300c4962879f3868 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Mon, 20 Apr 2015 15:14:26 +0200 Subject: pdf/ua docs - added parameter in profiles --- .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 141 ++++++++++----------- .../lib/impl/stamping/pdfbox/TableDrawUtils.java | 4 +- 2 files changed, 68 insertions(+), 77 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/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index ce341023..2e133284 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,6 +53,7 @@ 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; @@ -110,7 +111,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { RequestedSignature requestedSignature, PDFASSignatureInterface genericSigner) throws PdfAsException { String fisTmpFile = null; - + PDFAsVisualSignatureProperties properties=null; if (!(genericPdfObject instanceof PDFBOXObject)) { @@ -475,100 +476,88 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { logger.warn("Failed to name Signature Field! [Cannot find acroForm!]"); } - //marked sig - if (signatureProfileSettings.isPDFA()) { // Check for - // PDF-UA - PDDocumentCatalog root = doc.getDocumentCatalog(); - PDStructureTreeRoot treeRoot = - root.getStructureTreeRoot(); - if (treeRoot != null) { // Handle as PDF-UA + // PDF-UA + if (signatureProfileSettings.isPDFUA()==true) { + try{ + PDDocumentCatalog root = doc.getDocumentCatalog(); + PDStructureTreeRoot treeRoot = root.getStructureTreeRoot(); - logger.info("Tree Root: {}", treeRoot.toString()); - PDStructureTreeRoot rootElement = doc.getDocumentCatalog().getStructureTreeRoot(); - List kids = rootElement.getKids(); + if (treeRoot != null) { //check if structure tree available in pdf document + logger.info("Tree Root: {}", treeRoot.toString()); + List kids = treeRoot.getKids(); - PDStructureElement docElement = null; + if(kids==null){ + logger.info("No kid-elements in structure tree Root, maybe not PDF/UA document"); + } - for(Object k: kids){ - if(k instanceof PDStructureElement){ - if(((PDStructureElement) k).getStructureType().equals("Document")){ + PDStructureElement docElement = null; + for(Object k: kids){ + if(k instanceof PDStructureElement){ docElement=(PDStructureElement) k; + break; + // if(((PDStructureElement) k).getStructureType().equals("Document")){ + // docElement=(PDStructureElement) k; + // } } } - } - - PDStructureElement sigBlock = new PDStructureElement( - "Form", docElement); - - COSDictionary objectdic= new COSDictionary(); - objectdic.setName("Type", "OBJR"); - objectdic.setItem("Pg", signatureField.getWidget().getPage()); - objectdic.setItem("Obj", signatureField.getWidget()); - - //page cannot be set here-->leads to pdfua syntax error - // PDObjectReference pr = new PDObjectReference(); - // pr.setReferencedObject(signatureField.getWidget()); - - - sigBlock.setTitle("Signature Table"); + PDStructureElement sigBlock = new PDStructureElement( + "Form", docElement); - // COSDictionary dict = new COSDictionary();//(COSDictionary) atto.getCOSObject(); - // dict.setName("O", "Layout"); - // dict.setName("Placement", "Block"); - // PDAttributeObject atto =PDAttributeObject.create(dict); - // - // sigBlock.addAttribute(atto); - // atto.getCOSObject().setNeedToBeUpdate(true); + //create object dictionary and add as child element + COSDictionary objectdic= new COSDictionary(); + objectdic.setName("Type", "OBJR"); + objectdic.setItem("Pg", signatureField.getWidget().getPage()); + objectdic.setItem("Obj", signatureField.getWidget()); + List l = new ArrayList(); + l.add(objectdic); + sigBlock.setKids(l); + sigBlock.setTitle("Signature Table"); + sigBlock.setParent(docElement); + docElement.appendKid(sigBlock); + //Create and add Attribute dictionary to mitigate PAC warning + COSDictionary sigBlockDic = (COSDictionary) sigBlock.getCOSObject(); + COSDictionary sub = new COSDictionary(); - //PDMarkedContent pdm = new PDMarkedContent("test", properties) + sub.setName("O", "Layout"); + sub.setName("Placement", "Block"); + sigBlockDic.setItem(COSName.A, sub); + sigBlockDic.setNeedToBeUpdate(true); - List l = new ArrayList(); - l.add(objectdic); - sigBlock.setKids(l); + //Modify number tree + PDNumberTreeNode ntn = treeRoot.getParentTree(); + if(ntn==null){ + ntn = new PDNumberTreeNode(objectdic, null); + logger.info("No number-tree-node found!"); + } - sigBlock.setParent(docElement); - //sigBlock.setPage(signatureField.getWidget().getPage()); - docElement.appendKid(sigBlock); - - PDNumberTreeNode ntn = treeRoot.getParentTree(); - if(ntn==null) - ntn = new PDNumberTreeNode(objectdic, null); - - //int index=ntn.getUpperLimit(); - - COSDictionary ntndic=ntn.getCOSDictionary(); - COSArray ntndicnumbersarray = (COSArray)ntndic.getDictionaryObject(COSName.NUMS); - int lastindex = ntndicnumbersarray.size(); - - ntndicnumbersarray.add(lastindex, new COSInteger(lastindex-1)); - ntndicnumbersarray.add(lastindex+1, sigBlock.getCOSObject()); - - treeRoot.setParentTree(ntn); - - treeRoot.setParentTreeNextKey(lastindex); - + COSDictionary ntndic=ntn.getCOSDictionary(); + COSArray ntndicnumbersarray = (COSArray)ntndic.getDictionaryObject(COSName.NUMS); + int arrindex = ntndicnumbersarray.size(); + int treeindex = arrindex/2; - PDAnnotationWidget widg=signatureField.getWidget(); - widg.setStructParent(lastindex-1); - ntndicnumbersarray.setNeedToBeUpdate(true); + ntndicnumbersarray.add(arrindex, COSInteger.get(treeindex)); + ntndicnumbersarray.add(arrindex+1, sigBlock.getCOSObject()); - ntndic.setNeedToBeUpdate(true); - docElement.getCOSObject().setNeedToBeUpdate(true); - sigBlock.getCOSObject().setNeedToBeUpdate(true); - docElement.getCOSObject().setNeedToBeUpdate(true); - rootElement.getCOSObject().setNeedToBeUpdate(true); - signatureField.getWidget().getPage().findResources().getCOSObject() - .setNeedToBeUpdate(true); + treeRoot.setParentTree(ntn); + treeRoot.setParentTreeNextKey(treeindex+1); + //setStructureParent + PDAnnotationWidget widg=signatureField.getWidget(); + widg.setStructParent(treeindex); - treeRoot.getCOSObject().setNeedToBeUpdate(true); - root.getCOSObject().setNeedToBeUpdate(true); - objectdic.getCOSObject().setNeedToBeUpdate(true); + ntndic.setNeedToBeUpdate(true); + sigBlock.getCOSObject().setNeedToBeUpdate(true); + treeRoot.getCOSObject().setNeedToBeUpdate(true); + objectdic.getCOSObject().setNeedToBeUpdate(true); + } + }catch(Exception e){ + logger.error("Could not create PDF-UA conform document!"); + logger.debug(e.getMessage()); } } 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 6074b4c9..9b0f5ae1 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 @@ -52,7 +52,7 @@ public class TableDrawUtils { public static final String TABLE_DEBUG = "debug.table"; - private static StringBuilder alternateTableCaption = new StringBuilder(); + private static StringBuilder alternateTableCaption; public static void drawTable(PDPage page, PDPageContentStream contentStream, float x, float y, float width, @@ -61,6 +61,8 @@ public class TableDrawUtils { Map images, ISettings settings, IDGenerator generator, PDFAsVisualSignatureProperties properties) throws PdfAsException { + alternateTableCaption = new StringBuilder(); + logger.debug("Drawing Table: X {} Y {} WIDTH {} HEIGHT {} \n{}", x, y, width, height, abstractTable.getOrigTable().toString()); -- cgit v1.2.3 From b8a81cf495fe905ab076d15c0f71c0ea0f93a216 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Mon, 20 Apr 2015 16:21:03 +0200 Subject: document element needed to be updated --- .../at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 2 ++ 1 file changed, 2 insertions(+) (limited to 'pdf-as-pdfbox/src/main/java/at') 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 2e133284..9b173d66 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 @@ -554,6 +554,8 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { sigBlock.getCOSObject().setNeedToBeUpdate(true); treeRoot.getCOSObject().setNeedToBeUpdate(true); objectdic.getCOSObject().setNeedToBeUpdate(true); + docElement.getCOSObject().setNeedToBeUpdate(true); + } }catch(Exception e){ logger.error("Could not create PDF-UA conform document!"); -- cgit v1.2.3 From 541f9cbc07355d53b8eba9e1f3576d928b114bdb Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Tue, 21 Apr 2015 11:42:57 +0200 Subject: fixed automatic sig-placement for rotated pdfs --- .../at/knowcenter/wag/egov/egiz/pdf/PDFPage.java | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'pdf-as-pdfbox/src/main/java/at') 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 5a3711a4..fab63e2f 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 @@ -320,15 +320,13 @@ public class PDFPage extends PDFTextStripper { current_y = text.getY(); } if (pageRotation == 90) { - current_y = text.getX(); + current_y = text.getY(); } if (pageRotation == 180) { - float page_height = this.getCurrentPage().findMediaBox().getHeight(); - current_y = page_height - text.getY(); + current_y = text.getY(); } if (pageRotation == 270) { - float page_height = this.getCurrentPage().findMediaBox().getHeight(); - current_y = page_height - text.getX(); + current_y = text.getY(); } if (current_y > this.effectivePageHeight) { @@ -454,10 +452,8 @@ public class PDFPage extends PDFTextStripper { if (pageRotation == 180) { float min_y = findMinY(transformed_coordinates); logger.debug("min_y = " + min_y); - float page_height = this.mypage.getCurrentPage().findMediaBox().getHeight(); - actual_lowest_point = page_height - - findMaxY(transformed_coordinates); - actual_starting_point = page_height - 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); @@ -581,14 +577,14 @@ public class PDFPage extends PDFTextStripper { current_y = page_height - anno.getRectangle().getLowerLeftY(); } if (pageRotation == 90) { - current_y = anno.getRectangle().getLowerLeftX(); + current_y = anno.getRectangle().getUpperRightX(); } if (pageRotation == 180) { current_y = anno.getRectangle().getUpperRightY(); } if (pageRotation == 270) { - float page_height = page.findMediaBox().getHeight(); - current_y = page_height - anno.getRectangle().getUpperRightX(); + float page_width = page.findMediaBox().getWidth(); + current_y = page_width - anno.getRectangle().getLowerLeftX() ; } if (current_y > this.effectivePageHeight) { -- cgit v1.2.3 From 78f2fe168d7258af1e40509105ba4ed495606a4e Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Tue, 21 Apr 2015 14:17:56 +0200 Subject: automatic positioning set max_char_pos to effective page height if the last last character on page already reaches the footer, so that a new page is created for the signature block --- .../src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFPage.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'pdf-as-pdfbox/src/main/java/at') 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 fab63e2f..a7aaf2df 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 @@ -329,10 +329,8 @@ public class PDFPage extends PDFTextStripper { current_y = text.getY(); } - if (current_y > this.effectivePageHeight) { - // logger_.debug("character is below footer_line. footer_line = " + - // this.footer_line + ", text.character=" + character + ", y=" + - // current_y); + if (current_y > this.effectivePageHeight) { + this.max_character_ypos=this.effectivePageHeight; return; } -- cgit v1.2.3 From e41acb6dcca5e05b8a47421e4f1efb13d60e1448 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Wed, 22 Apr 2015 10:14:09 +0200 Subject: add the Tabs element to page if new page is added --- .../at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'pdf-as-pdfbox/src/main/java/at') 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 9b173d66..4538a052 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 @@ -549,6 +549,11 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { //setStructureParent PDAnnotationWidget widg=signatureField.getWidget(); widg.setStructParent(treeindex); + + //add the Tabs /S Element for Tabbing through annots + PDPage p = signatureField.getWidget().getPage(); + p.getCOSDictionary().setName("Tabs", "S"); + p.getCOSObject().setNeedToBeUpdate(true); ntndic.setNeedToBeUpdate(true); sigBlock.getCOSObject().setNeedToBeUpdate(true); -- cgit v1.2.3 From 1764433c3ac6a734000df912dd6122b292ec647e Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Wed, 22 Apr 2015 11:47:14 +0200 Subject: debug output --- .../at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'pdf-as-pdfbox/src/main/java/at') 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 4538a052..fcea384a 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 @@ -478,6 +478,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { // PDF-UA if (signatureProfileSettings.isPDFUA()==true) { + logger.info("Adding pdf/ua content."); try{ PDDocumentCatalog root = doc.getDocumentCatalog(); PDStructureTreeRoot treeRoot = root.getStructureTreeRoot(); @@ -564,7 +565,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } }catch(Exception e){ logger.error("Could not create PDF-UA conform document!"); - logger.debug(e.getMessage()); + logger.error(e.getMessage()); } } -- cgit v1.2.3 From 38b9dbc224d7be54dba23d04529df07f6fd0db51 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Thu, 23 Apr 2015 09:50:54 +0200 Subject: error handling --- .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 153 +++++++++++---------- 1 file changed, 77 insertions(+), 76 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/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index fcea384a..250d1242 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 @@ -477,95 +477,96 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } // PDF-UA - if (signatureProfileSettings.isPDFUA()==true) { - logger.info("Adding pdf/ua content."); - try{ - PDDocumentCatalog root = doc.getDocumentCatalog(); - PDStructureTreeRoot treeRoot = root.getStructureTreeRoot(); + logger.info("Adding pdf/ua content."); + try{ + PDDocumentCatalog root = doc.getDocumentCatalog(); + PDStructureTreeRoot treeRoot = root.getStructureTreeRoot(); - if (treeRoot != null) { //check if structure tree available in pdf document - logger.info("Tree Root: {}", treeRoot.toString()); - List kids = treeRoot.getKids(); + logger.info("Tree Root: {}", treeRoot.toString()); + List kids = treeRoot.getKids(); - if(kids==null){ - logger.info("No kid-elements in structure tree Root, maybe not PDF/UA document"); - } + if(kids==null){ + logger.info("No kid-elements in structure tree Root, maybe not PDF/UA document"); + } - PDStructureElement docElement = null; - for(Object k: kids){ - if(k instanceof PDStructureElement){ - docElement=(PDStructureElement) k; - break; - // if(((PDStructureElement) k).getStructureType().equals("Document")){ - // docElement=(PDStructureElement) k; - // } - } - } + PDStructureElement docElement = null; + for(Object k: kids){ + if(k instanceof PDStructureElement){ + docElement=(PDStructureElement) k; + break; + // if(((PDStructureElement) k).getStructureType().equals("Document")){ + // docElement=(PDStructureElement) k; + // } + } + } - PDStructureElement sigBlock = new PDStructureElement( - "Form", docElement); + PDStructureElement sigBlock = new PDStructureElement( + "Form", docElement); - //create object dictionary and add as child element - COSDictionary objectdic= new COSDictionary(); - objectdic.setName("Type", "OBJR"); - objectdic.setItem("Pg", signatureField.getWidget().getPage()); - objectdic.setItem("Obj", signatureField.getWidget()); + //create object dictionary and add as child element + COSDictionary objectdic= new COSDictionary(); + objectdic.setName("Type", "OBJR"); + objectdic.setItem("Pg", signatureField.getWidget().getPage()); + objectdic.setItem("Obj", signatureField.getWidget()); - List l = new ArrayList(); - l.add(objectdic); - sigBlock.setKids(l); + List l = new ArrayList(); + l.add(objectdic); + sigBlock.setKids(l); - sigBlock.setTitle("Signature Table"); - sigBlock.setParent(docElement); - docElement.appendKid(sigBlock); + sigBlock.setTitle("Signature Table"); + sigBlock.setParent(docElement); + docElement.appendKid(sigBlock); - //Create and add Attribute dictionary to mitigate PAC warning - COSDictionary sigBlockDic = (COSDictionary) sigBlock.getCOSObject(); - COSDictionary sub = new COSDictionary(); + //Create and add Attribute dictionary to mitigate PAC warning + COSDictionary sigBlockDic = (COSDictionary) sigBlock.getCOSObject(); + COSDictionary sub = new COSDictionary(); - sub.setName("O", "Layout"); - sub.setName("Placement", "Block"); - sigBlockDic.setItem(COSName.A, sub); - sigBlockDic.setNeedToBeUpdate(true); + sub.setName("O", "Layout"); + sub.setName("Placement", "Block"); + sigBlockDic.setItem(COSName.A, sub); + sigBlockDic.setNeedToBeUpdate(true); - //Modify number tree - PDNumberTreeNode ntn = treeRoot.getParentTree(); - if(ntn==null){ - ntn = new PDNumberTreeNode(objectdic, null); - logger.info("No number-tree-node found!"); - } + //Modify number tree + PDNumberTreeNode ntn = treeRoot.getParentTree(); + if(ntn==null){ + ntn = new PDNumberTreeNode(objectdic, null); + logger.info("No number-tree-node found!"); + } - COSDictionary ntndic=ntn.getCOSDictionary(); - COSArray ntndicnumbersarray = (COSArray)ntndic.getDictionaryObject(COSName.NUMS); - int arrindex = ntndicnumbersarray.size(); - int treeindex = arrindex/2; - - ntndicnumbersarray.add(arrindex, COSInteger.get(treeindex)); - ntndicnumbersarray.add(arrindex+1, sigBlock.getCOSObject()); - - treeRoot.setParentTree(ntn); - treeRoot.setParentTreeNextKey(treeindex+1); - - //setStructureParent - PDAnnotationWidget widg=signatureField.getWidget(); - widg.setStructParent(treeindex); - - //add the Tabs /S Element for Tabbing through annots - PDPage p = signatureField.getWidget().getPage(); - p.getCOSDictionary().setName("Tabs", "S"); - p.getCOSObject().setNeedToBeUpdate(true); - - ntndic.setNeedToBeUpdate(true); - sigBlock.getCOSObject().setNeedToBeUpdate(true); - treeRoot.getCOSObject().setNeedToBeUpdate(true); - objectdic.getCOSObject().setNeedToBeUpdate(true); - docElement.getCOSObject().setNeedToBeUpdate(true); - - } - }catch(Exception e){ + COSDictionary ntndic=ntn.getCOSDictionary(); + COSArray ntndicnumbersarray = (COSArray)ntndic.getDictionaryObject(COSName.NUMS); + int arrindex = ntndicnumbersarray.size(); + int treeindex = arrindex/2; + + ntndicnumbersarray.add(arrindex, COSInteger.get(treeindex)); + ntndicnumbersarray.add(arrindex+1, sigBlock.getCOSObject()); + + treeRoot.setParentTree(ntn); + treeRoot.setParentTreeNextKey(treeindex+1); + + //setStructureParent + PDAnnotationWidget widg=signatureField.getWidget(); + widg.setStructParent(treeindex); + + //add the Tabs /S Element for Tabbing through annots + PDPage p = signatureField.getWidget().getPage(); + p.getCOSDictionary().setName("Tabs", "S"); + p.getCOSObject().setNeedToBeUpdate(true); + + ntndic.setNeedToBeUpdate(true); + sigBlock.getCOSObject().setNeedToBeUpdate(true); + treeRoot.getCOSObject().setNeedToBeUpdate(true); + objectdic.getCOSObject().setNeedToBeUpdate(true); + docElement.getCOSObject().setNeedToBeUpdate(true); + + + }catch(Throwable e){ + if (signatureProfileSettings.isPDFUA()==true){ logger.error("Could not create PDF-UA conform document!"); - logger.error(e.getMessage()); + throw new PdfAsException("error.pdf.sig.pdfua.1", e); + }else{ + logger.info("Could not create PDF-UA conform signature"); } } -- cgit v1.2.3