Creating a simple PDF/UA document

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

11th October 2015
admin-marketing

Switch code for this example

PdfUA.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28222277/how-can-i-generate-a-pdf-ua-compatible-pdf-with-itext
  4.  */
  5. package sandbox.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.FontFactory;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.List;
  14. import com.itextpdf.text.ListItem;
  15. import com.itextpdf.text.PageSize;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.BaseFont;
  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.FileOutputStream;
  25. import java.io.IOException;
  26.  
  27. /**
  28.  * Creates an accessible PDF with images and text.
  29.  */
  30. @WrapToTest
  31. public class PdfUA {
  32.  
  33.     /** The resulting PDF. */
  34.     public static final String DEST = "results/pdfa/pdfua.pdf";
  35.     /** An image resource. */
  36.     public static final String FOX = "resources/images/fox.bmp";
  37.     /** An image resource. */
  38.     public static final String DOG = "resources/images/dog.bmp";
  39.     /** A font that will be embedded. */
  40.     public static final String FONT = "resources/fonts/FreeSans.ttf";
  41.    
  42.     /**
  43.      * Creates an accessible PDF with images and text.
  44.      * @param args no arguments needed
  45.      * @throws IOException
  46.      * @throws DocumentException
  47.      */
  48.     static public void main(String args[]) throws IOException, DocumentException {
  49.         File file = new File(DEST);
  50.         file.getParentFile().mkdirs();
  51.         new PdfUA().createPdf(DEST);
  52.     }
  53.    
  54.     /**
  55.      * Creates an accessible PDF with images and text.
  56.      * @param dest  the path to the resulting PDF
  57.      * @throws IOException
  58.      * @throws DocumentException
  59.      */
  60.     public void createPdf(String dest) throws IOException, DocumentException {
  61.         Document document = new Document(PageSize.A4.rotate());
  62.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  63.         writer.setPdfVersion(PdfWriter.VERSION_1_7);
  64.         //TAGGED PDF
  65.         //Make document tagged
  66.         writer.setTagged();
  67.         //===============
  68.         //PDF/UA
  69.         //Set document metadata
  70.         writer.setViewerPreferences(PdfWriter.DisplayDocTitle);
  71.         document.addLanguage("en-US");
  72.         document.addTitle("English pangram");
  73.         writer.createXmpMetadata();
  74.         //=====================
  75.         document.open();
  76.  
  77.         Paragraph p = new Paragraph();
  78.         //PDF/UA
  79.         //Embed font
  80.         Font font = FontFactory.getFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 20);
  81.         p.setFont(font);
  82.         //==================
  83.         Chunk c = new Chunk("The quick brown ");
  84.         p.add(c);
  85.         Image i = Image.getInstance(FOX);
  86.         c = new Chunk(i, 0, -24);
  87.         //PDF/UA
  88.         //Set alt text
  89.         c.setAccessibleAttribute(PdfName.ALT, new PdfString("Fox"));
  90.         //==============
  91.         p.add(c);
  92.         p.add(new Chunk(" jumps over the lazy "));
  93.         i = Image.getInstance(DOG);
  94.         c = new Chunk(i, 0, -24);
  95.         //PDF/UA
  96.         //Set alt text
  97.         c.setAccessibleAttribute(PdfName.ALT, new PdfString("Dog"));
  98.         //==================
  99.         p.add(c);
  100.         document.add(p);
  101.  
  102.         p = new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n", font);
  103.         document.add(p);
  104.         List list = new List(true);
  105.         list.add(new ListItem("quick", font));
  106.         list.add(new ListItem("brown", font));
  107.         list.add(new ListItem("fox", font));
  108.         list.add(new ListItem("jumps", font));
  109.         list.add(new ListItem("over", font));
  110.         list.add(new ListItem("the", font));
  111.         list.add(new ListItem("lazy", font));
  112.         list.add(new ListItem("dog", font));
  113.         document.add(list);
  114.         document.close();
  115.     }
  116.  
  117. }

results

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