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 +- ...SignatureFieldsAndPlaceHolderExtractorTest.java | 32 +++++++++++++++++ 4 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java (limited to 'pdf-as-pdfbox-2') 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(); diff --git a/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java new file mode 100644 index 00000000..14fb962f --- /dev/null +++ b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java @@ -0,0 +1,32 @@ +package at.gv.egiz.pdfas.lib.testpdfbox; + +import at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignatureFieldsAndPlaceHolderExtractor; +import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; +import org.apache.pdfbox.pdmodel.PDDocument; + +import java.io.File; + +public class SignatureFieldsAndPlaceHolderExtractorTest { + + public static void main(String[] args) { + String dir = "/Users/amarsalek/Documents/pdf-as-4/pdf-as-pdfbox-2/src/test/resources/"; + + getNextSignaturePlaceHolder(dir + "new_qr_2-2.pdf"); + getNextSignaturePlaceHolder(dir + "new_qr_2_signed.pdf"); + getNextSignaturePlaceHolder(dir + "new_qr_2_signed_signed.pdf"); + getNextSignaturePlaceHolder(dir + "new_qr_2_signed_signed_signed.pdf"); + //TODO convert into junit test + } + + public static void getNextSignaturePlaceHolder(String filePath) { + try { + PDDocument doc = PDDocument.load(new File(filePath)); + SignaturePlaceholderData result = + SignatureFieldsAndPlaceHolderExtractor.getNextUnusedSignaturePlaceHolder(doc); + System.out.println(filePath + ": " + result); + } catch (Throwable e) { + e.printStackTrace(); + } + } + +} -- cgit v1.2.3