New to Telerik Document Processing? Start a free 30-day trial
How to replace text in PDF
Updated on Jun 9, 2026
Environment
| Product Version | Product | Author |
|---|---|---|
| 2021.2.615 | RadPdfProcessing | Tanya Dimitrova |
Description
With RadPdfProcessing, you can import a PDF document and traverse the text inside to replace specific matches. Iterate the TextFragment objects and modify their Text property when needed.
Solution
The following code iterates the TextFragment instances on each RadFixedPage and replaces the "<%textfield,1234%>" string with "replaced text":
csharp
foreach (RadFixedPage page in document.Pages)
{
foreach (ContentElementBase contentElement in page.Content)
{
if (contentElement is TextFragment)
{
TextFragment textFragment = (TextFragment)contentElement;
string replacedText = "replaced text";
Replace(textFragment, "<%textfield,1234%>", replacedText);
}
}
}
The following snippet shows the implementation of the Replace method:
csharp
private static void Replace(TextFragment textFragment, string oldValue, string newValue)
{
if (textFragment.Text.Contains(oldValue))
{
textFragment.Text = newValue;
}
}
Notes
The team has logged requests to improve the API and provide a more straightforward way to search and replace text. You can vote for their implementation and subscribe to receive updates about status changes through the related public items: