Jose Viota
Jose Viota

Reputation: 11

iText error while check pdf PDF/A-1A with java

I am writting in reference to an error I get while a signing a concrete pdf file with Java iText libraries version 5.5.3.

Adobe Pro verifies the conformance and tell me that conformance has been verified but iText tell me that "Annotation of type /Widget should have Contents key".

You can get the concrete file at http://www.boe.es/boe/dias/2014/08/06/pdfs/BOE-A-2014-8500.pdf

I post below the trace I get:

Caused by: com.itextpdf.text.pdf.PdfAConformanceException: Annotation of type /Widget should have Contents key.
at com.itextpdf.text.pdf.internal.PdfA1Checker.checkAnnotation(PdfA1Checker.java:462)
at com.itextpdf.text.pdf.internal.PdfAChecker.checkPdfAConformance(PdfAChecker.java:219)
at com.itextpdf.text.pdf.internal.PdfAConformanceImp.checkPdfIsoConformance(PdfAConformanceImp.java:71)
at com.itextpdf.text.pdf.PdfWriter.checkPdfIsoConformance(PdfWriter.java:3426)
at com.itextpdf.text.pdf.PdfWriter.checkPdfIsoConformance(PdfWriter.java:3422)
at com.itextpdf.text.pdf.PdfAnnotation.toPdf(PdfAnnotation.java:999)
at com.itextpdf.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:158)
at com.itextpdf.text.pdf.PdfWriter$PdfBody.write(PdfWriter.java:420)
at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:398)
at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:373)
at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:369)
at com.itextpdf.text.pdf.PdfWriter.addToBody(PdfWriter.java:843)
at com.itextpdf.text.pdf.PdfStamperImp.addAnnotation(PdfStamperImp.java:1389)
at com.itextpdf.text.pdf.PdfStamperImp.addAnnotation(PdfStamperImp.java:1401)
at com.itextpdf.text.pdf.PdfSignatureAppearance.preClose(PdfSignatureAppearance.java:1283)

Thanks in advance for any reply.

Upvotes: 1

Views: 2042

Answers (1)

Alexander Chingarev
Alexander Chingarev

Reputation: 394

I have tried applied sample from here (http://itextpdf.com/book/digitalsignatures20130304.pdf, page 29) to your PDF document and everything seems to work fine. The only exceptions in code are:

  1. Create PdfAStamper instead of PdfStamper:

    PdfAStamper stamper = PdfAStamper.createSignature(reader, os, '\0', PdfAConformanceLevel.PDF_A_1B);
    
  2. Set font to PdfSignatureAppearance so that font can be embedded:

    appearance.setLayer2Font(FontFactory.getFont("FreeMonoBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 12));
    
  3. Set output intents:

    ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream("sRGB Color Space Profile.icm"));
    stamper.getWriter().setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
    

Here's a complete code:

static public void sign(String src, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, MakeSignature.CryptoStandard subfilter, String reason, String location) throws GeneralSecurityException, IOException, DocumentException {
    // Creating the reader and the stamper
    PdfReader reader = new PdfReader(src);
    FileOutputStream os = new FileOutputStream(dest);
    PdfAStamper stamper = PdfAStamper.createSignature(reader, os, '\0', PdfAConformanceLevel.PDF_A_1B);
    // Creating the appearance
    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
    appearance.setLayer2Font(FontFactory.getFont("FreeMonoBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 12));
    appearance.setReason(reason);
    appearance.setLocation(location);
    appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
    ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream("sRGB Color Space Profile.icm"));
    stamper.getWriter().setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
    // Creating the signature
    ExternalDigest digest = new BouncyCastleDigest();
    ExternalSignature signature = new PrivateKeySignature(pk, digestAlgorithm, provider);
    MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, subfilter);
}

But probably your use case is a little bit different. Maybe you set special options or something... Can you please provide the sample code which fails?

Upvotes: 2

Related Questions