iText in Action - book cover

iText in Action 2nd Edition

The PdfXPdfA example is part of the book iText in Action (ISBN 9781935182610).
It's a small standalone application. You can use this example for inspiration, but please buy the book if there's something you don't understand about the example. Read Chapter 13 for more info.

Chapter 13: PDFs inside-out

If you want this example to work, you need the following jars: iText.jar
Make sure you have the following fonts in directory c:/windows/fonts/: Arial (arial.ttf).
This example uses the following resources: sRGB.profile.

part4.chapter13.PdfXPdfA

If you compile and execute this example, you'll get the following result:
You can download the full source code of PdfXPdfA, or read it here:
 
Powered by GeSHi
/* in_action/chapterF/HelloWorldPdfX.java */
 
package part4.chapter13;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.CMYKColor;
import com.itextpdf.text.pdf.ICC_Profile;
import com.itextpdf.text.pdf.PdfAConformanceLevel;
import com.itextpdf.text.pdf.PdfAWriter;
import com.itextpdf.text.pdf.PdfWriter;
 
public class PdfXPdfA {
 
    /** The resulting PDF file. */
    public static final String RESULT1 = "results/part4/chapter13/x.pdf";
    /** The resulting PDF file. */
    public static final String RESULT2 = "results/part4/chapter13/a.pdf";
 
    /** A font program that is used. */
    public static final String FONT = "c:/windows/fonts/arial.ttf";
    /** A color profile that is used. */
    public static final String PROFILE = "resources/img/sRGB.profile";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException 
     * @throws IOException
     */
    public void createPdfX(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(filename));
        writer.setPDFXConformance(PdfWriter.PDFX1A2001);
        // step 3
        document.open();
        // step 4
        Font font = FontFactory.getFont(
            FONT, BaseFont.CP1252, BaseFont.EMBEDDED, Font.UNDEFINED,
            Font.UNDEFINED, new CMYKColor(255, 255, 0, 0));
        document.add(new Paragraph("Hello World", font));
        // step 5
        document.close();
    }
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException 
     * @throws IOException
     */
    public void createPdfA(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfAWriter writer = PdfAWriter.getInstance(document,
            new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_1B);
        writer.createXmpMetadata();
        // step 3
        document.open();
        // step 4
        Font font = FontFactory.getFont(FONT, BaseFont.CP1252, BaseFont.EMBEDDED);
        document.add(new Paragraph("Hello World", font));
        ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream(PROFILE));
        writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
        // step 5
        document.close();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        PdfXPdfA example = new PdfXPdfA();
        example.createPdfX(RESULT1);
        example.createPdfA(RESULT2);
    }
}