Sample code for 30+ languages & platforms
Java Requires Chilkat v11.7.0+

Page Numbers, Dates and the File Name in a PDF Watermark

See more PDF Watermark Examples

Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // The PDF used in this example is available at four_pages.pdf

        Pdf pdf = new Pdf();
        try {
            pdf.loadFile("qa_data/pdf/four_pages.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json = new JsonObject();
        json.updateString("type", "text");
        json.updateString("text", "Page {page} of {pages}  -  printed {date:yyyy-MM-dd HH:mm}  -  {filename}");
        json.updateString("id", "pagefoot");
        json.updateString("position.align", "bottom");
        json.updateNumber("position.margin", "24");
        json.updateNumber("font.size", "9");
        json.updateString("color", "#505050");

        JsonObject jsonOut = new JsonObject();
        try {
            pdf.watermark("add", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("footer pages: " + jsonOut.stringOf("pages"));

        // Roman numerals that start at i on page 2, so that a cover page is not numbered.
        Pdf pdf2 = new Pdf();
        try {
            pdf2.loadFile("qa_data/pdf/four_pages.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json2 = new JsonObject();
        json2.updateString("type", "text");
        json2.updateString("text", "- {page} -");
        json2.updateString("id", "roman");
        json2.updateString("pages", "2-");
        json2.updateInt("pageNumber.start", 1);
        json2.updateString("pageNumber.style", "romanLower");
        json2.updateString("position.align", "bottom");
        json2.updateNumber("font.size", "10");
        try {
            pdf2.watermark("add", json2, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("roman pages: " + jsonOut.stringOf("pages"));

        try {
            pdf.writeFile("qa_output/four_pages_numbered.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Sample output:

        // footer pages: 1-4
        // roman pages: 2-4
    }
}