Add Page Numbers to PDF in Java

Page numbers can be added to an existing PDF document or to a PDF document created from scratch by using the Merger and Generator products respectively. The steps and sample codes to add page numbers to PDF is given below for both the products.

How to Add Page Numbers to an Existing PDF

Below are steps and sample code on how to add page numbering labels to an existing PDF document. The PageNumberingLabel page element can be used to automatically add page numbers to a PDF.

Steps for adding page numbers to an existing PDF document

  1. Create a MergeDocument object with the source PDF.
  2. Create a document Template object and add it to the Document.
  3. Place a PageNumberingLabel on the document Template.
  4. Invoke the draw method of the Document to output the PDF.

Sample code - Java

MergeDocument document = new MergeDocument("doc-a.pdf");

Template template = new Template();
PageNumberingLabel pageLabels = new PageNumberingLabel("%%CP%% of %%TP%%", 0, 0, 200, 20);
template.getElements().add(pageLabels);
document.setTemplate(template);

document.Draw(@"C:\Output.pdf");

How to Create PDF with Page Numbers

Below are steps and sample code on how to add page numbers to a PDF document you are creating from scratch. The PageNumberingLabel page element can be used to automatically add page numbers to a PDF.

Steps for adding page numbers to a PDF document

  1. Create a Document object.
  2. Create a document Template object and add it to the Document.
  3. Place a PageNumberingLabel on the document Template.
  4. Begin the Section of the Document with different numbering styles and add pages to the sections as needed.
  5. Invoke the draw method of the Document to output the PDF.

Sample code - Java

Document document = new Document();    
Template documentTemplate = new Template();
document.setTemplate(documentTemplate);
    
documentTemplate.getElements().add( new PageNumberingLabel("%%PR%%%%SP%% of %%ST%%", 0, 680, 512, 12, Font.getHelvetica(), 12, TextAlign.CENTER));    

document.getSections().begin( NumberingStyle.ROMAN_LOWERCASE );
document.getPages().add(new Page()); //Page 1
document.getPages().add(new Page()); //Page 2
document.getPages().add(new Page()); //Page 3
    
document.getSections().begin(NumberingStyle.NUMERIC);
document.getPages().add(new Page()); //Page 4
document.getPages().add(new Page()); //page 5
document.getPages().add(new Page()); //page 6
document.getPages().add(new Page()); //page 7
    
document.getSections().begin( NumberingStyle.ROMAN_LOWERCASE, "Appendix A - " );
document.getPages().add(new Page()); //page 8
document.getPages().add(new Page()); //page 9
    
document.Draw(@"C:\Output.pdf");

Getting Started

DynamicPDF Generator and Merger Information

More information can be found here:

Available in Other Platforms

Generator and Merger PDF Libraries are available for .NET and COM/AxtiveX platforms. Refer to the respective product pages for more details.

Why Choose DynamicPDF?

  • Transparent Pricing
  • Lots of Features
  • Easy to Use
  • Great Support
  • Efficient Performance
  • Product Maturity (Over 18 Years)
  • Free Evaluation
  • .NET Core Support (Most Products)
  • Flexible Licensing

We’re Not The Only Ones That Think We’re Great!