pdfHTML: Accessible PDF Creation

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

7th June 2017
iText PDF

Switch code for this example

CreateAccessiblePDF.java

  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.pdfHTML.AccessiblePDF;
  10.  
  11. import com.itextpdf.html2pdf.ConverterProperties;
  12. import com.itextpdf.html2pdf.HtmlConverter;
  13. import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory;
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfDocumentInfo;
  16. import com.itextpdf.kernel.pdf.PdfName;
  17. import com.itextpdf.kernel.pdf.PdfString;
  18. import com.itextpdf.kernel.pdf.PdfViewerPreferences;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.WriterProperties;
  21. import com.itextpdf.layout.font.FontProvider;
  22. import com.itextpdf.licensekey.LicenseKey;
  23. import com.itextpdf.samples.pdfHTML.AccessiblePDF.HeaderTagging.AccessibilityTagWorkerFactory;
  24.  
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. public class CreateAccessiblePDF {
  32.  
  33.     public static final String sourceFolder = "src/test/resources/pdfHTML/AccessiblePDF/";
  34.     public static final String destinationFolder = "target/output/pdfHTML/AccessiblePDF/";
  35.     public static final String[] files = {"Accessibility"};
  36.     //License key path
  37.     public static final String LICENSE = "src/test/resources/pdfHTML/itextkey-html2pdf_typography.xml";
  38.  
  39.  
  40.     public static void main(String[] args) throws IOException, InterruptedException {
  41.         LicenseKey.loadLicenseFile(LICENSE);
  42.         for (String name : files) {
  43.             String htmlSource = sourceFolder + name  + ".html";
  44.             String resourceFolder = sourceFolder;
  45.             String pdfDest = destinationFolder + name + ".pdf";
  46.             File file = new File(pdfDest);
  47.  
  48.             System.out.println("Parsing: " + htmlSource);
  49.             file.getParentFile().mkdirs();
  50.  
  51.             new CreateAccessiblePDF().createPdf(htmlSource,pdfDest,resourceFolder);
  52.         }
  53.     }
  54.  
  55.     public void createPdf(String src, String dest, String resources) throws IOException {
  56.         try {
  57.             FileOutputStream outputStream = new FileOutputStream(dest);
  58.  
  59.             WriterProperties writerProperties = new WriterProperties();
  60.             //Add metadata
  61.             writerProperties.addXmpMetadata();
  62.  
  63.             PdfWriter pdfWriter = new PdfWriter(outputStream, writerProperties);
  64.  
  65.             PdfDocument pdfDoc = new PdfDocument(pdfWriter);
  66.             pdfDoc.getCatalog().setLang(new PdfString("en-US"));
  67.             //Set the document to be tagged
  68.             pdfDoc.setTagged();
  69.             pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
  70.  
  71.             //Set meta tags
  72.             PdfDocumentInfo pdfMetaData = pdfDoc.getDocumentInfo();
  73.             pdfMetaData.setAuthor("Samuel Huylebroeck");
  74.             pdfMetaData.addCreationDate();
  75.             pdfMetaData.getProducer();
  76.             pdfMetaData.setCreator("iText Software");
  77.             pdfMetaData.setKeywords("example, accessibility");
  78.             pdfMetaData.setSubject("PDF accessibility");
  79.             //Title is derived from html
  80.  
  81.             // pdf conversion
  82.             ConverterProperties props = new ConverterProperties();
  83.             FontProvider fp = new FontProvider();
  84.             fp.addStandardPdfFonts();
  85.             fp.addDirectory(resources);//The noto-nashk font file (.ttf extension) is placed in the resources
  86.  
  87.             props.setFontProvider(fp);
  88.             props.setBaseUri(resources);
  89.             //Setup custom tagworker factory for better tagging of headers
  90.             DefaultTagWorkerFactory tagWorkerFactory = new AccessibilityTagWorkerFactory();
  91.             props.setTagWorkerFactory(tagWorkerFactory);
  92.  
  93.             try (FileInputStream fileInputStream = new FileInputStream(src)) {
  94.                 HtmlConverter.convertToPdf(fileInputStream, pdfDoc, props);
  95.             }
  96.             pdfDoc.close();
  97.  
  98.         } catch (Exception e) {
  99.             e.printStackTrace();
  100.         }
  101.     }
  102. }

AccessibilityTagWorkerFactory.java

  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.pdfHTML.AccessiblePDF.HeaderTagging;
  10.  
  11. import com.itextpdf.html2pdf.attach.ITagWorker;
  12. import com.itextpdf.html2pdf.attach.ProcessorContext;
  13. import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory;
  14. import com.itextpdf.styledxmlparser.node.IElementNode;
  15.  
  16. public class AccessibilityTagWorkerFactory extends DefaultTagWorkerFactory {
  17.  
  18.     @Override
  19.     public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
  20.         //This can probably replaced with a regex or string pattern
  21.         if(tag.name().equals("h1")){
  22.             return new HeaderTagWorker(tag, context,1);
  23.         }
  24.         if(tag.name().equals("h2")){
  25.             return new HeaderTagWorker(tag, context,2);
  26.         }
  27.         if(tag.name().equals("h3")){
  28.             return new HeaderTagWorker(tag, context,3);
  29.         }
  30.         if(tag.name().equals("h4")){
  31.             return new HeaderTagWorker(tag, context,4);
  32.         }
  33.         if(tag.name().equals("h5")){
  34.             return new HeaderTagWorker(tag, context,5);
  35.         }
  36.         if(tag.name().equals("h6")){
  37.             return new HeaderTagWorker(tag, context,6);
  38.         }
  39.  
  40.         if(tag.name().equals("th")){
  41.             return new TableHeaderTagWorker(tag,context);
  42.         }
  43.  
  44.         return null;
  45.     }
  46. }

HeaderTagWorker.java

  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.pdfHTML.AccessiblePDF.HeaderTagging;
  10.  
  11. import com.itextpdf.html2pdf.attach.ProcessorContext;
  12. import com.itextpdf.html2pdf.attach.impl.tags.DivTagWorker;
  13. import com.itextpdf.layout.IPropertyContainer;
  14. import com.itextpdf.layout.element.Div;
  15. import com.itextpdf.styledxmlparser.node.IElementNode;
  16.  
  17.  
  18. public class HeaderTagWorker extends DivTagWorker {
  19.     private int i;
  20.     public HeaderTagWorker(IElementNode element, ProcessorContext context, int i) {
  21.         super(element, context);
  22.         this.i = i;
  23.     }
  24.  
  25.     @Override
  26.     public IPropertyContainer getElementResult() {
  27.         Div div =(Div) super.getElementResult();
  28.         div.getAccessibilityProperties().setRole("H"+i);
  29.         return super.getElementResult();
  30.     }
  31. }

TableHeaderTagWorker.java

  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.pdfHTML.AccessiblePDF.HeaderTagging;
  10.  
  11. import com.itextpdf.html2pdf.attach.ProcessorContext;
  12. import com.itextpdf.html2pdf.attach.impl.tags.TdTagWorker;
  13. import com.itextpdf.kernel.pdf.tagging.StandardRoles;
  14. import com.itextpdf.layout.IPropertyContainer;
  15. import com.itextpdf.layout.element.Cell;
  16. import com.itextpdf.styledxmlparser.node.IElementNode;
  17.  
  18. public class TableHeaderTagWorker extends TdTagWorker {
  19.     public TableHeaderTagWorker(IElementNode element, ProcessorContext context) {
  20.         super(element, context);
  21.     }
  22.  
  23.     @Override
  24.     public IPropertyContainer getElementResult() {
  25.         Cell cell =(Cell) super.getElementResult();
  26.         cell.getAccessibilityProperties().setRole(StandardRoles.TH);
  27.         return super.getElementResult();
  28.     }
  29. }
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