How to Find and Replace Text in PDF using Java

This brief tutorial explains how to find and replace text in PDF using Java. It is also helpful in scenarios where you want to exclude some sensitive or categorized information. For replacing text, load the PDF search and replace using Java and then save the updated PDF document.

Steps to Find and Replace Text in PDF using Java

  1. Configure your application to install Aspose.PDF for Java to find and replace text
  2. Load the input PDF file using the Document class for replacing the text
  3. Specify the text phrase which needs to be searched in the TextFragmentAbsorber object
  4. Set the text replacing options and accept the text absorber for all the pages
  5. Create a collection of matching text phrases in the PDF
  6. Update the found text by replacing it with new text
  7. Save the output PDF file after replacing the text

These steps summarize how with the help of Java replace text in PDF in your applications. We can perform this operation on a newly created PDF file as well as on an existing PDF document depending upon the application workflow. Note that while replacing the text, you can change the text font, foreground color, and background color also.

Code to Replace Text in PDF using Java

import com.aspose.pdf.Document;
import com.aspose.pdf.License;
public class FindAndReplaceTextInPdfUsingJava {
public static void main(String[] args) throws Exception {
// Instantiate license to create presentation in HTML
License pdfLicense = new License();
pdfLicense.setLicense("Aspose.Pdf.lic");
// Load the input PDF document
Document pdfDocument = new Document("Input.pdf");
// Create TextFragmentAbsorber object
com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("Rack");
// Set text replace options
com.aspose.pdf.TextReplaceOptions options = new com.aspose.pdf.TextReplaceOptions();
options.setReplaceScope(com.aspose.pdf.TextReplaceOptions.Scope.REPLACE_FIRST);
textFragmentAbsorber.setTextReplaceOptions(options);
// Accept the text absorber for the entire collection of pages
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted fragments in a collection
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through all text fragments
for (com.aspose.pdf.TextFragment textFragment : textFragmentCollection) {
// Update the text
textFragment.setText("New Rack");
}
// Save the updated PDF file
pdfDocument.save("Output.pdf");
System.out.println("Done");
}
}

This code sample exhibits how to replace text in PDF using Java. Moreover, you can work with many properties to enhance the code snippet. For example, updating the text appearance properties, finding the text from a specific page region, finding text using a regular expression, text replacement scope, text replacement strategy, etc.

In order to replace text in PDF Java-based environment configuration and code snippet is discussed in this article. However, if you want to learn to protect PDF with a password, refer to the article on how to protect PDF with password in Java.

 English