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.
How to Replace Text in a PDF
- Download the Java library to replace text in a PDF
- Load an existing PDF or create a new one
- Use the
replaceText
method to replace text on all pages - Use
PageSelection
to specify the pages for more accurate text replacement - Save and export the edited PDF document
Start using IronPDF in your project today with a free trial.
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.
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");
}
}
Tips
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");
}
}
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 note
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, settingstartIndex = 0
andendIndex = 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
What is the text replacement feature used for?
IronPDF's text replacement feature is used to swiftly and accurately edit content within a PDF. It is ideal for fixing typos, refreshing information, or tailoring templates for specific needs, and is a time-saver for documents that require frequent updates or personalized touches.
How do I replace text in a PDF using a Java library?
To replace text in a PDF using IronPDF in Java, you need to download the library, load an existing PDF or create a new one, and use the replaceText method to perform the text replacement. You can specify pages for replacement using the PageSelection class, and finally, save and export the edited PDF document.
What parameters does the replaceText method take?
The replaceText method takes three parameters: PageSelection to specify the page, a string representing the old text, and a string for the new text.
Can I replace text on multiple pages using a Java library?
Yes, you can replace text on multiple pages using the replaceText method in conjunction with the PageSelection class. You can specify a list of pages or a range of pages where the text replacement should occur.
What happens if the old text is not found during replacement?
If the specified old text cannot be found during the replacement process, a runtime exception will be encountered.
How do you specify which pages to replace text on?
You can specify which pages to replace text on using the PageSelection class. This class provides methods like allPages, firstPage, lastPage, pageRange, and singlePage to select desired pages for text replacement.
Is it necessary to create an instance of the PageSelection class to use its methods?
No, since the PageSelection class methods are static, you do not need to create a new instance to use its methods.
What is the significance of zero-based indexing in the library?
In IronPDF, all page indexes follow zero-based indexing, meaning the index of the first page starts at 0. This is important for accurately specifying page numbers when using the PageSelection methods.
How do I save the edited PDF document after replacing text?
After replacing text in the PDF, you can save the edited document by calling the saveAs method on the PdfDocument object, specifying the desired file name.