Am 11.12.2019 um 14:03 schrieb Wade Polk:
3 days in trying to learn PDFBox 1.8.10 source code examples.

I think I finally got it to run, CreateVisibleSignatures.java. The output
was the following indicating I need to pass in some arguments:

java org.apache.pdfbox.examples.signature.CreateVisibleSignature
<pkcs12-keystore-file> <pin> <input-pdf> <sign-image>

Questions
1. Do you folks have example keystore/pdf/sign-images (just so I can rule
out issues with my files)?
2. Where do the keystore/pdf/sign-images need to be put?
3. How does this class know which signature field to use for each
certificate based signature? My form has 8 signature lines, but there is no
PDSignatureField argument for this function so I don't see how this could
work.

Hi,

A keystore can be created with the java keytool (e.g.

keytool -genkeypair -storepass 123456 -storetype pkcs12 -alias test -validity 365 -v -keyalg RSA -keystore keystore.p12
)

The password here is 123456.

There is a "usage" that tells where to put them in the command line. Or insert this just below "main":

args = new String[]{"keystore.p12",
          "123456",
          "blah.pdf",
          "sig.jpg" // must be jpeg IIRC
      };


The 1.8 example does not support signing existing signature fields. Here's some code from 2.0. It may be needed to adjust it, e.g. put some casts after the results of "getCOSObject()".


    // Find an existing signature (assumed to be empty). You will usually not need this.     private PDSignature findExistingSignature(PDDocument doc, String sigFieldName)
    {
        PDSignature signature = null;
        PDSignatureField signatureField;
        PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
        if (acroForm != null)
        {
            signatureField = (PDSignatureField) acroForm.getField(sigFieldName);
            if (signatureField != null)
            {
                // retrieve signature dictionary
                signature = signatureField.getSignature();
                if (signature == null)
                {
                    signature = new PDSignature();
                    // after solving PDFBOX-3524
                    // signatureField.setValue(signature)
                    // until then:
signatureField.getCOSObject().setItem(COSName.V, signature);
                }
                else
                {
                    throw new IllegalStateException("The signature field " + sigFieldName + " is already signed.");
                }
            }
        }
        return signature;
    }

So instead of calling "new PDSignature()" use the result.

Note that 1.8 has some flaws (e.g. involving multiple signing) that were fixed in 2.0. So insisting on using 1.8.10 is not a good practice.

Tilman


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org

Reply via email to