PDF Technique 16

Setting the default language using the /Lang entry in the document catalog of a PDF document

This technique was partially discussed in the introduction. The /Lang entry at the document level allows the screen reader to read the title of a document in the correct language. It also tells the screen reader what the default language is for the rest of the document, but this could also be achieved by setting the language attribute for PdfStructureTreeRoot (see introduction for more information). At the time of writing, there does not appear to be a sensible way to set the the language attribute at the document level. It appears that iText 5.3.5 will provide a way to do so (as documented here), but only the javadocs appear to be available.

Until 5.3.5 is available, there is an alternate way to achieve the same result. The PdfReader class has an API for setting the language. Hence, you have to create the PDF and then stamp it. It’s a two step process, which means you either have to buffer the entire PDF in memory or write it out to disk between the two steps. Buffering it would look like this:

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 Pdf16DocumentLang {
  
  
  public static void main(String[] args) {
    try {
      Document document = new Document(PageSize.LETTER);
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      PdfWriter writer = PdfWriter.getInstance(document, bos);

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

      document.open();

      BaseFont bf = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);

      PdfContentByte cb = writer.getDirectContent();

      PdfStructureTreeRoot rootElement = writer.getStructureTreeRoot();
      PdfStructureElement paragraphElement = new PdfStructureElement(rootElement, PdfName.P);

      cb.beginMarkedContentSequence(paragraphElement);

      cb.beginText();

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

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

      cb.showText("iText is awesome!");
      cb.endText();

      cb.endMarkedContentSequence();

      document.close();
      
      PdfReader reader = new PdfReader(bos.toByteArray());
      reader.getCatalog().put(PdfName.LANG, new PdfString("fr-ca")); // for french canadian
      PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("pdf-output/Pdf16DocumentLang.pdf")); 
      stamper.close(); 
    } 
    catch (Exception e) {
      e.printStackTrace();
    }
  }

}

Lines 16 through 47 create an arbitrary PDF. Lines 49 to 52 demonstrate how to stamp the PDF with
the /Lang attribute.

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>