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.
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:
- PdfAWriter writer = PdfAWriter.getInstance(document,
- new FileOutputStream("./target/pdfa3-example.pdf"),
XMP metadata is a PDF/A requirement. The XMP metadata is constructed automatically from the document info.
Output intent is a PDF/A requirement.
- ICC_Profile icc = ICC_Profile.getInstance(
- new FileInputStream(
- "./src/main/resources/com/itextpdf/sRGB Color Space Profile.icm"));
- "Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
All fonts should be embedded:
- Font bold10 = FontFactory.getFont(
- 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.
- PdfDictionary parameters = new PdfDictionary();
- parameters.put(PdfName.MODDATE, new PdfDate());
- PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(
- writer, "./src/main/resources/com/itextpdf/invoice.xml",
- "invoice.xml", null, "application/xml", parameters, 0);
- fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data"));
- writer.addFileAttachment("invoice.xml", fileSpec);
- PdfArray array = new PdfArray();
- 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.
The complete example can be downloaded:
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.