PDF Technique 01

PDF1: Applying text alternatives to images with the Alt entry in PDF documents

When the content is plain text, the accessible content should be fairly obvious. However, some PDF documents will contain images or other visually-oriented artifacts. This technique involves providing alternate text for in these situations. The code below creates a PDF with a single image.

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 Pdf01Alt {

  public static final String IMAGE_PATH = 
      "resources/itext-in-action.jpg";
  public static final String IMAGE_ALT_TEXT =
      "Image of book cover: iText in Action. Author Bruno Lowagie. " +
      "Also, there is a withered old lady on the cover. " +
      "This is what you look like after you have read the PDF spec from cover to cover.";

  public static void main(String[] args) {
    try {
      Document document = new Document(PageSize.LETTER);
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
          "pdf-output/Pdf01Alt.pdf"));

      Rectangle rect = document.getPageSize();

      writer.setTagged();

      document.open();

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

      DocumentStructure documentStructure = new DocumentStructure(rootElement, "en-us");
      PageStructure pageStructure = documentStructure.getPage(0);

      PdfStructureElement imageElement = new PdfStructureElement(pageStructure.bodyElement, PdfName.DIV);

      cb.beginMarkedContentSequence(imageElement);

      PdfDictionary dict = new PdfDictionary();
      dict.put(PdfName.ALT, new PdfString(IMAGE_ALT_TEXT));
      cb.beginMarkedContentSequence(new PdfName("Span"), dict, true);
      Image img = Image.getInstance(IMAGE_PATH);
      img.setAbsolutePosition(100, rect.getHeight() - 200);
      cb.addImage(img, true);

      cb.endMarkedContentSequence();  // end span
      cb.endMarkedContentSequence();  // end imageElement

      document.close();
    } 
    catch (Exception e) {
      e.printStackTrace();
    }
  }

}

Line 42 stores the alternate text in a dictionary. Line 43 begins the marked content sequence to which the alternate text applies. Lastly, line 46 adds the images to the document. The rest of the code should be self-explanatory.

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>