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 2016/11/02 18:52:11 UTC

svn commit: r1767761 - /pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java

Author: tilman
Date: Wed Nov  2 18:52:11 2016
New Revision: 1767761

URL: http://svn.apache.org/viewvc?rev=1767761&view=rev
Log:
PDFBOX-3547: improve/simplify signing of existing signature fields, as suggested by Lonzak

Modified:
    pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java

Modified: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java?rev=1767761&r1=1767760&r2=1767761&view=diff
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java (original)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java Wed Nov  2 18:52:11 2016
@@ -32,8 +32,6 @@ import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.io.IOUtils;
 
 import org.apache.pdfbox.pdmodel.PDDocument;
-import org.apache.pdfbox.pdmodel.common.PDRectangle;
-import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.ExternalSigningSupport;
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface;
@@ -99,6 +97,20 @@ public class CreateVisibleSignature exte
      */
     public void signPDF(File inputFile, File signedFile, TSAClient tsaClient) throws IOException
     {
+        this.signPDF(inputFile, signedFile, tsaClient, null);
+    }
+
+    /**
+     * Sign pdf file and create new file that ends with "_signed.pdf".
+     *
+     * @param inputFile The source pdf document file.
+     * @param signedFile The file to be signed.
+     * @param tsaClient optional TSA client
+     * @param signatureFieldName optional name of an existing (unsigned) signature field
+     * @throws IOException
+     */
+    public void signPDF(File inputFile, File signedFile, TSAClient tsaClient, String signatureFieldName) throws IOException
+    {
         setTsaClient(tsaClient);
 
         if (inputFile == null || !inputFile.exists())
@@ -114,10 +126,8 @@ public class CreateVisibleSignature exte
 
         PDSignature signature;
 
-        // You will usually not need this:
-        // sign a PDF with an existing empty signature, as created by the 
-        // CreateEmptySignatureForm example. Delete this line if you want to insert a new signature.
-        signature = findExistingSignature(doc, "Signature1");
+        // sign a PDF with an existing empty signature, as created by the CreateEmptySignatureForm example. 
+        signature = findExistingSignature(doc, signatureFieldName);
 
         if (signature == null)
         {
@@ -209,16 +219,8 @@ public class CreateVisibleSignature exte
                 }
                 else
                 {
-                    //TODO add your error handling here:
-                    // it doesn't make sense to replace an existing signature
+                    throw new IllegalStateException("The signature field " + sigFieldName + " is already signed.");
                 }
-                // position according to existing field widget
-                PDAnnotationWidget widget = signatureField.getWidgets().get(0);
-                PDRectangle rect = widget.getRectangle();
-                // need to substract from height because this is done later too
-                // see in PDVisibleSigBuilder.createSignatureRectangle()
-                visibleSignDesigner.xAxis(rect.getLowerLeftX())
-                        .yAxis(-rect.getLowerLeftY() + visibleSignatureProperties.getPdVisibleSignature().getPageHeight() - visibleSignDesigner.getHeight());
             }
         }
         return signature;