iText PdfStamper increase size of a signed pdf

2019-03-01 04:19发布

问题:

Why PdfStamper increase the size of my signed pdfs every i use?

That's my code:

private static void test(String src, String pwd, String dest) throws Exception {
    byte[] pwdByte = pwd != null ? pwd.getBytes() : null;
    PdfReader r = null;
    PdfStamper stp = null;
    FileOutputStream fos = null;
    try {
        r = new PdfReader(src, pwdByte);
        fos = new FileOutputStream(dest);
        stp = new PdfStamper(r, fos, '\0', true);
    } finally {
        stp.close();
        fos.close();
        r.close();
    }
}

If i call test, the resulting pdf increases the size depending on the signed info:

  • My initial pdf has a LTV sign but i need to add a timestamp. I need to addLtv() some pdfs and later addLtvNoTs() because it's a different TSA. This makes calling twice PdfReader in mode append and every time i do it increases my pdf in 190kb aprox. That means increasing it 380kb. http://developers.itextpdf.com/question/how-enable-ltv-timestamp-signature

  • I've tested calling above test method with a pdf with a simple sing and every time i call test() on the resulting pdf it increase 3,5kb each time (i think due to small sign info).

Why pdfstamper increase my pdf size if it's signed even i don't add any change? How can i avoid it? 390kb it's a problem because i need to resign thousand of pdfs and store them.

I'm using itext 5.5.9

Thanks in advance!

回答1:

This is a strange question because it reveals that you don't understand the concept of a digital signature. When you sign a PDF, you want to make sure that the signature breaks as soon as someone changes a single byte. Every time you sign a document, you protect the bytes of the previous revision of the document by adding a signature dictionary of which the contents contain a signed hash of at least the bytes you want to protect.

See the following figure that shows a schema of a PDF that is signed three times:

Suppose that you would want to add a fourth signature, and you would want the resulting file to have the same size as before, you would have to change some of the bytes in the existing document. That would break at least one of the existing signatures.

You mention LTV. LTV implies adding a Document Security Store (DSS) and a Timestamp signature. The same rules apply: you have to add extra bytes, because the moment you change existing bytes, you'll break an existing signature:

Every time the certificate of the latest timestamp signature expires, you have to repeat the same procedure:

The total file size will always increase.



标签: java pdf itext