Example that shows how to write "Page X of N" footer on every page.
Example_18.pdf — the PDF this example creates.
/*
* Example_18.java
*
* Copyright (c) 2026 PDFjet Software
* Licensed under the MIT License. See LICENSE file in the project root.
*/
package examples;
import java.io.*;
import java.util.*;
import com.pdfjet.*;
import com.pdfjet.fonts.*;
/**
* Example_18.java
* This example shows how to write "Page X of N" footer on every page.
*/
public class Example_18 {
public Example_18() throws Exception {
PDF pdf = new PDF(
new BufferedOutputStream(new FileOutputStream("Example_18.pdf")));
Font font = new Font(pdf, IBMPlexSans.Regular);
float fontSize = 14f;
List<Page> pages = new ArrayList<Page>();
Page page = new Page(pdf, A4.PORTRAIT, Page.DETACHED);
Rect rect = new Rect(50f, 50f, 100f, 100f);
rect.setFillColor(Color.red);
rect.drawOn(page);
pages.add(page);
page = new Page(pdf, A4.PORTRAIT, Page.DETACHED);
rect = new Rect(50f, 50f, 100f, 100f);
rect.setFillColor(Color.green);
rect.drawOn(page);
pages.add(page);
page = new Page(pdf, A4.PORTRAIT, Page.DETACHED);
rect = new Rect(50f, 50f, 100f, 100f);
rect.setFillColor(Color.blue);
rect.drawOn(page);
pages.add(page);
for (int i = 0; i < pages.size(); i++) {
page = pages.get(i);
String footer = "Page " + (i + 1) + " of " + pages.size();
page.setBrushColor(Color.black);
page.drawString(
font,
fontSize,
footer,
(page.getWidth() - font.stringWidth(fontSize, footer))/2f,
(page.getHeight() - 3f*fontSize/2f));
}
pdf.addPages(pages);
pdf.complete();
}
public static void main(String[] args) throws Exception {
long time0 = System.currentTimeMillis();
new Example_18();
long time1 = System.currentTimeMillis();
System.out.printf("Example_18 => %4d ms%n", time1 - time0);
}
} // End of Example_18.java
©2026 PDFjet Software.