Finding and replacing text in PDF documents is essential for updating reports, legal contracts, or any other type of document where accurate and consistent information is crucial. The process involves identifying specific pieces of text and replacing them with new content, allowing users to update placeholder text, correct mistakes, customize document details, or undertake other modifications involving the written word.
This article introduces how to find and replace text in a PDF document in Java by using the Spire.PDF for Java library.
- Replace Text in a Specific PDF Page in Java
- Replace Text in an Entire PDF Document in Java
- Replace the First Instance of the Target Text in Java
- Replace Text Based on a Regular Expression in Java
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>10.10.7</version> </dependency> </dependencies>
Replace Text in a Specific PDF Page in Java
In Spire.PDF for Java, the PdfTextReplacer class is designed to facilitate text replacement within PDF documents. One of its primary methods, replaceAllText(), enables developers to replace all instances of a specified text on a page with new text.
To replace text in a specific page in Java, follow these steps:
- Create a PdfDocument object.
- Load a PDF file for a specified path.
- Get a specific page from the document.
- Create a PdfTextReplaceOptions object, and specify the replace options using setReplaceType() method of the object.
- Create a PdfTextReplacer object, and apply the replace options using setOptions() method of it.
- Replace all instances of the target text in the page with new text using PdfTextReplacer.replaceAllText() method.
- Save the document to a different PDF file.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.texts.PdfTextReplaceOptions; import com.spire.pdf.texts.PdfTextReplacer; import com.spire.pdf.texts.ReplaceActionType; import java.util.EnumSet; public class ReplaceTextInPage { public static void main(String[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf"); // Create a PdfTextReplaceOptions object PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions(); // Specify the options for text replacement textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.IgnoreCase)); textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.WholeWord)); // Get a specific page PdfPageBase page = doc.getPages().get(0); // Create a PdfTextReplacer object based on the page PdfTextReplacer textReplacer = new PdfTextReplacer(page); // Set the replace options textReplacer.setOptions(textReplaceOptions); // Replace all instances of target text with new text textReplacer.replaceAllText("MySQL", "mysql"); // Save the document to a different PDF file doc.saveToFile("output/ReplaceTextInPage.pdf"); // Dispose resources doc.dispose(); } }
Replace Text in an Entire PDF Document in Java
You already know how to replace text in one page. To replace all instances of a specific text within a PDF document with new text, you just need to iterate through each page of the document and use the PdfTextReplacer.replaceAllText() method to update the text on every page.
The following are the steps to replace text in an entire PDF document using Java.
- Create a PdfDocument object.
- Load a PDF file for a specified path.
- Create a PdfTextReplaceOptions object, and specify the replace options using setReplaceType() method of the object.
- Iterate through the pages in the document.
- Create a PdfTextReplacer object based on a specified page, and apply the replace options using setOptions() method.
- Replace all instances of the target text in the page with new text using PdfTextReplacer.replaceAllText() method.
- Save the document to a different PDF file.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.texts.PdfTextReplaceOptions; import com.spire.pdf.texts.PdfTextReplacer; import com.spire.pdf.texts.ReplaceActionType; import java.util.EnumSet; public class ReplaceTextInDocument { public static void main(String[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf"); // Create a PdfTextReplaceOptions object PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions(); // Specify the options for text replacement textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.IgnoreCase)); textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.WholeWord)); for (int i = 0; i < doc.getPages().getCount(); i++) { // Get a specific page PdfPageBase page = doc.getPages().get(i); // Create a PdfTextReplacer object based on the page PdfTextReplacer textReplacer = new PdfTextReplacer(page); // Set the replace options textReplacer.setOptions(textReplaceOptions); // Replace all instances of target text with new text textReplacer.replaceAllText("MySQL", "mysql"); } // Save the document to a different PDF file doc.saveToFile("output/ReplaceTextInDocument.pdf"); // Dispose resources doc.dispose(); } }
Replace the First Instance of the Target Text in Java
To replace the first instance of the target text in a page, you can make use of the replaceText() method from the PdfTextReplacer class. Here are the steps to accomplish this task in Java.
- Create a PdfDocument object.
- Load a PDF file for a specified path.
- Get a specific page from the document.
- Create a PdfTextReplaceOptions object, and specify the replace options using replaceType method of the object.
- Create a PdfTextReplacer object, and apply the replace options using setOptions() method.
- Replace the first occurrence of the target text in the page with new text using PdfTextReplacer.replaceText() method.
- Save the document to a different PDF file.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.texts.PdfTextReplaceOptions; import com.spire.pdf.texts.PdfTextReplacer; import com.spire.pdf.texts.ReplaceActionType; import java.util.EnumSet; public class ReplaceFirstInstance { public static void main(String[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf"); // Create a PdfTextReplaceOptions object PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions(); // Specify the options for text replacement textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.IgnoreCase)); textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.WholeWord)); // Get a specific page PdfPageBase page = doc.getPages().get(0); // Create a PdfTextReplacer object based on the page PdfTextReplacer textReplacer = new PdfTextReplacer(page); // Set the replace options textReplacer.setOptions(textReplaceOptions); // Replace the first instance of target text with new text textReplacer.replaceText("MySQL", "mysql"); // Save the document to a different PDF file doc.saveToFile("output/ReplaceFirstInstance.pdf"); // Dispose resources doc.dispose(); } }
Replace Text Based on a Regular Expression in Java
Regular expressions are incredibly powerful and flexible patterns that are commonly used for matching text. When working with Spire.PDF for Java, you can harness the capabilities of regular expressions to search for specific text within a PDF document and replace it or them with new text.
To replace text in a PDF based on a regular expression, you can follow these steps:
- Create a PdfDocument object.
- Load a PDF file for a specified path.
- Get a specific page from the document.
- Create a PdfTextReplaceOptions object.
- Specify the replace type as Regex using PdfTextReplaceOptions.setReplaceType() method.
- Create a PdfTextReplacer object, and apply the replace options using setOptions() method.
- Find and replace the text that matches a specified regular expression using PdfTextReplacer.replaceAllText() method.
- Save the document to a different PDF file.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.texts.PdfTextReplaceOptions; import com.spire.pdf.texts.PdfTextReplacer; import com.spire.pdf.texts.ReplaceActionType; import java.util.EnumSet; public class ReplaceBasedOnRegularExpression { public static void main(String[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf"); // Create a PdfTextReplaceOptions object PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions(); // Set the replace type as Regex textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.Regex)); // Get a specific page PdfPageBase page = doc.getPages().get(0); // Create a PdfTextReplacer object based on the page PdfTextReplacer textReplacer = new PdfTextReplacer(page); // Set the replace options textReplacer.setOptions(textReplaceOptions); // Specify the regular expression String regularExpression = "\\bS\\w*L\\b"; // Replace all instances that match the regular expression with new text textReplacer.replaceAllText(regularExpression, "NEW"); // Save the document to a different PDF file doc.saveToFile("output/ReplaceWithRegularExpression.pdf"); // Dispose resources doc.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.