You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/10/19 17:18:23 UTC
svn commit: r1844358 -
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Author: tilman
Date: Fri Oct 19 17:18:23 2018
New Revision: 1844358
URL: http://svn.apache.org/viewvc?rev=1844358&view=rev
Log:
PDFBOX-4343: Prevent calling addSignature twice, remove misleading method addSignatureField
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1844358&r1=1844357&r2=1844358&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Fri Oct 19 17:18:23 2018
@@ -147,7 +147,10 @@ public class PDDocument implements Close
// document-wide cached resources
private ResourceCache resourceCache = new DefaultResourceCache();
-
+
+ // to make sure only one signature is added
+ private boolean signatureAdded = false;
+
/**
* Creates an empty PDF document.
* You need to add at least one page for the document to be valid.
@@ -215,9 +218,14 @@ public class PDDocument implements Close
* Add parameters of signature to be created externally using default signature options. See
* {@link #saveIncrementalForExternalSigning(OutputStream)} method description on external
* signature creation scenario details.
+ * <p>
+ * Only one signature may be added at in a document. To sign several times,
+ * load document, add signature, save incremental and close again.
*
* @param sigObject is the PDSignatureField model
* @throws IOException if there is an error creating required fields
+ * @throws IllegalStateException if one attempts to add several signature
+ * fields.
*/
public void addSignature(PDSignature sigObject) throws IOException
{
@@ -228,10 +236,15 @@ public class PDDocument implements Close
* Add parameters of signature to be created externally. See
* {@link #saveIncrementalForExternalSigning(OutputStream)} method description on external
* signature creation scenario details.
+ * <p>
+ * Only one signature may be added at in a document. To sign several times,
+ * load document, add signature, save incremental and close again.
*
* @param sigObject is the PDSignatureField model
* @param options signature options
* @throws IOException if there is an error creating required fields
+ * @throws IllegalStateException if one attempts to add several signature
+ * fields.
*/
public void addSignature(PDSignature sigObject, SignatureOptions options) throws IOException
{
@@ -240,11 +253,16 @@ public class PDDocument implements Close
/**
* Add a signature to be created using the instance of given interface.
+ * <p>
+ * Only one signature may be added at in a document. To sign several times,
+ * load document, add signature, save incremental and close again.
*
* @param sigObject is the PDSignatureField model
* @param signatureInterface is an interface whose implementation provides
* signing capabilities. Can be null if external signing if used.
* @throws IOException if there is an error creating required fields
+ * @throws IllegalStateException if one attempts to add several signature
+ * fields.
*/
public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface) throws IOException
{
@@ -255,16 +273,27 @@ public class PDDocument implements Close
* This will add a signature to the document. If the 0-based page number in the options
* parameter is smaller than 0 or larger than max, the nearest valid page number will be used
* (i.e. 0 or max) and no exception will be thrown.
+ * <p>
+ * Only one signature may be added at in a document. To sign several times,
+ * load document, add signature, save incremental and close again.
*
* @param sigObject is the PDSignatureField model
* @param signatureInterface is an interface whose implementation provides
* signing capabilities. Can be null if external signing if used.
* @param options signature options
* @throws IOException if there is an error creating required fields
+ * @throws IllegalStateException if one attempts to add several signature
+ * fields.
*/
public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface,
SignatureOptions options) throws IOException
{
+ if (signatureAdded)
+ {
+ throw new IllegalStateException("Only one signature may be added at in a document");
+ }
+ signatureAdded = true;
+
// Reserve content
// We need to reserve some space for the signature. Some signatures including
// big certificate chain and we need enough space to store it.
@@ -568,67 +597,6 @@ public class PDDocument implements Close
}
/**
- * This will add a list of signature fields to the document.
- *
- * @param sigFields are the PDSignatureFields that should be added to the
- * document
- * @param signatureInterface is an interface whose implementation provides
- * signing capabilities. Can be null if external signing if used.
- * @param options signature options
- * @throws IOException if there is an error creating required fields
- */
- public void addSignatureField(List<PDSignatureField> sigFields, SignatureInterface signatureInterface,
- SignatureOptions options) throws IOException
- {
- PDDocumentCatalog catalog = getDocumentCatalog();
- catalog.getCOSObject().setNeedToBeUpdated(true);
-
- PDAcroForm acroForm = catalog.getAcroForm();
- if (acroForm == null)
- {
- acroForm = new PDAcroForm(this);
- catalog.setAcroForm(acroForm);
- }
- COSDictionary acroFormDict = acroForm.getCOSObject();
- acroFormDict.setDirect(true);
- acroFormDict.setNeedToBeUpdated(true);
- if (!acroForm.isSignaturesExist())
- {
- // 1 if at least one signature field is available
- acroForm.setSignaturesExist(true);
- }
-
- List<PDField> acroformFields = acroForm.getFields();
-
- for (PDSignatureField sigField : sigFields)
- {
- sigField.getCOSObject().setNeedToBeUpdated(true);
-
- // Check if the field already exists
- boolean checkSignatureField = checkSignatureField(acroForm.getFieldIterator(), sigField);
- if (checkSignatureField)
- {
- sigField.getCOSObject().setNeedToBeUpdated(true);
- }
- else
- {
- acroformFields.add(sigField);
- }
-
- // Check if we need to add a signature
- if (sigField.getSignature() != null)
- {
- sigField.getCOSObject().setNeedToBeUpdated(true);
- if (options == null)
- {
- // TODO ??
- }
- addSignature(sigField.getSignature(), signatureInterface, options);
- }
- }
- }
-
- /**
* Remove the page from the document.
*
* @param page The page to remove from the document.