Created
December 23, 2020 03:48
Find and Replace Text in PDF C#
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
// Open document | |
Document pdfDocument = new Document("Document.pdf"); | |
// Create TextAbsorber object to find all instances of the input search phrase | |
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text"); | |
// Accept the absorber for desired | |
pdfDocument.Pages[1].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 = "TEXT"; | |
textFragment.TextState.Font = FontRepository.FindFont("Verdana"); | |
textFragment.TextState.FontSize = 22; | |
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue); | |
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green); | |
} | |
// Save resulting PDF document. | |
pdfDocument.Save("updated-document.pdf"); |
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
// Open document | |
Document pdfDocument = new Document("Document.pdf"); | |
// Create TextAbsorber object to find all the phrases matching the regular expression | |
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("\\d{4}-\\d{4}"); // Like 1999-2000 | |
// Set text search option to specify regular expression usage | |
TextSearchOptions textSearchOptions = new TextSearchOptions(true); | |
textFragmentAbsorber.TextSearchOptions = textSearchOptions; | |
// Accept the absorber for a single page | |
pdfDocument.Pages[1].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 = "New Phrase"; | |
// Set to an instance of an object. | |
textFragment.TextState.Font = FontRepository.FindFont("Verdana"); | |
textFragment.TextState.FontSize = 22; | |
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue); | |
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green); | |
} | |
// Save PDF | |
pdfDocument.Save("output.pdf"); |
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
// load PDF file | |
Document pdf = new Document("Document.pdf"); | |
// instantiate TextFragment Absorber object | |
TextFragmentAbsorber TextFragmentAbsorberAddress = new TextFragmentAbsorber(); | |
// search text within page bound | |
TextFragmentAbsorberAddress.TextSearchOptions.LimitToPageBounds = true; | |
// specify the page region for TextSearch Options | |
TextFragmentAbsorberAddress.TextSearchOptions.Rectangle = new Rectangle(100, 100, 200, 200); | |
// search text from first page of PDF file | |
pdf.Pages[1].Accept(TextFragmentAbsorberAddress); | |
// iterate through individual TextFragment | |
foreach (TextFragment tf in TextFragmentAbsorberAddress.TextFragments) | |
{ | |
// update text to blank characters | |
tf.Text = ""; | |
} | |
// save updated PDF file after text replace | |
pdf.Save("output.pdf"); |
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
// Open document | |
Document pdfDocument = new Document("Document.pdf"); | |
// Create TextAbsorber object to find all instances of the input search phrase | |
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text"); | |
// 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 = "TEXT"; | |
textFragment.TextState.Font = FontRepository.FindFont("Verdana"); | |
textFragment.TextState.FontSize = 22; | |
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue); | |
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green); | |
} | |
// Save resulting PDF document. | |
pdfDocument.Save("updated-document.pdf"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment