ZUGFeRD Tutorial: examples for chapter 2

This is a code example of iText PDF, discover more.

27th August 2015
admin-marketing

Switch code for this example

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.


Tags
ZUGFeRD

C2E1_SimplePdf.java
  1. /*
  2.  * This code sample was written in the context of the tutorial:
  3.  * ZUGFeRD: The future of Invoicing
  4.  */
  5. package zugferd.pdfa;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.PageSize;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. import sandbox.WrapToTest;
  21.  
  22. /**
  23.  * Creates a simple PDF with images and text.
  24.  */
  25. @WrapToTest
  26. public class C2E1_SimplePdf {
  27.  
  28.     /** The resulting PDF. */
  29.     public static final String DEST = "results/zugferd/pdfa/quickbrownfox1.pdf";
  30.     /** An image resource. */
  31.     public static final String FOX = "resources/images/fox.bmp";
  32.     /** An image resource. */
  33.     public static final String DOG = "resources/images/dog.bmp";
  34.  
  35.     /**
  36.      * Creates a simple PDF with images and text.
  37.      * @param args no arguments needed.
  38.      * @throws IOException
  39.      * @throws DocumentException
  40.      */
  41.     static public void main(String args[]) throws IOException, DocumentException {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new C2E1_SimplePdf().createPdf(DEST);
  45.     }
  46.    
  47.     /**
  48.      * Creates a simple PDF with images and text
  49.      * @param dest  the resulting PDF
  50.      * @throws IOException
  51.      * @throws DocumentException
  52.      */
  53.     public void createPdf(String dest) throws IOException, DocumentException {
  54.         Document document = new Document(PageSize.A4.rotate());
  55.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  56.         writer.setPdfVersion(PdfWriter.VERSION_1_7);
  57.         document.open();
  58.         Paragraph p = new Paragraph();
  59.         p.setFont(new Font(Font.FontFamily.HELVETICA, 20));
  60.         Chunk c = new Chunk("The quick brown ");
  61.         p.add(c);
  62.         Image i = Image.getInstance(FOX);
  63.         c = new Chunk(i, 0, -24);
  64.         p.add(c);
  65.         c = new Chunk(" jumps over the lazy ");
  66.         p.add(c);
  67.         i = Image.getInstance(DOG);
  68.         c = new Chunk(i, 0, -24);
  69.         p.add(c);
  70.         document.add(p);
  71.         document.close();
  72.     }
  73.  
  74. }
C2E2_TaggedPdf.java
  1. /*
  2.  * This code sample was written in the context of the tutorial:
  3.  * ZUGFeRD: The future of Invoicing
  4.  */
  5. package zugferd.pdfa;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.PageSize;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. /**
  21.  * Creates a Tagged PDF with images and text.
  22.  */
  23. public class C2E2_TaggedPdf {
  24.  
  25.     /** The resulting PDF. */
  26.     public static final String DEST = "results/zugferd/pdfa/quickbrownfox2.pdf";
  27.     /** An image resource. */
  28.     public static final String FOX = "resources/images/fox.bmp";
  29.     /** An image resource. */
  30.     public static final String DOG = "resources/images/dog.bmp";
  31.  
  32.     /**
  33.      * Creates a tagged PDF with images and text.
  34.      * @param args  no arguments needed
  35.      * @throws IOException
  36.      * @throws DocumentException
  37.      */
  38.     static public void main(String args[]) throws IOException, DocumentException {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new C2E2_TaggedPdf().createPdf(DEST);
  42.     }
  43.  
  44.     /**
  45.      * Creates a tagged PDF with images and text.
  46.      * @param dest  the path to the resulting PDF
  47.      * @throws IOException
  48.      * @throws DocumentException
  49.      */
  50.     public void createPdf(String dest) throws IOException, DocumentException {
  51.         Document document = new Document(PageSize.A4.rotate());
  52.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  53.         writer.setPdfVersion(PdfWriter.VERSION_1_7);
  54.         //TAGGED PDF
  55.         //Make document tagged
  56.         writer.setTagged();
  57.         //==========
  58.         document.open();
  59.  
  60.         Paragraph p = new Paragraph();
  61.         p.setFont(new Font(Font.FontFamily.HELVETICA, 20));
  62.         Chunk c = new Chunk("The quick brown ");
  63.         p.add(c);
  64.         Image i = Image.getInstance(FOX);
  65.         c = new Chunk(i, 0, -24);
  66.         p.add(c);
  67.         c = new Chunk(" jumps over the lazy ");
  68.         p.add(c);
  69.         i = Image.getInstance(DOG);
  70.         c = new Chunk(i, 0, -24);
  71.         p.add(c);
  72.         document.add(p);
  73.  
  74.         document.close();
  75.     }
  76.  
  77. }
C2E3_PdfA3b.java
  1. /*
  2.  * This code sample was written in the context of the tutorial:
  3.  * ZUGFeRD: The future of Invoicing
  4.  */
  5. package zugferd.pdfa;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.FontFactory;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.PageSize;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.pdf.BaseFont;
  15. import com.itextpdf.text.pdf.ICC_Profile;
  16. import com.itextpdf.text.pdf.PdfAConformanceLevel;
  17. import com.itextpdf.text.pdf.PdfAWriter;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19. import sandbox.WrapToTest;
  20.  
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25.  
  26. /**
  27.  * Creates a PDF that conforms with PDF/A-3 Level B.
  28.  */
  29. @WrapToTest
  30. public class C2E3_PdfA3b {
  31.  
  32.     /** The resulting PDF. */
  33.     public static final String DEST = "results/zugferd/pdfa/quickbrownfox3.pdf";
  34.     /** An image resource. */
  35.     public static final String FOX = "resources/images/fox.bmp";
  36.     /** An image resource. */
  37.     public static final String DOG = "resources/images/dog.bmp";
  38.     /** A path to a color profile. */
  39.     public static final String ICC = "resources/data/sRGB_CS_profile.icm";
  40.     /** A font that will be embedded. */
  41.     public static final String FONT = "resources/fonts/FreeSans.ttf";
  42.    
  43.     /**
  44.      * Creates a PDF that conforms with PDF/A-3 Level B.
  45.      * @param args  No arguments needed
  46.      * @throws IOException
  47.      * @throws DocumentException
  48.      */
  49.     static public void main(String args[]) throws IOException, DocumentException {
  50.         File file = new File(DEST);
  51.         file.getParentFile().mkdirs();
  52.         new C2E3_PdfA3b().createPdf(DEST);
  53.     }
  54.    
  55.     /**
  56.      * Creates a PDF that conforms with PDF/A-3 Level B.
  57.      * @param dest  the path to the resulting PDF
  58.      * @throws IOException
  59.      * @throws DocumentException
  60.      */
  61.     public void createPdf(String dest) throws IOException, DocumentException {
  62.         Document document = new Document(PageSize.A4.rotate());
  63.         //PDF/A-3b
  64.         //Create PdfAWriter with the required conformance level
  65.         PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(dest), PdfAConformanceLevel.PDF_A_3B);
  66.         writer.setPdfVersion(PdfWriter.VERSION_1_7);
  67.         //Create XMP metadata
  68.         writer.createXmpMetadata();
  69.         //====================
  70.         document.open();
  71.         //PDF/A-3b
  72.         //Set output intents
  73.         ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream(ICC));
  74.         writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
  75.         //===================
  76.  
  77.         Paragraph p = new Paragraph();
  78.         //PDF/A-3b
  79.         //Embed font
  80.         p.setFont(FontFactory.getFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 20));
  81.         //=============
  82.         Chunk c = new Chunk("The quick brown ");
  83.         p.add(c);
  84.         Image i = Image.getInstance(FOX);
  85.         c = new Chunk(i, 0, -24);
  86.         p.add(c);
  87.         c = new Chunk(" jumps over the lazy ");
  88.         p.add(c);
  89.         i = Image.getInstance(DOG);
  90.         c = new Chunk(i, 0, -24);
  91.         p.add(c);
  92.         document.add(p);
  93.  
  94.         document.close();
  95.     }
  96.  
  97. }
C2E4_PdfA3a.java
  1. /*
  2.  * This code sample was written in the context of the tutorial:
  3.  * ZUGFeRD: The future of Invoicing
  4.  */
  5. package zugferd.pdfa;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.FontFactory;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.PageSize;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.pdf.BaseFont;
  15. import com.itextpdf.text.pdf.ICC_Profile;
  16. import com.itextpdf.text.pdf.PdfAConformanceLevel;
  17. import com.itextpdf.text.pdf.PdfAWriter;
  18. import com.itextpdf.text.pdf.PdfName;
  19. import com.itextpdf.text.pdf.PdfString;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21. import sandbox.WrapToTest;
  22.  
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27.  
  28. /**
  29.  * Creates a PDF that conforms with PDF/A-3 Level A.
  30.  */
  31. @WrapToTest
  32. public class C2E4_PdfA3a {
  33.  
  34.     /** The resulting PDF. */
  35.     public static final String DEST = "results/zugferd/pdfa/quickbrownfox4.pdf";
  36.     /** An image resource. */
  37.     public static final String FOX = "resources/images/fox.bmp";
  38.     /** An image resource. */
  39.     public static final String DOG = "resources/images/dog.bmp";
  40.     /** A path to a color profile. */
  41.     public static final String ICC = "resources/data/sRGB_CS_profile.icm";
  42.     /** A font that will be embedded. */
  43.     public static final String FONT = "resources/fonts/FreeSans.ttf";
  44.    
  45.     /**
  46.      * Creates a PDF that conforms with PDF/A-3 Level A.
  47.      * @param args  no arguments needed
  48.      * @throws IOException
  49.      * @throws DocumentException
  50.      */
  51.     static public void main(String args[]) throws IOException, DocumentException {
  52.         File file = new File(DEST);
  53.         file.getParentFile().mkdirs();
  54.         new C2E4_PdfA3a().createPdf(DEST);
  55.     }
  56.    
  57.     /**
  58.      * Creates a PDF that conforms with PDF/A-3 Level B.
  59.      * @param dest  the path to the resulting PDF
  60.      * @throws IOException
  61.      * @throws DocumentException
  62.      */
  63.     public void createPdf(String dest) throws IOException, DocumentException {
  64.         Document document = new Document(PageSize.A4.rotate());
  65.         //PDF/A-3a
  66.         //Create PdfAWriter with the required conformance level
  67.         PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(dest), PdfAConformanceLevel.PDF_A_3A);
  68.         writer.setPdfVersion(PdfWriter.VERSION_1_7);
  69.         //====================
  70.         //TAGGED PDF
  71.         //Make document tagged
  72.         writer.setTagged();
  73.         //===============
  74.         //PDF/UA
  75.         //Set document metadata
  76.         writer.setViewerPreferences(PdfWriter.DisplayDocTitle);
  77.         document.addLanguage("en-US");
  78.         document.addTitle("Some title");
  79.         writer.createXmpMetadata();
  80.         //=====================
  81.         document.open();
  82.         //PDF/A-3b
  83.         //Set output intents
  84.         ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream(ICC));
  85.         writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
  86.         //===================
  87.  
  88.         Paragraph p = new Paragraph();
  89.         //PDF/UA
  90.         //Embed font
  91.         p.setFont(FontFactory.getFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 20));
  92.         //==================
  93.         Chunk c = new Chunk("The quick brown ");
  94.         p.add(c);
  95.         Image i = Image.getInstance(FOX);
  96.         c = new Chunk(i, 0, -24);
  97.         //PDF/UA
  98.         //Set alt text
  99.         c.setAccessibleAttribute(PdfName.ALT, new PdfString("Fox"));
  100.         //==============
  101.         p.add(c);
  102.         p.add(new Chunk(" jumps over the lazy "));
  103.         i = Image.getInstance(DOG);
  104.         c = new Chunk(i, 0, -24);
  105.         //PDF/UA
  106.         //Set alt text
  107.         c.setAccessibleAttribute(PdfName.ALT, new PdfString("Dog"));
  108.         //==================
  109.         p.add(c);
  110.         document.add(p);
  111.  
  112.         document.close();
  113.     }
  114.  
  115. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now