How to Replace Text in a PDF

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;

/**
 * Main application class for demonstrating how to replace text in a PDF.
 */
public class App {

    public static void main(String[] args) throws IOException {

        // Set the IronPDF license key
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Render HTML content into a PDF
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>.NET6</h1>");

        // Define the old and new text for replacement
        String oldText = ".NET6";
        String newText = ".NET7";

        // Replace all instances of oldText with newText on the first page
        pdf.replaceText(PageSelection.firstPage(), oldText, newText);

        // Save the resulting PDF document
        pdf.saveAs("replaceText.pdf");
    }
}
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;
import java.io.IOException;

/**
 * Main application class for demonstrating how to replace text in a PDF.
 */
public class App {

    public static void main(String[] args) throws IOException {

        // Set the IronPDF license key
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Render HTML content into a PDF
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>.NET6</h1>");

        // Define the old and new text for replacement
        String oldText = ".NET6";
        String newText = ".NET7";

        // Replace all instances of oldText with newText on the first page
        pdf.replaceText(PageSelection.firstPage(), oldText, newText);

        // Save the resulting PDF document
        pdf.saveAs("replaceText.pdf");
    }
}
JAVA

TipsAll 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 a list 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.util.Arrays;
import java.util.List;

/**
 * Main application class for demonstrating how to replace text on multiple pages of a PDF.
 */
public class App {

    public static void main(String[] args) throws IOException {

        // Set the IronPDF license key
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // HTML content that will be converted to a 3-page PDF
        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>";

        // Render the HTML content into a PDF
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);

        // Define the old and new text for replacement
        String oldText = ".NET6";
        String newText = ".NET7";

        // Define the pages where text replacement should occur (first and third page)
        List<Integer> pages = Arrays.asList(0, 2);

        // Replace the text on specified pages
        pdf.replaceText(PageSelection.pageRange(pages), oldText, newText);

        // Save the resulting PDF document
        pdf.saveAs("replaceTextOnMultiplePages.pdf");
    }
}
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Main application class for demonstrating how to replace text on multiple pages of a PDF.
 */
public class App {

    public static void main(String[] args) throws IOException {

        // Set the IronPDF license key
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // HTML content that will be converted to a 3-page PDF
        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>";

        // Render the HTML content into a PDF
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);

        // Define the old and new text for replacement
        String oldText = ".NET6";
        String newText = ".NET7";

        // Define the pages where text replacement should occur (first and third page)
        List<Integer> pages = Arrays.asList(0, 2);

        // Replace the text on specified pages
        pdf.replaceText(PageSelection.pageRange(pages), oldText, newText);

        // Save the resulting PDF document
        pdf.saveAs("replaceTextOnMultiplePages.pdf");
    }
}
JAVA

Output PDF


Explore PageSelection Class

Like the example above, using the PageSelection methods allows developers to specify which pages to replace text within. A complete list of the parameters is below.

Please noteSince the PageSelection class methods are 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 specifies a range of pages to select. For example, setting startIndex = 0 and endIndex = 2 selects pages 1 to 3.
  • pageRange(List<Integer> pageList): A method that specifies which pages to select; from the example above, if the list contains the integers 0 and 2, the method selects only the first and third page, skipping the second page.
  • singlePage(int pageIndex): A method that specifies a single page of the PDF.

Frequently Asked Questions

How can I replace text in a PDF using Java?

You can replace text in a PDF using IronPDF for Java by downloading the library, loading an existing PDF or creating a new one, and using the replaceText method. This method allows you to specify the text to be replaced and the new text, either on specific pages or throughout the document.

What steps are involved in replacing text in a PDF with IronPDF?

Replacing text in a PDF using IronPDF involves five main steps: downloading the Java library, loading or creating a PDF, using the replaceText method, specifying pages with PageSelection, and saving the edited PDF.

Can I replace text on specific pages in a PDF using Java?

Yes, you can replace text on specific pages in a PDF using IronPDF by utilizing the PageSelection class. This class offers methods like pageRange, singlePage, and others to target specific pages for text replacement.

What happens if the text to be replaced is not found in the PDF?

If the specified text to be replaced is not found in the PDF, IronPDF will encounter a runtime exception during the replacement process, indicating that the text could not be located.

How do I save a modified PDF after text replacement in Java?

After replacing text in a PDF using IronPDF, you can save the modified document by calling the saveAs method on the PdfDocument object, specifying the desired file name for the output PDF.

Is it necessary to instantiate the PageSelection class to use its methods in IronPDF?

No, it is not necessary to instantiate the PageSelection class to use its methods because they are static. You can directly call methods like allPages, firstPage, and others without creating a new instance.

How does zero-based indexing affect page selection in IronPDF?

In IronPDF, page selection uses zero-based indexing, meaning that page numbers start from 0. This is important for accurately specifying which pages to target when using the PageSelection methods for text replacement.

What are some methods provided by the PageSelection class in IronPDF?

The PageSelection class in IronPDF provides methods such as allPages, firstPage, lastPage, pageRange, and singlePage, which allow you to efficiently select specific pages for text replacement in a PDF.

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity ...Read More

Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?