3
votes

I have a original PDF document and separate file as PKCS#7 signed data. I would like to combine these to create signed PDF document so that it could be shown in for example Adobe reader that it is signed and who signed the document.

Are there any libraries in Java that could be used to create such signed PDF document programatically? I need to load that PDF and add the signature to it somehow, then save it as a new PDF document.

Sample code would be appreciated.

EDIT:

The resulting code should take the PDF document as input parameter and do the following:

  1. prepare data to be signed
  2. provide data to be signed to external signing mechanism that produces PKCS#7 signed data file
  3. provide PKCS#7 signed data file
  4. add signed data to PDF document
  5. save PDF document

As I understand the first step is important because without that the PKCS#7 signed data would be wrong.

2
Try using Apache PDFBox. Also, take a look at:<br> stackoverflow.com/questions/12426064/…BK Batchelor
Any decent general purpose PDF library should nowadays offer signing.mkl

2 Answers

5
votes

If I correctly understand your question, your pkcs#7 is signing the whole content of the PDF. That is, all PDF file bytes. In this case the answer is you can't.

When you sign a PDF file you don't sign all its bytes "as is". Before signing a PDF you have to create two "byte ranges" in the PDF, leaving in the midle of the PDF enough room to include the signature. Then you have to concatenate these two byte ranges, sign these bytes and include the signature in the space you left inside the PDF.

Take a look at
* https://www.adobe.com/devnet-docs/acrobatetk/tools/DigSig/Acrobat_DigitalSignatures_in_PDF.pdf (www.adobe.com)

0
votes

Try Apache PDFBox

Also, check out the following posts it goes over signing PDFs.

How to sign pdf in Java using pdfbox