Creating a simple PDF/UA document

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

31st May 2016
admin-marketing

Switch code for this example

PdfUA.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/28222277/how-can-i-generate-a-pdf-ua-compatible-pdf-with-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.pdfa;
  13.  
  14.  
  15. import com.itextpdf.io.font.PdfEncodings;
  16. import com.itextpdf.io.image.ImageDataFactory;
  17. import com.itextpdf.kernel.font.PdfFont;
  18. import com.itextpdf.kernel.font.PdfFontFactory;
  19. import com.itextpdf.kernel.geom.PageSize;
  20. import com.itextpdf.kernel.pdf.*;
  21. import com.itextpdf.kernel.xmp.XMPException;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.*;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30. import java.io.IOException;
  31.  
  32. @Category(SampleTest.class)
  33. public class PdfUA extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/pdfa/pdf_ua.pdf";
  35.     public static final String DOG = "./src/test/resources/img/dog.bmp";
  36.     public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
  37.     public static final String FOX = "./src/test/resources/img/fox.bmp";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new PdfUA().manipulatePdf(DEST);
  43.     }
  44.  
  45.     public void manipulatePdf(String dest) throws IOException, XMPException {
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest, new WriterProperties().setPdfVersion(PdfVersion.PDF_1_7)));
  47.         Document document = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
  48.         //TAGGED PDF
  49.         //Make document tagged
  50.         pdfDoc.setTagged();
  51.         //===============
  52.         //PDF/UA
  53.         //Set document metadata
  54.  
  55.         pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
  56.         pdfDoc.getCatalog().setLang(new PdfString("en-US"));
  57.         PdfDocumentInfo info = pdfDoc.getDocumentInfo();
  58.         info.setTitle("English pangram");
  59.         //=====================
  60.  
  61.         Paragraph p = new Paragraph();
  62.         //PDF/UA
  63.         //Embed font
  64.         PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, true);
  65.         p.setFont(font);
  66.         //==================
  67.         Text c = new Text("The quick brown ");
  68.         p.add(c);
  69.         Image i = new Image(ImageDataFactory.create(FOX));
  70.         //PDF/UA
  71.         //Set alt text
  72.         i.getAccessibilityProperties().setAlternateDescription("Fox");
  73.         //==============
  74.         p.add(i);
  75.         p.add(" jumps over the lazy ");
  76.         i = new Image(ImageDataFactory.create(DOG));
  77.         //PDF/UA
  78.         //Set alt text
  79.         i.getAccessibilityProperties().setAlternateDescription("Dog");
  80.         //==================
  81.         p.add(i);
  82.         document.add(p);
  83.         p = new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n").setFont(font).setFontSize(20);
  84.         document.add(p);
  85.         List list = new List();
  86.         list.add((ListItem) new ListItem("quick").setFont(font).setFontSize(20));
  87.         list.add((ListItem) new ListItem("brown").setFont(font).setFontSize(20));
  88.         list.add((ListItem) new ListItem("fox").setFont(font).setFontSize(20));
  89.         list.add((ListItem) new ListItem("jumps").setFont(font).setFontSize(20));
  90.         list.add((ListItem) new ListItem("over").setFont(font).setFontSize(20));
  91.         list.add((ListItem) new ListItem("the").setFont(font).setFontSize(20));
  92.         list.add((ListItem) new ListItem("lazy").setFont(font).setFontSize(20));
  93.         list.add((ListItem) new ListItem("dog").setFont(font).setFontSize(20));
  94.         document.add(list);
  95.         document.close();
  96.     }
  97. }
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