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. The steps and sample codes to add page numbers to PDF is given below for both the products.
How to Add Page Numbers to 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 Existing PDF Document
- Create a
MergeDocument
object with the source PDF. - Create a document
Template
object and add it to the Document. - Place a
PageNumberingLabel
on the document Template. - Invoke the
Draw
method of the Document object 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(@"Output.pdf");
How to Create PDF with Page Numbers
The following steps and sample code illustrate adding 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
- Create a
Document
object. - Create a document
Template
object and add it to the Document instance. - Place a
PageNumberingLabel
on the document Template. - Begin the
Section
of the Document instance with different numbering styles and add pages to the sections as needed. - Invoke the
Draw
method of the Document instance 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(@"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.
- DynamicPDF Generator
- .NET - DynamicPDF Generator for .NET
- COM/ActiveX - DynamicPDF Generator for COM/ActiveX
- DynamicPDF Merger
- .NET - DynamicPDF Merger for .NET
- COM/ActiveX - DynamicPDF Merger for COM/ActiveX