From 980eed2c3ffe3784747abd5dc6a6fdebbbe2caff Mon Sep 17 00:00:00 2001 From: Alexander Marsalek Date: Thu, 22 Jul 2021 13:21:06 +0200 Subject: added getNextUnusedSignaturePlaceHolder => pdf over needs next unused placeholder issue #54 --- .../SignatureFieldsAndPlaceHolderExtractor.java | 41 +++++++++++++++++++--- .../placeholder/SignaturePlaceholderExtractor.java | 5 --- .../impl/signing/pdfbox2/PADESPDFBOXSigner.java | 2 +- 3 files changed, 37 insertions(+), 11 deletions(-) (limited to 'pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl') diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignatureFieldsAndPlaceHolderExtractor.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignatureFieldsAndPlaceHolderExtractor.java index ca620aec..63552c2a 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignatureFieldsAndPlaceHolderExtractor.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignatureFieldsAndPlaceHolderExtractor.java @@ -8,6 +8,7 @@ import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; import org.apache.pdfbox.pdmodel.interactive.form.PDField; import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -35,12 +36,27 @@ public class SignatureFieldsAndPlaceHolderExtractor { /* Needed by PDF-OVER */ - public static SignaturePlaceholderData getNextSignaturePlaceHolder(PDDocument doc) { + + /** + * Returns the next unused signature placeholder + * @param doc The document to be searched for signature placeholders + * @return The next unused signature placeholder or null in case there is none + */ + public static SignaturePlaceholderData getNextUnusedSignaturePlaceHolder(PDDocument doc) { try { - SignaturePlaceholderExtractor signaturePlaceholderExtractor = new SignaturePlaceholderExtractor("1", - PlaceholderExtractorConstants.PLACEHOLDER_MATCH_MODE_SORTED, doc); - return signaturePlaceholderExtractor.extract(doc, "1", - PlaceholderExtractorConstants.PLACEHOLDER_MATCH_MODE_SORTED); + String placeholderId = "1"; + int mode = PlaceholderExtractorConstants.PLACEHOLDER_MATCH_MODE_SORTED; + SignaturePlaceholderExtractor signaturePlaceholderExtractor = new SignaturePlaceholderExtractor( placeholderId, + mode, doc); + List results = signaturePlaceholderExtractor.extractList(doc, placeholderId, + mode); + List used = getExistingSignatureLocations(doc); + //return first not used + for(SignaturePlaceholderData result : results) { + if(!used.contains(result.getPlaceholderName())) + return result; + } + return null; } catch (Exception e) { e.printStackTrace(); return null; @@ -69,4 +85,19 @@ public class SignatureFieldsAndPlaceHolderExtractor { return null; } } + + public static List getExistingSignatureLocations(PDDocument doc) { + List existingLocations = new ArrayList<>(); + try { + List pdSignatureList = doc.getSignatureDictionaries(); + if(pdSignatureList.size() != 0) { + for(PDSignature sig : pdSignatureList) { + existingLocations.add(sig.getLocation()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return existingLocations; + } } diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignaturePlaceholderExtractor.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignaturePlaceholderExtractor.java index 71eef856..6df4f0b1 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignaturePlaceholderExtractor.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/pdfbox2/placeholder/SignaturePlaceholderExtractor.java @@ -126,11 +126,6 @@ public class SignaturePlaceholderExtractor extends PDFStreamEngine implements Pl this.doc = doc; } -// public static List getPlaceholders() { -// return placeholders; -// } - - /** * Search the document for placeholder images and possibly included * additional info.
diff --git a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java index 3cf0853f..3e1b7d9a 100644 --- a/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java +++ b/pdf-as-pdfbox-2/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox2/PADESPDFBOXSigner.java @@ -162,7 +162,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { signature.setFilter(COSName.getPDFName(signer.getPDFFilter())); signature.setSubFilter(COSName.getPDFName(signer.getPDFSubFilter())); // SignaturePlaceholderData signaturePlaceholderDataInit = - placeholders =PlaceholderFilter.checkPlaceholderSignatureLocationList(pdfObject.getStatus(), + placeholders = PlaceholderFilter.checkPlaceholderSignatureLocationList(pdfObject.getStatus(), pdfObject.getStatus().getSettings(), placeholder_id); // placeholders = SignaturePlaceholderExtractor.getPlaceholders(); -- cgit v1.2.3