Search code examples
javaitextpdfa

iText create PDF/A_1A with images


I am trying to create a pdf/a 1a document with iText 5.5.2

I can create a simple pdf/a with hello world but I am not able to add an image in the document without the error:

com.itextpdf.text.pdf.PdfAConformanceException: Alt entry should specify alternate description for /Figure element.

Below is my code tryout. I don't know how to add Alt entry Figure for the image. I tried with the PdfDictionary but it does not work. I am out of idea's. Does anyone have a tip for me?

final float MARGIN_OF_ONE_CM = 28.8f;
    final com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4
        , MARGIN_OF_ONE_CM
        , MARGIN_OF_ONE_CM
        , MARGIN_OF_ONE_CM
        , MARGIN_OF_ONE_CM);
    ByteArrayOutputStream pdfAsStream = new ByteArrayOutputStream();
    PdfAWriter writer = PdfAWriter.getInstance(document,
        new FileOutputStream("D:\\tmp\\pdf\\test.pdf"), PdfAConformanceLevel.PDF_A_1A);
    document.addAuthor("Author");
    document.addSubject("Subject");
    document.addLanguage("nl-nl");
    document.addCreationDate();
    document.addCreator("Creator");
    document.addTitle("title");

    writer.setPdfVersion(PdfName.VERSION);
    writer.setTagged();
    writer.createXmpMetadata();
    document.open();

    final String FONT = "./src/main/resources/fonts/arial.ttf";
    Font font = FontFactory.getFont(FONT, BaseFont.CP1252, BaseFont.EMBEDDED);

    final Paragraph element = new Paragraph("Hello World", font);
    document.add(element);

    final InputStream logo = this.getClass().getResourceAsStream("/logos/logo.jpg");
    final byte[] bytes = IOUtils.toByteArray(logo);
    Image logoImage = Image.getInstance(bytes);
    document.add(logoImage);

    final String colorProfile = "/color/sRGB Color Space Profile.icm";
    final InputStream resourceAsStream = this.getClass().getResourceAsStream(colorProfile);
    ICC_Profile icc = ICC_Profile.getInstance(resourceAsStream);
    writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);

    document.close();

Solution

  • Normally, you need to add the alternate description like this:

    logoImage.setAccessibleAttribute(PdfName.ALT, new PdfString("Logo"));
    

    This works for PDF/A2-A and PDF/A3-A, but not for PDF/A1-A. I have tested this and I see that you have discovered a bug. This bug has now been fixed. The fix will be in the next release.