DD.
DD.

Reputation: 21971

How do I digitally sign a PDF in Java using a PEM file?

I have exported my digital certificate to a password protected PFX file in windows.

How do I digitally sign a pdf with this?

Upvotes: 0

Views: 2341

Answers (3)

Zig158
Zig158

Reputation: 381

I am signing pdfs with the Aspose Pdf java sdk. These first two steps will likely be the same for any library.

If you don't already have a certificate generate an ssl private key with openssl. Obviously this will give warnings since it is not signed by a CA.

openssl req -x509 -newkey rsa:2048 -keyout testkey.pem -out testcert.pem -days 365

Next convert your key to a certificate in the pkcs12 format.

openssl pkcs12 -name test -export -in testcert.pem -inkey testkey.pem -out test.pfx

Lastly use the Aspose Pdf or some other library to sign the pdf

        PdfFileSignature pdfSignSingle = new PdfFileSignature();
        //Open the pdf
        pdfSignSingle.bindPdf(pdfFileInputStream);

        // Load the certificate using the com.aspose.pdf.PKCS1 object
        PKCS1 pkcs1 = new PKCS1(certificateFileInputStream, certificatePassword);
        pkcs1.setAuthority("Sample Person");
        pkcs1.setReason("I want to sign");
        pkcs1.setContactInfo("some contact info");
        pkcs1.setLocation("here");
        pkcs1.setShowProperties(false);

        // Apply the signature. (0,0) is in the lower left corner.
        pdfSignSingle.sign(1, true, new Rectangle(100, 100, 150, 50), pkcs1);
        // Apply the signature image
        pdfSignSingle.setSignatureAppearanceStream(imageFileInputStream);

        pdfSignSingle.save(pdfFileOutputStream);

Upvotes: 3

Our SecureBlackbox can be used to sign the PDF documents using certificates stored in files in various formats, in Windows CryptoAPI and on hardware devices via PKCS#11.

Upvotes: 0

Bobo
Bobo

Reputation: 1

iTextPdf is a good API for java, handling pdf files. There is also a good documentation with examples: http://itextpdf.com/book/digitalsignatures20130304.pdf

There is also an example for signing PDF documents

Upvotes: -2

Related Questions