Skip to content

Instantly share code, notes, and snippets.

@dannycabrera
Created June 26, 2018 20:49
Show Gist options
  • Save dannycabrera/ffd642817184d2291ce36939d6f2e4d6 to your computer and use it in GitHub Desktop.
Save dannycabrera/ffd642817184d2291ce36939d6f2e4d6 to your computer and use it in GitHub Desktop.
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