So signieren Sie PDF in Java mit pdfbox

Ich versuche, PDF mit PDFbox-Bibliotheken zu signieren. Ich bin jetzt stecken geblieben und brauche dringend Hilfe.

Das ist mein 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);
}

Dann speichere ich mein PDF, aber: 1) Ich habe bemerkt, dass die Zeichenmethode niemals aufgerufen wird. 2) Wo soll ich das Zertifikat anhängen? in Vorzeichenmethode?

pdf:

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

Ich denke, dass ich etwas vermisse, aber die Dokumentation sagt nichts darüber aus, wie man eine Datei signiert.

Tahnks im Voraus JC.

@Ed

So speichere ich mein 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();
        }
    }
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage