14

I am trying to sign pdf using pdfbox libraries. I have stuck now and realy need a help.

This is my code:

private static void signPdf(PDDocument document) throws Exception 
{
    PDSignature sig = new PDSignature();
    sig.setFilter(COSName.ADOBE_PPKLITE);
    sig.setSubFilter(COSName.ADBE_PKCS7_DETACHED);
    sig.setByteRange(new int[] {'a','a','a','a'});
    sig.setContents(new byte[]{(byte) 23, (byte) 23, (byte) 23, (byte) 23});

    SignatureOptions options = new SignatureOptions();

    document.addSignature(sig, new SignatureInterface() {
        public byte[] sign(InputStream content)
                throws SignatureException, IOException       {        
             //this should be made MD5 checksum?           
            return new byte[]{(byte) 'a', (byte) 'a', (byte) 'a', (byte) 'a'};
        }
    }, options);
}

Then Iam saving my pdf, but: 1) I have noticed that sign method is never called 2) Where should I attach certyficate? in sign method?

pdf:

/Type /Sig
/Filter /Adobe.PPKLite
/SubFilter /adbe.pkcs7.sha1
/Contents <0000000000. a lot of zeros..000>
/ByteRange [0 1000000000 1000000000 1000000000]

I think that i miss something, but documentation says nothing about how to sign a file.

Tahnks in advance JC.

@Ed

Here is how I save my pdf:

public static void saveFile(COSDocument doc, String out)
        throws IOException, COSVisitorException {  
    java.io.OutputStream os = null;  
    COSWriter writer = null;  
    try {
        os = new java.io.FileOutputStream(out);
        writer = new COSWriter(os);
        writer.write(doc);
    } finally {
        if (os != null) {
            os.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}
Dani
  • 3,744
  • 4
  • 27
  • 35
Jakub C
  • 595
  • 1
  • 5
  • 14
  • 3
    Seems like Signing with PDFBox is rather low-level and undocumented. You may want to read this white paper to understand more about digital signatures in PDF: http://itextpdf.com/book/digitalsignatures – Bruno Lowagie Sep 14 '12 at 15:27
  • How are you saving the document? That's where we need to look, to figure out why sign() isn't being called. – Ed Staub Sep 15 '12 at 01:03
  • @Ed I have added my saving function above – Jakub C Sep 17 '12 at 16:17

1 Answers1

11

The linked PDFBox-SignExample.zip is out of date. Please use this sample instead:

https://svn.apache.org/repos/asf/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java

It is better documented and kept up-to-date.

ThomasCh
  • 127
  • 1
  • 4
  • 18
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Ilija Dimov May 01 '15 at 08:50
  • Yes this would be better, but this question is at the moment under the top hits when searching "pdfbox signing" and there are many people that like to sign. The link point on the pdfbox svn trunk, so if the project don't move to git, the link will match, otherwise I will update it. So there is really no problem. – ThomasCh May 04 '15 at 14:17
  • Same file, but in syntax highlighted source viewer: https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java?view=markup – Морт Mar 17 '21 at 12:05