Add Page Number to PDF
All the documents must have page numbers in it. The page number makes it easier for the reader to locate different parts of the document. Aspose.PDF for Java allows you to add page numbers with PageNumberStamp.
You can use PageNumberStamp class to add a page number stamp in a PDF document. The PageNumberStamp class provides methods to create a page number based stamp like format, margins, alignments, starting number etc. In order to add page number stamp, you need to create a Document object and a PageNumberStamp object with required properties. After that, you can call addStamp(..) method of the Page class to add the stamp in PDF file. You can also set the font attributes of the page number stamp.
The following code snippet shows you how to add page numbers in a PDF file.
package com.aspose.pdf.examples;
import com.aspose.pdf.Color;
import com.aspose.pdf.Document;
import com.aspose.pdf.FontRepository;
import com.aspose.pdf.FontStyles;
import com.aspose.pdf.HorizontalAlignment;
import com.aspose.pdf.PageNumberStamp;
public class ExampleAddPageNumberToPDF {
private static String _dataDir = "/home/admin1/pdf-examples/Samples/";
public static void ExampleAddPageNumber() {
// Open document
Document pdfDocument = new Document(_dataDir + "PageNumberStamp.pdf");
// Create page number stamp
PageNumberStamp pageNumberStamp = new PageNumberStamp();
// Whether the stamp is background
pageNumberStamp.setBackground(false);
pageNumberStamp.setFormat("Page # of " + pdfDocument.getPages().size());
pageNumberStamp.setBottomMargin (10);
pageNumberStamp.setHorizontalAlignment ( HorizontalAlignment.Center);
pageNumberStamp.setStartingNumber(1);
// Set text properties
pageNumberStamp.getTextState().setFont (FontRepository.findFont("Arial"));
pageNumberStamp.getTextState().setFontSize (14.0F);
pageNumberStamp.getTextState().setFontStyle (FontStyles.Bold);
pageNumberStamp.getTextState().setForegroundColor (Color.getAqua());
// Add stamp to particular page
pdfDocument.getPages().get_Item(1).addStamp(pageNumberStamp);
_dataDir = _dataDir + "PageNumberStamp_out.pdf";
// Save output document
pdfDocument.save(_dataDir);
}
}