Modifying PDF content programmatically is crucial when sensitive or outdated information must be updated before distribution. One of the most practical features in PDF manipulation is the ability to locate a specific phrase or pattern and replace it with alternate content. In this guide, we’ll walk through how to find and replace text in PDF files using Java, leveraging the powerful Conholdate.Total for Java SDK. The SDK simplifies document manipulation tasks such as redaction, replacement, and batch processing with just a few lines of code.

We’ll explore two common scenarios:

  1. Exact phrase replacement – locate a specific word or sentence and substitute it with a placeholder or new text.
  2. Regular‑expression replacement – match variable patterns like phone numbers, account numbers, or custom identifiers and replace them dynamically.

Why Find and Replace Text in PDF Files?

  • Ensure privacy and compliance – Remove personal or confidential data before publishing or sharing PDFs.
  • Update documents efficiently – Automatically replace outdated terminology across large batches of files.
  • Flexible search options – Use both exact phrase matching and regular expressions for thorough redaction.
  • Preserve document integrity – Keep the original layout and formatting while updating the content.
  • Automation friendly – Integrate into Java workflows for batch processing and document automation.

Find and Replace Text in PDF using Java - SDK Installation

To work with PDF documents, install the Conholdate.Total for Java SDK. You can add the library via Maven, Gradle, or download the JAR directly from the New Releases page.

For Maven, add the dependency:

<dependency>
    <groupId>com.conholdate.total</groupId>
    <artifactId>total-java</artifactId>
    <version>latest</version>
</dependency>

Alternatively, if you prefer a package manager, run:

Install-Package Conholdate.Total

Find and Replace Text in PDF using Java

When PDFs contain sensitive information such as names, contact details, or organization identifiers, you often need to redact or replace those strings before distribution. The Conholdate.Total SDK provides a simple API to accomplish this. The example below demonstrates how to search for an exact phrase and replace it with a placeholder.

final Redactor redactor  = new Redactor("path/document.docx");
redactor.apply(new ExactPhraseRedaction("John Doe", new ReplacementOptions("[censored]")));
// Save the edited file to a new location.
FileOutputStream stream = new FileOutputStream("path/exactPhrase.docx");
RasterizationOptions rasterOptions = new RasterizationOptions();
rasterOptions.setEnabled(false);
redactor.save(stream, rasterOptions);

In this snippet, the SDK scans the PDF for the phrase “John Doe” and substitutes every occurrence with “[censored]”. The RasterizationOptions are disabled so the output remains a text‑based PDF, preserving searchability and file size. Saving to a new path ensures the original document stays untouched.

Find and Replace Text in PDF with Regular Expressions in Java

Sometimes the text you need to replace does not follow a fixed format. Patterns such as invoice numbers, postal codes, or identification numbers vary from document to document. Regular expressions let you define flexible matching rules, and the Conholdate.Total SDK applies them seamlessly.

// Find text using regular expression and replace it with a placeholder
final Redactor redactor  = new Redactor("path/document.docx");
redactor.apply(new RegexRedaction("\\d{2}\\s*\\d{2}[^\\d]*\\d{6}", new ReplacementOptions("[censored]")));
redactor.save();

The regular expression \\d{2}\\s*\\d{2}[^\\d]*\\d{6} matches common formatted codes (e.g., bank references or transaction IDs). Any text that fits this pattern is automatically located and replaced with “[censored]”, allowing you to protect sensitive data even when its exact value is unknown.

Conclusion

Finding and replacing text in PDF documents with Java is straightforward thanks to the Conholdate.Total SDK. Whether you need to swap a single word or apply complex regex patterns, the library offers precise control, maintains document formatting, and supports automation across large document sets. Incorporate this solution into your Java projects to streamline redaction, update workflows, and ensure compliance with privacy regulations.

See Also