PDF Technique 17

Specifying consistent page numbering for PDF documents

This technique ensures that the page numbers displayed in a document visually match the page number as shown by the the software displaying the document for navigation purposes. Specifically, Adobe Reader has a ToolBar area for page navigation that displays the current page number. A relevant example might be a PDF that has a preface or foreword. This pages are not generally considered to be a
core part of the document and often have their own page numbering. You might see these non-core pages numbered with roman numerals (i, ii, iii, etc) and the core content will restart the page numbering at Page 1. Many PDF authors do not take this into consideration and Adobe Reader will display the first page of the core content as Page 4.

Note, that this technique should be employed for all documents that have multiple page numbers, not just documents need to be accessible.

Here is an example of how you implement consistent page numbering. The document is six pages. The following list describes each page.

  1. Table of contents. It has no page number and Adobe Reader will display the page number as blank.
  2. Page 1 of preface. Both Adobe Reader and the PDF document will display the page number as the roman numeral i.
  3. Page 2 of preface. Both Adobe Reader and the PDF document will display the page number as the roman numeral ii.
  4. Page 1 of core. Both Adobe Reader and the PDF document will display the page number as 1.
  5. Page 2 of core. Both Adobe Reader and the PDF document will display the page number as 2.
  6. Page 1 of Appendix. Both Adobe Reader and the PDF document will display the page number as Appendix A-1.
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 Pdf17PageNumbers {
  
  
  public static void main(String[] args) {
    try {
      Document document = new Document(PageSize.LETTER);
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
          "pdf-output/Pdf17PageNumbers.pdf"));

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

      document.open();

      BaseFont bf = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);
      
      PdfContentByte cb = writer.getDirectContent();
      PdfStructureTreeRoot rootElement = writer.getStructureTreeRoot();

      DocumentStructure documentStructure = new DocumentStructure(rootElement, "en-us");

      
      
      
      // generate the table of contents which has no page number...
      
      PageStructure pageStructure = documentStructure.getPage(0);
      
      PdfStructureElement paragraphElement = new PdfStructureElement(pageStructure.bodyElement, PdfName.P);
      
      cb.beginMarkedContentSequence(paragraphElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

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

      cb.showText("Table of contents...");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end paragraphElement
      
      document.newPage();
      
      
      
      
      
      
      
      
      // generate first page of preface...
      
      pageStructure = documentStructure.getPage(0);
      
      paragraphElement = new PdfStructureElement(pageStructure.bodyElement, PdfName.P);
      
      cb.beginMarkedContentSequence(paragraphElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

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

      cb.showText("This is the first page of the preface.");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end paragraphElement
      
      
      cb.beginMarkedContentSequence(pageStructure.footerElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

      cb.moveText(rect.getRight() - 100, rect.getBottom() + 30);

      cb.showText("Page i");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end footerElement
      
      
      document.newPage();

      
      
      // generate second page of preface...
      
      pageStructure = documentStructure.getPage(1);
      
      paragraphElement = new PdfStructureElement(pageStructure.bodyElement, PdfName.P);
      
      cb.beginMarkedContentSequence(paragraphElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

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

      cb.showText("This is the second page of the preface.");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end paragraphElement
      
      
      cb.beginMarkedContentSequence(pageStructure.footerElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

      cb.moveText(rect.getRight() - 100, rect.getBottom() + 30);

      cb.showText("Page ii");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end footerElement
      
      
      document.newPage();
      
      
      // generate first page of book...
      
      pageStructure = documentStructure.getPage(2);
      
      paragraphElement = new PdfStructureElement(pageStructure.bodyElement, PdfName.P);
      
      cb.beginMarkedContentSequence(paragraphElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

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

      cb.showText("This is the first page of the book.");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end paragraphElement
      
      
      cb.beginMarkedContentSequence(pageStructure.footerElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

      cb.moveText(rect.getRight() - 100, rect.getBottom() + 30);

      cb.showText("Page 1");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end footerElement
      
      
      document.newPage();
      
      
      // generate second page of book...
      
      pageStructure = documentStructure.getPage(3);
      
      paragraphElement = new PdfStructureElement(pageStructure.bodyElement, PdfName.P);
      
      cb.beginMarkedContentSequence(paragraphElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

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

      cb.showText("This is the second page of the book.");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end paragraphElement
      
      
      cb.beginMarkedContentSequence(pageStructure.footerElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

      cb.moveText(rect.getRight() - 100, rect.getBottom() + 30);

      cb.showText("Page 2");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end footerElement
      
      
      document.newPage();
      
      // generate first page of appendix...
      
      pageStructure = documentStructure.getPage(4);
      
      paragraphElement = new PdfStructureElement(pageStructure.bodyElement, PdfName.P);
      
      cb.beginMarkedContentSequence(paragraphElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

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

      cb.showText("This is the first page of the appendix.");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end paragraphElement
      
      
      cb.beginMarkedContentSequence(pageStructure.footerElement);
      cb.beginText();
      cb.setFontAndSize(bf, 12);
      cb.setLeading(18);

      cb.moveText(rect.getRight() - 100, rect.getBottom() + 30);

      cb.showText("Appendix A-1");
      cb.endText();
      
      cb.endMarkedContentSequence();  // end footerElement
      
      
      
      PdfPageLabels labels = new PdfPageLabels();
      labels.addPageLabel(1, PdfPageLabels.EMPTY);
      labels.addPageLabel(2, PdfPageLabels.LOWERCASE_ROMAN_NUMERALS);
      labels.addPageLabel(4, PdfPageLabels.DECIMAL_ARABIC_NUMERALS);
      labels.addPageLabel(6, PdfPageLabels.DECIMAL_ARABIC_NUMERALS, "Appendix A-", 1);
      writer.setPageLabels(labels);

      
      
      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>