How to Replace Text in a PDF

by Curtis Chau

Swapping out text within a PDF is incredibly convenient for swiftly and accurately editing content. It's perfect for fixing typos, refreshing information, or tailoring templates for specific needs. This feature is a real-time-saver, particularly for documents that need frequent updates or personalized touches.

IronPDF offers a text replacement feature for PDFs, making it an indispensable tool for developers and professionals seeking to automate or customize PDF content.

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Replace Text Example

To replace text, we can simply call the replaceText method. The method takes three parameters: the first one is PageSelection, which specifies the page; the second is a string representing the old text; and the third is the new text. For this example below, we will call the PageSelection.firstPage method, which retrieves the first page of the PDF. We will replace all instances of '.NET6' with '.NET7'.

You will encounter a runtime exception if the method cannot find the specified old text. alt text

Code

import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;

import java.io.IOException;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) throws IOException {
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>.NET6</h1>");

        String oldText = ".NET6";
        String newText = ".NET7";

        // Replace text
        pdf.replaceText(PageSelection.firstPage(), oldText, newText);
        pdf.saveAs("replaceText.pdf");
    }
}
JAVA

Tips
All page indexes follow zero-based indexing.

Output PDF


Replace Text on Multiple Pages

We use the same replaceText method to replace text on multiple pages. But this time, we call the pageRange method from the PageSelection class and input an array of Integers to specify that we want to replace the text only on the first and third pages.

import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class App {
    public static void main(String[] args) throws IOException {
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        String html = "<p> .NET6 </p>" +
                    "<p> This is 1st Page </p>" +
                    "<div style = 'page-break-after: always;'></div>" +
                    "<p> This is 2nd Page</p>" +
                    "<div style = 'page-break-after: always;'></div>" +
                    "<p> .NET6 </p>" +
                    "<p> This is 3rd Page</p>";

        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);

        String oldText = ".NET6";
        String newText = ".NET7";
        // Replace text on page 1 & 3
        List<Integer> pages = Arrays.asList(0, 2);

        // Replace text
        pdf.replaceText(PageSelection.pageRange(pages), oldText, newText);
        pdf.saveAs("replaceTextOnMultiplePages.pdf");
    }
}
JAVA

Ouput PDF


Explore PageSelection Class

Like the example above, using the Pageselection method allows developers to specify which pages to replace text with. A complete list of the parameters is below.

Please note
Since the PageSelection class is static, you do not need to create a new instance to use its methods.

The page index starts at 0.

  • allPages: A method that selects all pages of the PDF.
  • firstPage: A method that selects the first page of the PDF.
  • lastPage: A method that selects the last page of the PDF.
  • pageRange​(int startIndex, int endIndex): A method that takes the startIndex and endIndex, this selects the number of pages. For example, putting it startIndex = 0 and endIndex = 2 selects pages 1 to 3.
  • pageRange​(ListpageList): A method that specifies which pages to select; from the example above, if the list only contains the Integer 0 and 2, the method only selects the first and third page and skips the second page.
  • singlePage​(int pageIndex): A method that specifies a single page of the PDF.