How to Replace Text in a PDF
IronPDF for Java gives developers direct control over existing PDF content through the replaceText method. Whether you need to correct a typo in a batch of generated reports, swap version numbers across template documents, or personalize contracts with customer-specific data, the method accepts a page selection, a search string, and a replacement string and handles the rest. This guide covers single-page replacement, multi-page targeting, every available PageSelection option, and practical patterns for template-driven workflows.
Add the IronPDF dependency, load or render a PDF, call replaceText, and save the result:
-
1Install IronPDF with Maven Central Repository
-
2Copy and run this code snippet.
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text/quickstart.javaJava -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 steps)
- Download the Java library to replace text in a PDF
- Load an existing PDF or render one from HTML
- Call
pdf.replaceText(PageSelection, oldText, newText)to replace on targeted pages - Use
PageSelectionmethods to control which pages are modified - Save and export the updated PDF with
pdf.saveAs("output.pdf")
How Do I Replace Text on a Single Page?
The replaceText method accepts three arguments: a PageSelection that targets one or more pages, the exact text to find, and the replacement string. To target the first page, pass PageSelection.firstPage(). All instances of the search string on that page are replaced in a single call. If the text cannot be found on the targeted pages, the method throws a runtime exception. The screenshot below shows what that exception looks like in the console.

What Parameters Does replaceText Accept?
The method signature is replaceText(PageSelection pageSelection, String oldText, String newText). Matching is case-sensitive by default: "net6" and "NET6" are treated as different strings. Verify the exact casing in the rendered PDF before calling the method. You can confirm the exact text by extracting the PDF's text content first. The IronPDF Java dependency is published on Maven Central and requires Java 8 or later.
-
1Install IronPDF with Maven Central Repository
-
2Copy and run this code snippet.
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text/replace-text-single-page.javaJava -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
The replaceText method integrates naturally with IronPDF's HTML to PDF conversion workflow. Render the HTML template first, then apply text replacement to inject dynamic values. This keeps your HTML clean while still producing personalized output. For documents loaded from disk, pass the file path to PdfDocument.fromFile instead of using renderHtmlAsPdf.
What Does the Output Look Like?
How Do I Replace Text Across Multiple Pages?
To target specific pages rather than just the first, pass a list of zero-indexed page numbers to PageSelection.pageRange(List<Integer>). The method replaces the search text on every page in the list and leaves all other pages untouched. This pattern suits documents with consistent headers or footers on known pages, or batch-generated reports where a version string only appears on certain pages.
Which Pages Are Modified When Using a Page List?
In the example below, a three-page PDF is created from HTML. The replacement runs on pages 0 and 2 (the first and third pages). Page 1 (the second page) keeps the original text unchanged. Page indexes always start at 0, consistent with Java's zero-based array conventions.
-
1Install IronPDF with Maven Central Repository
-
2Copy and run this code snippet.
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text/replace-text-multiple-pages.javaJava -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
0 is the first page, page 1 is the second, and so on.When creating multi-page PDFs from HTML, use the CSS page-break-after property to control where page boundaries fall. Setting the right custom paper size and page orientation before rendering ensures the content positions match your expectations when text replacement runs. After saving, verify the output by opening the PDF and confirming only the targeted pages changed.
What Does the Output Look Like?
What Page Selection Options Are Available?
The PageSelection class provides static factory methods that cover every common targeting pattern. No instance creation is needed; call the methods directly on the class. All indexes are zero-based.
Which Methods Target Single vs. Multiple Pages?
PageSelection factory methods for the replaceText API
| Method | Description |
|---|---|
PageSelection.allPages() | Selects every page in the document |
PageSelection.firstPage() | Selects page at index 0 |
PageSelection.lastPage() | Selects the final page regardless of document length |
PageSelection.singlePage(int pageIndex) | Selects one specific page by zero-based index |
PageSelection.pageRange(int startIndex, int endIndex) | Selects a contiguous range from startIndex to endIndex inclusive |
PageSelection.pageRange(List<Integer> pageList) | Selects pages at any arbitrary set of indexes (e.g., [0, 2] selects pages 1 and 3) |
When Should I Use Each PageSelection Method?
allPages() is the simplest choice for a global find-and-replace: every instance of the target text across the entire document is replaced in one call. Use firstPage() or lastPage() for quick edits to cover pages or last-page footers without touching the body. Choose pageRange(int, int) when the text appears on a sequential set of pages such as a chapter or section. Use pageRange(List<Integer>) when the target pages are non-contiguous; for example, when replacing a version string that only appears on pages 1, 3, and 7.
When working with merged PDFs or documents that include bookmarks and outlines, identify the page range for each logical section first, then apply targeted replacements to avoid modifying shared headers or footers accidentally. The IronPDF for Java API reference lists all PageSelection overloads with full parameter documentation.
replaceText calls in a try-catch block. The method throws a runtime exception when the specified search text is not found on any of the targeted pages. A validation step (extracting text and confirming the string is present) prevents unexpected failures in production.How Do I Replace Text on All Pages at Once?
PageSelection.allPages() performs a document-wide replacement in a single call, which is the most efficient approach for global token substitution. The example below loads a PDF from disk and replaces every occurrence of a placeholder token across the entire document:
-
1Install IronPDF with Maven Central Repository
-
2Copy and run this code snippet.
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text/replace-text-all-pages.javaJava -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
This pattern pairs naturally with template-driven workflows where the same placeholder appears in headers, footers, or body text across many pages. For single-page documents, allPages() and firstPage() produce identical results. Prefer allPages() when the page count may vary at runtime.
How Do I Handle Common Text Replacement Scenarios?
Text replacement in Java applications covers several patterns that go beyond a simple search-and-replace. Understanding how the method behaves in each case prevents bugs before they appear in production.
How Does Case-Sensitive Matching Affect Results?
replaceText performs exact, case-sensitive matching. The strings "Version 1.0", "version 1.0", and "VERSION 1.0" are treated as three distinct values. Before running a replacement on a document loaded from disk, confirm the exact casing by reviewing the original source or by extracting text from the PDF and inspecting it programmatically.
How Do I Replace Text in PDF Forms?
PDFs that contain interactive form fields store their text values separately from the document's content stream, as defined in the PDF specification. The replaceText method operates on the content stream and does not modify form field values. To update text inside form fields, use IronPDF's dedicated form creation and editing API instead. Mixing both approaches in the same document is safe: form field updates and content-stream replacements do not interfere with each other.
How Do I Update Text in Template-Based Workflows?
A common pattern is to maintain a PDF template with placeholder tokens (for example {{CUSTOMER_NAME}} or [INVOICE_DATE]) and replace them at runtime with actual values. Call replaceText once per placeholder, using PageSelection.allPages() so the replacement covers every location the token appears. For documents generated from HTML, this workflow works equally well with renderHtmlAsPdf followed by a series of replacement calls. Combine this with PDF watermarking or backgrounds and foregrounds to add branding or confidentiality markings to the final output.
What Are the Next Steps for PDF Text Replacement in Java?
This guide covered single-page replacement with PageSelection.firstPage(), selective multi-page replacement with PageSelection.pageRange(), document-wide replacement with PageSelection.allPages(), and the full set of PageSelection factory methods. The same replaceText API works whether the document was rendered from HTML, loaded from disk, or assembled by merging multiple sources.
Start a free trial of IronPDF for Java and run the code examples above against your own documents. When you are ready to deploy to production, review the licensing options. Licenses are per-developer and include one year of product updates.
Ready to see what else IronPDF for Java can do? Browse the complete IronPDF for Java how-to guides for tutorials on PDF generation, annotation, digital signatures, compression, and more.
Frequently Asked Questions
How can I replace text in a PDF using Java with IronPDF?
To replace text in a PDF using IronPDF for Java, use the `replaceText` method. This method requires a `PageSelection` for targeting pages, the search string, and the replacement string. You can target single pages, a range of pages, or all pages using various `PageSelection` options.
What is the syntax for the replaceText method in IronPDF?
The `replaceText` method signature is `replaceText(PageSelection pageSelection, String oldText, String newText)`. It performs a case-sensitive match and replaces the specified text on the targeted pages.
How do I replace text on a single page using IronPDF in Java?
To replace text on a single page, use `PageSelection.firstPage()` as your page selector in the `replaceText` method. This will target the first page in your PDF document.
Can I replace text on multiple specific pages using IronPDF?
Yes, to replace text on multiple specific pages, use `PageSelection.pageRange(List
What are the available page selection options when using IronPDF's replaceText method?
IronPDF provides several `PageSelection` methods: `allPages()`, `firstPage()`, `lastPage()`, `singlePage(int pageIndex)`, `pageRange(int startIndex, int endIndex)`, and `pageRange(List
How do I replace text across all pages in a PDF using IronPDF?
Use `PageSelection.allPages()` with the `replaceText` method to replace text across all pages in a PDF document. This is ideal for global text replacement tasks.
How does IronPDF handle case sensitivity when replacing text?
`replaceText` in IronPDF performs case-sensitive matching by default. Ensure that the exact casing of the text matches your search string in the PDF.
Can IronPDF replace text in form fields within a PDF?
IronPDF's `replaceText` method operates on content streams and does not affect form field values. For form field modification, use IronPDF's form creation and editing API.
What is the recommended workflow for replacing placeholders in a PDF template using IronPDF?
Maintain a PDF template with placeholders like `{{CUSTOMER_NAME}}`. Use `replaceText` with `PageSelection.allPages()` to substitute these placeholders with actual values at runtime.
How can I start using IronPDF for Java to replace text in PDFs?
Start by downloading a free trial of IronPDF for Java. Follow the documentation and code examples to replace text in PDFs. Consider licensing options when you're ready for production deployment.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.