Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created December 23, 2020 03:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/59dd796a6ed24191345670aff3ad9a99 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/59dd796a6ed24191345670aff3ad9a99 to your computer and use it in GitHub Desktop.
Find and Replace Text in PDF C#
// 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");
// 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");
// 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");
// 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