Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get PDVisibleSigProperties to write the signature on the 3 page into the signature box

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 I want the signature to go on the third page.

This is where it is going on the first page. This is where it is going on the first page.

These are the field names in the form. This shows the field names.

like image 498
DenisMP Avatar asked Oct 30 '22 23:10

DenisMP


1 Answers

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.

like image 126
Tilman Hausherr Avatar answered Nov 04 '22 14:11

Tilman Hausherr