PDF/A-3 with iText

Tags: PDF/A-3

In this short tutorial, we'll learn how to use iText to create PDF/A-3 compliant PDFs from scratch. This requires some low level knowledge of PDF and it is the developer's responsibility to make sure that all PDF/A-3 requirements are met and iText will generate exceptions when PDF/A requirements aren-t met, ensuring that the output is actually PDF/A-3 compliant.

We have written a simple example that shows how to create a PDF/A-3 document with the current version of iText. It takes data from an XML file and writes it to a PDF. The XML file is also embedded in the PDF (as an associated file). Below are some excerpts from the code that are important for PDF/A-3 compliance.

How to create a PDF/A-3 compliant PDF

First of all, we have to tell iText we want to write a PDF/A document. We do this by using PdfAWriter and setting the conformance level to PDF/A-3B:

  1. PdfAWriter writer = PdfAWriter.getInstance(document,
  2. new FileOutputStream("./target/pdfa3-example.pdf"),
  3. PdfAConformanceLevel.PDF_A_3B);

XMP metadata is a PDF/A requirement. The XMP metadata is constructed automatically from the document info.

  1. writer.createXmpMetadata();

Output intent is a PDF/A requirement.

  1. ICC_Profile icc = ICC_Profile.getInstance(
  2. new FileInputStream(
  3. "./src/main/resources/com/itextpdf/sRGB Color Space Profile.icm"));
  4. writer.setOutputIntents(
  5. "Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);

All fonts should be embedded:

  1. Font bold10 = FontFactory.getFont(
  2. "./src/main/resources/com/itextpdf/FreeSansBold.ttf",
  3. BaseFont.WINANSI, BaseFont.EMBEDDED, 10);

PDF/A-3 defines some extra requirements on embedded files. We have to store the necessary information, add the file to the PDF and add the associated file entry ("AF") to the Catalog. In this case we are adding an associated file for the document: we add the xml document that contains the data. It is also possible to add an associated file for a page.

  1. PdfDictionary parameters = new PdfDictionary();
  2. parameters.put(PdfName.MODDATE, new PdfDate());
  3. PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(
  4. writer, "./src/main/resources/com/itextpdf/invoice.xml",
  5. "invoice.xml", null, "application/xml", parameters, 0);
  6. fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data"));
  7. writer.addFileAttachment("invoice.xml", fileSpec);
  8. PdfArray array = new PdfArray();
  9. array.add(fileSpec.getReference());
  10. writer.getExtraCatalog().put(new PdfName("AF"), array);

From here on we can add content to the PDF just like we would do for a regular PDF.

Note: Please use a PDF/A-3 validator on the produced documents to verify that they are PDF/A-3 compliant.

Download full example

The complete example can be downloaded:

Further info

Please contact our Sales department for information regarding licenses and pricing. iText customers with a support contract can contact support to get more info about the creation of PDF/A documents.