Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Aspose.PDF ReplaceText method
public void ReplaceText(string sourcePDF, string destinationPDF, string textPhrase, string replacementText)
{
// Open document
using (Document pdfDocument = new Document(sourcePDF))
{
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(textPhrase);
// Accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber);
// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
// Update text and other properties
textFragment.Text = replacementText;
}
// Save resulting PDF document.
pdfDocument.Save(destinationPDF);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment