简体   繁体   中英

Aspose pdf java PdfFileSignature setAuthority not working

I'm trying to use Aspose pdf java to digitally sign a document. This is my code

public ByteArrayOutputStream signDocument(Document doc, String signedBy) throws Exception {

        PdfFileSignature pdfSignSingle = new PdfFileSignature();
        pdfSignSingle.bindPdf(doc);
        pdfSignSingle.setCertificate(prop.getSigningKeyStorePath(), prop.getKeystorePassword());
        PKCS7 signature = new PKCS7(prop.getSigningKeyStorePath(), prop.getKeystorePassword());
        pdfSignSingle.setSignatureAppearance(prop.getSimploudLogo());

        signature.setAuthority("Authority");
        signature.setDate(new Date());
        signature.setContactInfo("email");
        signature.setLocation("Location");
        signature.setReason("reason");
        pdfSignSingle.sign(1, true, new java.awt.Rectangle(100, 100, 200, 200), signature);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        pdfSignSingle.save(baos);
        pdfSignSingle.dispose();
        doc.dispose();
        return baos;
    }

In picture is shown how the signature looks in adobeReader.

图片

As you can see both image and Authority are not shown. I tried image to be both in pdf and png format. I've also tried to make it smaller then Rectangle area. As for authority i really need it to be customizable so that text in first line in picture can be Signed by "customParameter"

The API offers another Class ie SignatureCustomAppearance which can further be used to set such properties for a signature such as DateSigned, Reason, Location, etc. Please check the following complete code snippet which can fulfill your requirements:

String inputFile = "doc.pdf";
String outSignedFile = "out_20.9.pdf";
// Create PdfFileSignature instance
com.aspose.pdf.facades.PdfFileSignature pdfSignSingle = new com.aspose.pdf.facades.PdfFileSignature();
// Bind the source PDF by reading contents of Stream
pdfSignSingle.bindPdf(inputFile);

PKCS7 pkcs = new PKCS7("mykey2.pfx", "pass");
pkcs.setAuthority("Authority");
pkcs.setDate(new Date());
pkcs.setContactInfo("email");
pkcs.setLocation("Location");
pkcs.setReason("reason");
pkcs.setImage(new FileInputStream("simpleLogo.png"));

SignatureCustomAppearance sca = new SignatureCustomAppearance();
sca.setDateSignedAtLabel(null);
sca.setDigitalSignedLabel(null);
sca.setShowReason(true);
sca.setShowLocation(true);
sca.setShowContactInfo(true);

pkcs.setCustomAppearance(sca);

pdfSignSingle.sign(1, true, new java.awt.Rectangle(100, 100, 200, 200), pkcs);
// Set image for signature appearance
//pdfSignSingle.setSignatureAppearance("simpleLogo.png");
// Save final output
pdfSignSingle.save(outSignedFile);

As mentioned in the comment below the question, the same inquiry was posted in Aspose.PDF official forum as well at " Aspose pdf java PdfFileSignature setAuthority not working " and a solution has also been provided there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM