lstachowiak
lstachowiak

Reputation: 344

Problem with adding long term validation to signed pdf using iText

I have a problem with adding long-term-validation to PDF signed using iText. Do not know why it generates broken pdf. When I try to add LTV to PDF signed by different lib (ex 'node-signpdf') then I got valid PDF.

Signing of pdf looks like this:

private static void applySignature(InputStream src,
            OutputStream dest,
            Certificate[] chain,
            PrivateKey pk,
            String digestAlgorithm,
            String provider,
            PdfSigner.CryptoStandard subfilter,
            String reason,
            String location)
            throws GeneralSecurityException, IOException {

        PdfReader reader = new PdfReader(src);
        PdfSigner signer = new PdfSigner(reader, dest, true);

        PdfSignatureAppearance appearance = signer.getSignatureAppearance()
                .setReason(reason)
                .setLocation(location)
                .setReuseAppearance(false);
        Rectangle rect = new Rectangle(0, 0, 0, 0);
        appearance.setPageRect(rect).setPageNumber(1);
        signer.setFieldName("Signature1");
        IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
        IExternalDigest digest = new BouncyCastleDigest();
        signer.signDetached(digest, pks, chain, null, null, null, 0, subfilter);
}    

Enabling LTV is basically taken from iText examples -> https://github.com/mkl-public/testarea-itext7/blob/master/src/main/java/mkl/testarea/itext7/signature/AdobeLtvEnabling.java

My code can be found here -> https://github.com/lstachowiak/pdf-sign-and-enable-ltv-example

Thanks in advance for any help!

Upvotes: 2

Views: 310

Answers (1)

mkl
mkl

Reputation: 95918

In your PdfExample.main you do

public static void main(String[] args) throws Exception {
    ...

    PdfReader pdfSigned = new PdfReader(new FileInputStream(OUT_SIGNED_PDF));
    PdfDocument pdf = new PdfDocument(pdfSigned, new PdfWriter(OUT_SIGNED_LTV_PDF), new StampingProperties().preserveEncryption().useAppendMode());

    AdobeLtvEnabling adobeLtvEnabling = new AdobeLtvEnabling(pdf);
    adobeLtvEnabling.enable();
}

Here you forget to call pdf.close() or alternatively use a try-with-resources construct:

try (
    PdfReader pdfSigned = new PdfReader(new FileInputStream(OUT_SIGNED_PDF));
    PdfDocument pdf = new PdfDocument(pdfSigned, new PdfWriter(OUT_SIGNED_LTV_PDF), new StampingProperties().preserveEncryption().useAppendMode());
) {
    AdobeLtvEnabling adobeLtvEnabling = new AdobeLtvEnabling(pdf);
    adobeLtvEnabling.enable();
}

Upvotes: 1

Related Questions