PDF Technique 09

Providing headings by marking content with heading tags in PDF documents

The mark up used to create your content is very important for accessibility. Visually, you may see an <h1>…</h1> and <span style="font-size: x-large; font-weight: bold;">…</span> as the same, but underneath the covers they are very different. A screen reader will use this information to convey the significance of the content to the user. In the following example, we’ll use an H1 header. You will need to become familiar with Table 333 – Standard Structure Types for Grouping Elements of the PDF specification to apply this technique correctly.

package ca.discotek.itext.guide;

import java.io.*;

import ca.discotek.itext.DocumentStructure;
import ca.discotek.itext.DocumentStructure.PageStructure;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

public class Pdf09Headings {
  
  static final String MANUFACTURERS[] = {"BMW", "Lexus", "Mercedes", "Porshe"};
  
  public static void main(String[] args) {
    try {
      Document document = new Document(PageSize.LETTER);
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
          "pdf-output/Pdf09Headings.pdf"));

      Rectangle rect = document.getPageSize();
      
      writer.setTagged();

      document.open();

      PdfContentByte cb = writer.getDirectContent();
      PdfStructureTreeRoot rootElement = writer.getStructureTreeRoot();

      BaseFont bf = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);
      
      DocumentStructure documentStructure = new DocumentStructure(rootElement, "en-us");
      PageStructure pageStructure = documentStructure.getPage(0);
      
      
      
      PdfStructureElement headerElement = new PdfStructureElement(pageStructure.bodyElement, PdfName.H1);
      cb.beginMarkedContentSequence(headerElement);
      cb.beginText();

      cb.setFontAndSize(bf, 20);
      cb.setLeading(18);

      cb.moveText(rect.getLeft() + 50, rect.getTop() - 50);

      cb.showText("Fancy Car Manufacturers");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end headerElement
      
      
      PdfStructureElement paragraphElement = new PdfStructureElement(pageStructure.bodyElement, PdfName.P);
      cb.beginMarkedContentSequence(paragraphElement);
      
      for (int i=0; i<MANUFACTURERS.length; i++) {
        cb.beginText();

        cb.setFontAndSize(bf, 12);
        cb.setLeading(18);

        
        cb.moveText(rect.getLeft() + 50, rect.getTop() - (100 + i * 25 ));
        cb.showText(MANUFACTURERS[i]);
        
        cb.endText();
      }
      
      cb.endMarkedContentSequence();  // end paragraphElement
      
      
      document.close();
    } 
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>