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”

@velicko91

Would you please share your sample input PDF document with us. We will test the scenario in our environment and address it accordingly.

This is my document, it does not work on empty document.
doc.pdf (7.0 KB)
This is my png simploudLogo.png (20.1 KB)

@velicko91

Would you kindly try to use following complete code snippet with Aspose.PDF for Java 20.9 and let us know in case you are still unable to achieve what you require. You can set further properties of signatures in SignatureCustomAppearance Class as shown below:

String inputFile = dataDir + "doc.pdf";
String outSignedFile = dataDir + "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(dataDir + "mykey2.pfx", "aa");
pkcs.setAuthority("Authority");
pkcs.setDate(new Date());
pkcs.setContactInfo("email");
pkcs.setLocation("Location");
pkcs.setReason("reason");
pkcs.setImage(new FileInputStream(dataDir + "simploudLogo.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(dataDir + "simploudLogo.png");
// Save final output
pdfSignSingle.save(outSignedFile);

Thank you, it works.

1 Like