diff options
author | Alexander Marsalek <amarsalek@iaik.tugraz.at> | 2021-07-22 13:21:06 +0200 |
---|---|---|
committer | Alexander Marsalek <amarsalek@iaik.tugraz.at> | 2021-07-22 16:20:35 +0200 |
commit | 980eed2c3ffe3784747abd5dc6a6fdebbbe2caff (patch) | |
tree | 785fa406ee89957e04234e6ebe3b8d88088d7bd4 | |
parent | e1f63b5fa88dfa2a5ca3fe91d8b065753ce63a68 (diff) | |
download | pdf-as-4-980eed2c3ffe3784747abd5dc6a6fdebbbe2caff.tar.gz pdf-as-4-980eed2c3ffe3784747abd5dc6a6fdebbbe2caff.tar.bz2 pdf-as-4-980eed2c3ffe3784747abd5dc6a6fdebbbe2caff.zip |
added getNextUnusedSignaturePlaceHolder => pdf over needs next unused placeholder issue #54
5 files changed, 81 insertions, 12 deletions
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/SignaturePlaceholderData.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/SignaturePlaceholderData.java index cf0ad149..1ad96657 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/SignaturePlaceholderData.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/placeholder/SignaturePlaceholderData.java @@ -142,9 +142,20 @@ public class SignaturePlaceholderData { this.key = key; } + @Override public String toString() { - return getClass().toString() + ": profile=" + profile + "; type=" + type + "; sigKey=" + key + "; table pos=" + tablePos; + return "SignaturePlaceholderData{" + + "profile='" + profile + '\'' + + ", type='" + type + '\'' + + ", key='" + key + '\'' + + ", id='" + id + '\'' + + ", tablePos=" + tablePos + + ", placeholderName='" + placeholderName + '\'' + + '}'; } +// public String toString() { +// return getClass().toString() + ": profile=" + profile + "; type=" + type + "; sigKey=" + key + "; table pos=" + tablePos; +// } public void setPlaceholderName(String name) { this.placeholderName = name; 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<SignaturePlaceholderData> results = signaturePlaceholderExtractor.extractList(doc, placeholderId, + mode); + List<String> 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<String> getExistingSignatureLocations(PDDocument doc) { + List<String> existingLocations = new ArrayList<>(); + try { + List <PDSignature> 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<SignaturePlaceholderData> getPlaceholders() { -// return placeholders; -// } - - /** * Search the document for placeholder images and possibly included * additional info.<br/> 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(); + } + } + +} |