Creating PDF Document Page Labels in Java with Apache PDFBox

Tags: Java Apache PDFBox PDF

Introduction

In PDF document format the page label to identify each document page visually for example showing text label on thumbnail panel on PDF reader application. In this Java tutorial we will learn how to create page labels for PDF document by Java application using Apache PDFBox library.

Apache PDFBox Library Overview

The Apache PDFBox is an open source Java library for working with PDF documents. You can get more information about the project at pdfbox.apache.org

Adding Apache PDFBox Dependencies

Adding below dependencies to build.gradle file if you are using Gradle build tool.

compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.18'

Adding below XML to pom.xml file if you are using Maven build tool.

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.18</version>
</dependency>

Or you can download the pdfbox-2.0.18.jar file from pdfbox.apache.org/download.cgi

Step 1 - Creating an empty PDF document and add 5 blank pages

try (PDDocument document = new PDDocument())
{
    for(int i = 0; i < 5; i++) {
        document.addPage(new PDPage());
    }

} catch (IOException e) {
    e.printStackTrace();
}

Step 2 - Adding Page Labels for document

PDPageLabels pageLabels = new PDPageLabels(document);
PDPageLabelRange pageLabelRange = new PDPageLabelRange();
pageLabelRange.setStyle(PDPageLabelRange.STYLE_ROMAN_UPPER);
pageLabelRange.setPrefix("Chapter ");
pageLabelRange.setStart(1);
pageLabels.setLabelItem(0, pageLabelRange);
document.getDocumentCatalog().setPageLabels(pageLabels);

Step 3 - Save PDF document file to disk

document.save("D:\\SimpleSolution\\DocumentPageLabelsRomanUpper.pdf");

Complete Java application code

package dev.simplesolution;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDPageLabelRange;
import org.apache.pdfbox.pdmodel.common.PDPageLabels;

public class CreatePageLabelsRomanUpper {
	
	public static void main(String[] args)
    {
        try (PDDocument document = new PDDocument())
        {
        	for(int i = 0; i < 5; i++) {
        		document.addPage(new PDPage());
        	}
            
            PDPageLabels pageLabels = new PDPageLabels(document);
            PDPageLabelRange pageLabelRange = new PDPageLabelRange();
            pageLabelRange.setStyle(PDPageLabelRange.STYLE_ROMAN_UPPER);
            pageLabelRange.setPrefix("Chapter ");
            pageLabelRange.setStart(1);
            pageLabels.setLabelItem(0, pageLabelRange);
            document.getDocumentCatalog().setPageLabels(pageLabels);
            
            document.save("D:\\SimpleSolution\\DocumentPageLabelsRomanUpper.pdf");
        } catch (IOException e) {
			e.printStackTrace();
		}
    }

}

Execute the Java code above we will have PDF file created at D:\SimpleSolution\DocumentPageLabelsRomanUpper.pdf

Open DocumentPageLabelsRomanUpper.pdf file on PDF reader application and show the thumbnail window you can see the page labels as below.

Creating PDF Document Page Labels in Java with Apache PDFBox - Roman Upper

Page Label Style

There are 5 different page label style suport by Apache PDFBox library

  • Uppercase Roman numerals I, II, III,…
  • Lowercase Roman numerals i, ii, iii,…
  • Uppercase letters A-Z
  • Lowercase letters a-z
  • Decimal number 1,2,3,…

To update the page label style we use

pageLabelRange.setStyle(PDPageLabelRange.STYLE_ROMAN_UPPER);

Lowercase Roman page label style example

package dev.simplesolution;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDPageLabelRange;
import org.apache.pdfbox.pdmodel.common.PDPageLabels;

public class CreatePageLabelsRomanLower {
	
	public static void main(String[] args)
    {
        try (PDDocument document = new PDDocument())
        {
        	for(int i = 0; i < 5; i++) {
        		document.addPage(new PDPage());
        	}
            
            PDPageLabels pageLabels = new PDPageLabels(document);
            PDPageLabelRange pageLabelRange = new PDPageLabelRange();
            pageLabelRange.setStyle(PDPageLabelRange.STYLE_ROMAN_LOWER);
            pageLabelRange.setPrefix("Section ");
            pageLabelRange.setStart(1);
            pageLabels.setLabelItem(0, pageLabelRange);
            document.getDocumentCatalog().setPageLabels(pageLabels);
            
            document.save("D:\\SimpleSolution\\DocumentPageLabelsRomanLower.pdf");
        } catch (IOException e) {
			e.printStackTrace();
		}
    }

}
Execute the application above to see the page labels as below.

Creating PDF Document Page Labels in Java with Apache PDFBox - Roman Lower

Uppercase letters page label style example

package dev.simplesolution;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDPageLabelRange;
import org.apache.pdfbox.pdmodel.common.PDPageLabels;

public class CreatePageLabelsLettersUpper {
	
	public static void main(String[] args)
    {
        try (PDDocument document = new PDDocument())
        {
        	for(int i = 0; i < 5; i++) {
        		document.addPage(new PDPage());
        	}
            
            PDPageLabels pageLabels = new PDPageLabels(document);
            PDPageLabelRange pageLabelRange = new PDPageLabelRange();
            pageLabelRange.setStyle(PDPageLabelRange.STYLE_LETTERS_UPPER);
            pageLabelRange.setPrefix("Part ");
            pageLabelRange.setStart(1);
            pageLabels.setLabelItem(0, pageLabelRange);
            document.getDocumentCatalog().setPageLabels(pageLabels);
            
            document.save("D:\\SimpleSolution\\DocumentPageLabelsLettersUpper.pdf");
        } catch (IOException e) {
			e.printStackTrace();
		}
    }

}
Execute the application above to see the page labels as below.

Creating PDF Document Page Labels in Java with Apache PDFBox - Uppercase Letters

Lower letters page label style example

package dev.simplesolution;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDPageLabelRange;
import org.apache.pdfbox.pdmodel.common.PDPageLabels;

public class CreatePageLabelsLettersLower {
	
	public static void main(String[] args)
    {
        try (PDDocument document = new PDDocument())
        {
        	for(int i = 0; i < 5; i++) {
        		document.addPage(new PDPage());
        	}
            
            PDPageLabels pageLabels = new PDPageLabels(document);
            PDPageLabelRange pageLabelRange = new PDPageLabelRange();
            pageLabelRange.setStyle(PDPageLabelRange.STYLE_LETTERS_LOWER);
            pageLabelRange.setPrefix("Item ");
            pageLabelRange.setStart(1);
            pageLabels.setLabelItem(0, pageLabelRange);
            document.getDocumentCatalog().setPageLabels(pageLabels);
            
            document.save("D:\\SimpleSolution\\DocumentPageLabelsLettersLower.pdf");
        } catch (IOException e) {
			e.printStackTrace();
		}
    }

}

Execute the application above to see the page labels as below.

Creating PDF Document Page Labels in Java with Apache PDFBox - Lower Letters

Decimal page label style example

package dev.simplesolution;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDPageLabelRange;
import org.apache.pdfbox.pdmodel.common.PDPageLabels;

public class CreatePageLabelsDecimal {
	
	public static void main(String[] args)
    {
        try (PDDocument document = new PDDocument())
        {
        	for(int i = 0; i < 5; i++) {
        		document.addPage(new PDPage());
        	}
            
            PDPageLabels pageLabels = new PDPageLabels(document);
            PDPageLabelRange pageLabelRange = new PDPageLabelRange();
            pageLabelRange.setStyle(PDPageLabelRange.STYLE_DECIMAL);
            pageLabelRange.setPrefix("Page ");
            pageLabelRange.setStart(1);
            pageLabels.setLabelItem(0, pageLabelRange);
            document.getDocumentCatalog().setPageLabels(pageLabels);
            
            document.save("D:\\SimpleSolution\\DocumentPageLabelsDecimal.pdf");
        }catch (IOException e) {
			e.printStackTrace();
		}
    }

}
Execute the application above to see the page labels as below.

Creating PDF Document Page Labels in Java with Apache PDFBox - Decimal

Set different page label styles for a PDF document

Below example Java application to show how to set different page label styles for different page in one PDF document.

package dev.simplesolution;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDPageLabelRange;
import org.apache.pdfbox.pdmodel.common.PDPageLabels;

public class CreatePageLabels {
	
	public static void main(String[] args)
    {
        try (PDDocument document = new PDDocument())
        {
        	for(int i = 0; i < 10; i++) {
        		document.addPage(new PDPage());
        	}
            
            PDPageLabels pageLabels = new PDPageLabels(document);
            PDPageLabelRange pageLabelRangeRoman = new PDPageLabelRange();
            pageLabelRangeRoman.setStyle(PDPageLabelRange.STYLE_ROMAN_UPPER);
            pageLabelRangeRoman.setPrefix("Chapter ");
            pageLabelRangeRoman.setStart(1);
            pageLabels.setLabelItem(0, pageLabelRangeRoman);
            
            PDPageLabelRange pageLabelRangeLetters = new PDPageLabelRange();
            pageLabelRangeLetters.setStyle(PDPageLabelRange.STYLE_LETTERS_UPPER);
            pageLabelRangeLetters.setPrefix("Part ");
            pageLabelRangeLetters.setStart(1);
            pageLabels.setLabelItem(2, pageLabelRangeLetters);
            
            
            PDPageLabelRange pageLabelRangeDecimal = new PDPageLabelRange();
            pageLabelRangeDecimal.setStyle(PDPageLabelRange.STYLE_DECIMAL);
            pageLabelRangeDecimal.setPrefix("Page ");
            pageLabelRangeDecimal.setStart(1);
            pageLabels.setLabelItem(5, pageLabelRangeDecimal);
            
            document.getDocumentCatalog().setPageLabels(pageLabels);
            
            document.save("D:\\SimpleSolution\\DocumentPageLabels.pdf");
        }catch (IOException e) {
			e.printStackTrace();
		}
    }

}

Execute the application above to see the page labels as below.

Creating PDF Document Page Labels in Java with Apache PDFBox

Download Source Code

The source code in this article can be found at: github.com/simplesolutiondev/ApachePDFBoxPageLabels

or download at:

Download Source Code

Happy Coding 😊

Creating PDF Document File in Java using Apache PDFBox

Insert Image to PDF Document in Java using Apache PDFBox

Apache PDFBox Adding Metadata to PDF Document in Java

Creating Bookmarks for PDF Document in Java with Apache PDFBox