How can I get PDVisibleSigProperties to write the

2019-06-21 04:17发布

问题:

I am working with the pdfbox example signature CreateVisableSignature and I would like the code to write the image of the signature into a signature field called "ApplicantSignature" on the third page.

Can someone give a clue as to why it writes the signature in the upper left corner of the first page?

Here is the code:

    public static void main(String[] args) throws KeyStoreException,
        NoSuchAlgorithmException, CertificateException,
        FileNotFoundException, IOException, COSVisitorException,
        SignatureException {

    if (args.length != 4) {
        usage();
        System.exit(1);
    } else {
        File ksFile = new File(args[0]);
        KeyStore keystore = KeyStore.getInstance("PKCS12", provider);
        char[] pin = args[1].toCharArray();
        keystore.load(new FileInputStream(ksFile), pin);
        File document = new File(args[2]);
        CreateVisibleSignature signing = new CreateVisibleSignature(
                keystore, pin.clone());
        String jpgFile = CreateVisibleSignature.convertPngToJpeg( args[3] );
        FileInputStream image = new FileInputStream( jpgFile );
        PDVisibleSignDesigner visibleSig = new PDVisibleSignDesigner(
                args[2], image, 1);
        visibleSig.xAxis(0).yAxis(0).zoom(-75)
                .signatureFieldName("ApplicantSignature");
        PDVisibleSigProperties signatureProperties = new PDVisibleSigProperties();
        signatureProperties.signerName("name").signerLocation("location")
                .signatureReason("Security").preferredSize(0).page(3)
                .visualSignEnabled(true).setPdVisibleSignature(visibleSig)
                .buildSignature();
        signing.signPDF(document, signatureProperties);
    }
}

I have also tried:

    PDVisibleSignDesigner visibleSig = new PDVisibleSignDesigner(
                args[2], image, 3);
        visibleSig.xAxis(0).yAxis(0).zoom(-75)
                .signatureFieldName("ApplicantSignature");
        PDVisibleSigProperties signatureProperties = new PDVisibleSigProperties();
        signatureProperties.signerName("name").signerLocation("location")
                .signatureReason("Security").preferredSize(0).page(1)
                .visualSignEnabled(true).setPdVisibleSignature(visibleSig)
                .buildSignature();

And I have tried:

    PDVisibleSignDesigner visibleSig = new PDVisibleSignDesigner(
                args[2], image, 3);
        visibleSig.xAxis(0).yAxis(0).zoom(-75)
                .signatureFieldName("ApplicantSignature");
        PDVisibleSigProperties signatureProperties = new PDVisibleSigProperties();
        signatureProperties.signerName("name").signerLocation("location")
                .signatureReason("Security").preferredSize(0).page(3)
                .visualSignEnabled(true).setPdVisibleSignature(visibleSig)
                .buildSignature();

This is where I want the signature to go on the third page.

This is where it is going on the first page.

These are the field names in the form.

回答1:

Based on the page settings given to me by the folks that responded to my question I am posting an answer base on my current knowledge of how signatures work in pdfbox.

First of all, the pre-existing form that I am working with, at least from a pdfbox perspective, seems to only have one page it in. When I use adobe DC pro to show the coordinates, it only shows the coordinates up to about the middle of the second page. So even though I set the X,Y coordinates to 100, 715, the signature image appears on the bottom of the first page, not on the 3rd.

Please correct me if I am wrong, but I believe that pdfbox only allows you sign the entire document and not individual signature fields that already exist in the document. Pre-existing signature fields are not affected by pdfbox as pdfbox appears not to be able to reference them.

If you bring up the pdf on adobe dc pro and click on the Signature Panel, you can see that the pdf was signed by pdfbox, but the pre-existing signature fields still indicate that they need to be signed.

I am guessing at this point that pdfbox adds the signatures that it signs to the pre-existing document.

I also noticed that the pdfbox signatures are invisible, even though the "who", "location", "reason", and date have been set, and that the image that you place in the the signature that pdfbox signs is the only thing that is actually visible in the resulting output pdf. If there is no image, then hard copy print outs don't show that the document is signed.

Again, please feel free to correct me if I am wrong.



回答2:

See this line in CreateVisibleSignature.java, in signPDF() (not included in your question, but part of the example code in PDFBox you mention):

// options.setPage(signatureProperties.getPage());

remove the "//" and the signature appears on page 3.

Re position, change this part of your code

xAxis(0).yAxis(0)

to other coordinates, e.g. these:

xAxis(100).yAxis(715)

Now about the question which code in the question is correct - the last one is:

PDVisibleSignDesigner visibleSig = new PDVisibleSignDesigner(args[2], image, 3);
visibleSig.xAxis(0).yAxis(0).zoom(-75).signatureFieldName("ApplicantSignature");
PDVisibleSigProperties signatureProperties = new PDVisibleSigProperties();
signatureProperties.signerName("name").signerLocation("location")
    .signatureReason("Security").preferredSize(0).page(3)
    .visualSignEnabled(true).setPdVisibleSignature(visibleSig)
    .buildSignature();

The 3 in PDVisibleSignDesigner is to choose the size of the page. The second 3 is to store the number of the page for later. The page numbers are 1-based here.