From de6b5593bd28cf91ecef3b9ec1123fc06de1e49f Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 8 Jul 2014 09:55:12 +0200 Subject: Fixed unique Signature field names, to make Adobe Reader happy --- .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 71 ++++++++++++---------- .../at/gv/egiz/pdfas/lib/util/SignatureUtils.java | 29 +++++++++ 2 files changed, 69 insertions(+), 31 deletions(-) create mode 100644 pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/util/SignatureUtils.java diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index 1e33a3cb..146e4993 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java @@ -75,6 +75,7 @@ import at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureProperties; import at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PdfBoxVisualObject; import at.gv.egiz.pdfas.lib.impl.status.PDFObject; import at.gv.egiz.pdfas.lib.impl.status.RequestedSignature; +import at.gv.egiz.pdfas.lib.util.SignatureUtils; import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction; import at.knowcenter.wag.egov.egiz.pdf.TablePos; import at.knowcenter.wag.egov.egiz.table.Table; @@ -172,15 +173,16 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { .getSignaturePosition(); TablePos signaturePos = null; - + String signaturePosString = signatureProfileConfiguration - .getDefaultPositioning(); + .getDefaultPositioning(); - if(signaturePosString != null) { - logger.debug("using signature Positioning: " + signaturePos); + if (signaturePosString != null) { + logger.debug("using signature Positioning: " + + signaturePos); signaturePos = new TablePos(signaturePosString); } - + logger.debug("using Positioning: " + posString); if (posString != null) { @@ -190,8 +192,8 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { // Fallback to signature Position! tablePos = signaturePos; } - - if(tablePos == null) { + + if (tablePos == null) { // Last Fallback default position tablePos = new TablePos(); } @@ -288,8 +290,9 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { if (signatureProfileSettings.isPDFA()) { PDDocumentCatalog root = doc.getDocumentCatalog(); - COSBase base = root.getCOSDictionary().getItem(COSName.OUTPUT_INTENTS); - if(base == null) { + COSBase base = root.getCOSDictionary().getItem( + COSName.OUTPUT_INTENTS); + if (base == null) { InputStream colorProfile = PDDocumentCatalog.class .getResourceAsStream("/icm/sRGB Color Space Profile.icm"); try { @@ -335,33 +338,39 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { String sigFieldName = signatureProfileSettings.getSignFieldValue(); - if (sigFieldName != null) { - PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); - if (acroForm != null) { - @SuppressWarnings("unchecked") - List fields = acroForm.getFields(); - PDSignatureField signatureField = null; - - if (fields != null) { - for (PDField pdField : fields) { - if (pdField instanceof PDSignatureField) { - if (((PDSignatureField) pdField).getSignature() - .getDictionary() - .equals(signature.getDictionary())) { - signatureField = (PDSignatureField) pdField; - } + if (sigFieldName == null) { + sigFieldName = "PDF-AS Signatur"; + } + + int count = SignatureUtils.countSignatures(doc); + + sigFieldName = sigFieldName + count; + + PDAcroForm acroFormm = doc.getDocumentCatalog().getAcroForm(); + if (acroFormm != null) { + @SuppressWarnings("unchecked") + List fields = acroFormm.getFields(); + PDSignatureField signatureField = null; + + if (fields != null) { + for (PDField pdField : fields) { + if (pdField instanceof PDSignatureField) { + if (((PDSignatureField) pdField).getSignature() + .getDictionary() + .equals(signature.getDictionary())) { + signatureField = (PDSignatureField) pdField; } } - } else { - logger.warn("Failed to name Signature Field! [Cannot find Field list in acroForm!]"); - } - - if (signatureField != null) { - signatureField.setPartialName(sigFieldName); } } else { - logger.warn("Failed to name Signature Field! [Cannot find acroForm!]"); + logger.warn("Failed to name Signature Field! [Cannot find Field list in acroForm!]"); + } + + if (signatureField != null) { + signatureField.setPartialName(sigFieldName); } + } else { + logger.warn("Failed to name Signature Field! [Cannot find acroForm!]"); } if (requestedSignature.isVisual()) { diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/util/SignatureUtils.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/util/SignatureUtils.java new file mode 100644 index 00000000..28713e1a --- /dev/null +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/util/SignatureUtils.java @@ -0,0 +1,29 @@ +package at.gv.egiz.pdfas.lib.util; + +import org.apache.pdfbox.cos.COSArray; +import org.apache.pdfbox.cos.COSDictionary; +import org.apache.pdfbox.cos.COSName; +import org.apache.pdfbox.pdmodel.PDDocument; + +public class SignatureUtils { + + public static int countSignatures(PDDocument doc) { + int count = 0; + COSDictionary trailer = doc.getDocument().getTrailer(); + COSDictionary root = (COSDictionary) trailer + .getDictionaryObject(COSName.ROOT); + COSDictionary acroForm = (COSDictionary) root + .getDictionaryObject(COSName.ACRO_FORM); + COSArray fields = (COSArray) acroForm + .getDictionaryObject(COSName.FIELDS); + for (int i = 0; i < fields.size(); i++) { + COSDictionary field = (COSDictionary) fields.getObject(i); + String type = field.getNameAsString("FT"); + if ("Sig".equals(type)) { + count++; + } + } + + return count; + } +} -- cgit v1.2.3