Created
June 26, 2018 20:49
-
-
Save dannycabrera/ffd642817184d2291ce36939d6f2e4d6 to your computer and use it in GitHub Desktop.
Aspose.PDF ReplaceText method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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