ZUGFeRD Tutorial: examples for chapter 2

Tags: PDF/A-3PDF/APDF/UAiText 5

These are some examples that were written in the context of Chapter 2 of the tutorial ZUGFeRD: The Future of Invoicing.

  • C2E1_SimplePdf creates a simple "Quick brown fox jumps over the lazy dog" PDF with some images, but without any structure. This results in a regular PDF.

  • C2E2_TaggedPdf.java uses the same code as the first example, but now we ask iText to introduce structure. This results in a Tagged PDF.

  • C2E3_PdfA3b.java adapts the first example, so that it conforms to the PDF/A-3 standard, level B (for Basic). The resulting PDF is not a Tagged PDF.

  • C2E4_PdfA3a.java adapts the third example, so that it conforms to the PDF/A-3 standard, level A (for Accessibility). The resulting PDF is a Tagged PDF.

Files: 
/*
 * This code sample was written in the context of the tutorial:
 * ZUGFeRD: The future of Invoicing
 */
package zugferd.pdfa;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
/**
 * Creates a simple PDF with images and text.
 */
@WrapToTest
public class C2E1_SimplePdf {
 
    /** The resulting PDF. */
    public static final String DEST = "results/zugferd/pdfa/quickbrownfox1.pdf";
    /** An image resource. */
    public static final String FOX = "resources/images/fox.bmp";
    /** An image resource. */
    public static final String DOG = "resources/images/dog.bmp";
 
    /**
     * Creates a simple PDF with images and text.
     * @param args no arguments needed.
     * @throws IOException
     * @throws DocumentException 
     */
    static public void main(String args[]) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C2E1_SimplePdf().createPdf(DEST);
    }
 
    /**
     * Creates a simple PDF with images and text
     * @param dest  the resulting PDF
     * @throws IOException
     * @throws DocumentException 
     */
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        writer.setPdfVersion(PdfWriter.VERSION_1_7);
        document.open();
        Paragraph p = new Paragraph();
        p.setFont(new Font(Font.FontFamily.HELVETICA, 20));
        Chunk c = new Chunk("The quick brown ");
        p.add(c);
        Image i = Image.getInstance(FOX);
        c = new Chunk(i, 0, -24);
        p.add(c);
        c = new Chunk(" jumps over the lazy ");
        p.add(c);
        i = Image.getInstance(DOG);
        c = new Chunk(i, 0, -24);
        p.add(c);
        document.add(p);
        document.close();
    }
 
}
/*
 * This code sample was written in the context of the tutorial:
 * ZUGFeRD: The future of Invoicing
 */
package zugferd.pdfa;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
/**
 * Creates a Tagged PDF with images and text.
 */
public class C2E2_TaggedPdf {
 
    /** The resulting PDF. */
    public static final String DEST = "results/zugferd/pdfa/quickbrownfox2.pdf";
    /** An image resource. */
    public static final String FOX = "resources/images/fox.bmp";
    /** An image resource. */
    public static final String DOG = "resources/images/dog.bmp";
 
    /**
     * Creates a tagged PDF with images and text.
     * @param args  no arguments needed
     * @throws IOException
     * @throws DocumentException 
     */
    static public void main(String args[]) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C2E2_TaggedPdf().createPdf(DEST);
    }
 
    /**
     * Creates a tagged PDF with images and text.
     * @param dest  the path to the resulting PDF
     * @throws IOException
     * @throws DocumentException 
     */
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        writer.setPdfVersion(PdfWriter.VERSION_1_7);
        //TAGGED PDF
        //Make document tagged
        writer.setTagged();
        //==========
        document.open();
 
        Paragraph p = new Paragraph();
        p.setFont(new Font(Font.FontFamily.HELVETICA, 20));
        Chunk c = new Chunk("The quick brown ");
        p.add(c);
        Image i = Image.getInstance(FOX);
        c = new Chunk(i, 0, -24);
        p.add(c);
        c = new Chunk(" jumps over the lazy ");
        p.add(c);
        i = Image.getInstance(DOG);
        c = new Chunk(i, 0, -24);
        p.add(c);
        document.add(p);
 
        document.close();
    }
 
}
/*
 * This code sample was written in the context of the tutorial:
 * ZUGFeRD: The future of Invoicing
 */
package zugferd.pdfa;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
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;
import sandbox.WrapToTest;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
/**
 * Creates a PDF that conforms with PDF/A-3 Level B.
 */
@WrapToTest
public class C2E3_PdfA3b {
 
    /** The resulting PDF. */
    public static final String DEST = "results/zugferd/pdfa/quickbrownfox3.pdf";
    /** An image resource. */
    public static final String FOX = "resources/images/fox.bmp";
    /** An image resource. */
    public static final String DOG = "resources/images/dog.bmp";
    /** A path to a color profile. */
    public static final String ICC = "resources/data/sRGB_CS_profile.icm";
    /** A font that will be embedded. */
    public static final String FONT = "resources/fonts/FreeSans.ttf";
 
    /**
     * Creates a PDF that conforms with PDF/A-3 Level B.
     * @param args  No arguments needed
     * @throws IOException
     * @throws DocumentException 
     */
    static public void main(String args[]) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C2E3_PdfA3b().createPdf(DEST);
    }
 
    /**
     * Creates a PDF that conforms with PDF/A-3 Level B.
     * @param dest  the path to the resulting PDF
     * @throws IOException
     * @throws DocumentException 
     */
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        //PDF/A-3b
        //Create PdfAWriter with the required conformance level
        PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(dest), PdfAConformanceLevel.PDF_A_3B);
        writer.setPdfVersion(PdfWriter.VERSION_1_7);
        //Create XMP metadata
        writer.createXmpMetadata();
        //====================
        document.open();
        //PDF/A-3b
        //Set output intents
        ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream(ICC));
        writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
        //===================
 
        Paragraph p = new Paragraph();
        //PDF/A-3b
        //Embed font
        p.setFont(FontFactory.getFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 20));
        //=============
        Chunk c = new Chunk("The quick brown ");
        p.add(c);
        Image i = Image.getInstance(FOX);
        c = new Chunk(i, 0, -24);
        p.add(c);
        c = new Chunk(" jumps over the lazy ");
        p.add(c);
        i = Image.getInstance(DOG);
        c = new Chunk(i, 0, -24);
        p.add(c);
        document.add(p);
 
        document.close();
    }
 
}
/*
 * This code sample was written in the context of the tutorial:
 * ZUGFeRD: The future of Invoicing
 */
package zugferd.pdfa;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ICC_Profile;
import com.itextpdf.text.pdf.PdfAConformanceLevel;
import com.itextpdf.text.pdf.PdfAWriter;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfWriter;
import sandbox.WrapToTest;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
/**
 * Creates a PDF that conforms with PDF/A-3 Level A.
 */
@WrapToTest
public class C2E4_PdfA3a {
 
    /** The resulting PDF. */
    public static final String DEST = "results/zugferd/pdfa/quickbrownfox4.pdf";
    /** An image resource. */
    public static final String FOX = "resources/images/fox.bmp";
    /** An image resource. */
    public static final String DOG = "resources/images/dog.bmp";
    /** A path to a color profile. */
    public static final String ICC = "resources/data/sRGB_CS_profile.icm";
    /** A font that will be embedded. */
    public static final String FONT = "resources/fonts/FreeSans.ttf";
 
    /**
     * Creates a PDF that conforms with PDF/A-3 Level A.
     * @param args  no arguments needed
     * @throws IOException
     * @throws DocumentException 
     */
    static public void main(String args[]) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C2E4_PdfA3a().createPdf(DEST);
    }
 
    /**
     * Creates a PDF that conforms with PDF/A-3 Level B.
     * @param dest  the path to the resulting PDF
     * @throws IOException
     * @throws DocumentException 
     */
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        //PDF/A-3a
        //Create PdfAWriter with the required conformance level
        PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(dest), PdfAConformanceLevel.PDF_A_3A);
        writer.setPdfVersion(PdfWriter.VERSION_1_7);
        //====================
        //TAGGED PDF
        //Make document tagged
        writer.setTagged();
        //===============
        //PDF/UA
        //Set document metadata
        writer.setViewerPreferences(PdfWriter.DisplayDocTitle);
        document.addLanguage("en-US");
        document.addTitle("Some title");
        writer.createXmpMetadata();
        //=====================
        document.open();
        //PDF/A-3b
        //Set output intents
        ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream(ICC));
        writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
        //===================
 
        Paragraph p = new Paragraph();
        //PDF/UA
        //Embed font
        p.setFont(FontFactory.getFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 20));
        //==================
        Chunk c = new Chunk("The quick brown ");
        p.add(c);
        Image i = Image.getInstance(FOX);
        c = new Chunk(i, 0, -24);
        //PDF/UA
        //Set alt text
        c.setAccessibleAttribute(PdfName.ALT, new PdfString("Fox"));
        //==============
        p.add(c);
        p.add(new Chunk(" jumps over the lazy "));
        i = Image.getInstance(DOG);
        c = new Chunk(i, 0, -24);
        //PDF/UA
        //Set alt text
        c.setAccessibleAttribute(PdfName.ALT, new PdfString("Dog"));
        //==================
        p.add(c);
        document.add(p);
 
        document.close();
    }
 
}
File nameRaw URLUpdated
C2E1_SimplePdf.javaC2E1_SimplePdf.java2015-08-27 12:02 pm
C2E2_TaggedPdf.javaC2E2_TaggedPdf.java2015-08-27 12:02 pm
C2E3_PdfA3b.javaC2E3_PdfA3b.java2015-08-27 12:02 pm
C2E4_PdfA3a.javaC2E4_PdfA3a.java2015-08-27 12:02 pm
Resources: 
File nameRaw URLUpdated
dog.bmpdog.bmp2015-08-27 12:05 pm
fox.bmpfox.bmp2015-08-27 12:05 pm
sRGB_CS_profile.icmsRGB_CS_profile.icm2015-08-27 12:06 pm
FreeSans.ttfFreeSans.ttf2015-08-27 12:07 pm