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

Replace PDF Text on Selected Pages Only

See more PDF Text Replacement Examples

Demonstrates the "pages" option, which limits the search for placeholders and labels to the given pages: "all" (the default), "first", "last", "odd", "even", or a list such as "1,3,5-9". The two-page order confirmation carries the same "Status:" label on both pages; the status is changed on page 2 only, then on both pages.

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 order_confirmation.pdf

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

        JsonObject json = new JsonObject();
        JsonObject jsonOut = new JsonObject();

        // Both pages have "Status:   Pending".  With pages "2", only page 2 is searched.
        json.updateString("pages", "2");
        json.updateString("labels[0].label", "Status:");
        json.updateString("labels[0].value", "Shipped");
        try {
            pdf.textReplace("check", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("pages 2: found on page(s) " + jsonOut.stringOf("replacements[0].pages"));

        // With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
        json.updateString("pages", "all");
        try {
            pdf.textReplace("check", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("pages all, first occurrence: found on page(s) " + jsonOut.stringOf("replacements[0].pages"));

        // Every occurrence on every page.
        json.updateString("labels[0].occurrence", "all");
        try {
            pdf.textReplace("apply", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("pages all, every occurrence: " + jsonOut.intOf("replacements[0].count") + " replaced on page(s) " + jsonOut.stringOf("replacements[0].pages"));

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

        // Sample output:

        // pages 2: found on page(s) 2
        // pages all, first occurrence: found on page(s) 1
        // pages all, every occurrence: 2 replaced on page(s) 1,2
    }
}