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 2017/10/24 16:05:02 UTC

svn commit: r1813189 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Author: tilman
Date: Tue Oct 24 16:05:02 2017
New Revision: 1813189

URL: http://svn.apache.org/viewvc?rev=1813189&view=rev
Log:
PDFBOX-3978: if there's a signature with the update flag, check that one instead of the "last" one; clarify javadoc; fix typo

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1813189&r1=1813188&r2=1813189&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Tue Oct 24 16:05:02 2017
@@ -834,7 +834,8 @@ public class PDDocument implements Close
     }
 
     /**
-     * This will return the last signature.
+     * This will return the last signature from the field tree. Note that this may not be the
+     * last in time when empty signature fields are created first but signed after other fields.
      * 
      * @return the last signature as <code>PDSignatureField</code>.
      * @throws IOException if no document catalog can be found.
@@ -1348,7 +1349,7 @@ public class PDDocument implements Close
      * @return instance to be used for external signing and setting CMS signature
      * @throws IOException if the output could not be written
      * @throws IllegalStateException if the document was not loaded from a file or a stream or
-     * signature optionss were not set.
+     * signature options were not set.
      */
     public ExternalSigningSupport saveIncrementalForExternalSigning(OutputStream output) throws IOException
     {
@@ -1356,7 +1357,18 @@ public class PDDocument implements Close
         {
             throw new IllegalStateException("document was not loaded from a file or a stream");
         }
-        int[] byteRange = getLastSignatureDictionary().getByteRange();
+        // PDFBOX-3978: getLastSignatureDictionary() not helpful if signing into a template
+        // that is not the last signature. So give higher priority to signature with update flag.
+        PDSignature foundSignature = null;
+        for (PDSignature sig : getSignatureDictionaries())
+        {
+            foundSignature = sig;
+            if (sig.getCOSObject().isNeedToBeUpdated())
+            {
+                break;
+            }
+        }
+        int[] byteRange = foundSignature.getByteRange();
         if (!Arrays.equals(byteRange, RESERVE_BYTE_RANGE))
         {
             throw new IllegalStateException("signature reserve byte range has been changed "