Home > Code > C# > How to Search & Replace Text in all Pages of PDF Using C# & VB.NET

How to Search & Replace Text in all Pages of PDF Using C# & VB.NET

by sher azam   on Oct 09, 2013   Category: C#   |  Views: 4249    |  Points: 25   |  Gold 


This technical tip shows how to replace text in all pages of a PDF Document inside .NET Applications. In order to replace text in all the pages of a PDF document, you first need to use TextFragmentAbsorber to find the particular phrase you want to replace. After that, you need to go through all the TextFragments to replace the text and change any other attributes. Once you have done that, you only need to save the output PDF using Save method of the Document object.

The following code snippet shows you how to replace text in all pages of PDF document.

[C#]

//open document
Document pdfDocument = new Document("input.pdf");
//create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Figure");
//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 = "New Phrase";
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);
}

pdfDocument.Save("output.pdf");

[VB.NET]


'open document
Dim pdfDocument As New Document("input.pdf")
'create TextAbsorber object to find all instances of the input search phrase
Dim textFragmentAbsorber As New TextFragmentAbsorber("Figure")
'accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber)
'get the extracted text fragments
Dim textFragmentCollection As TextFragmentCollection = textFragmentAbsorber.TextFragments
'loop through the fragments
For Each textFragment As TextFragment In textFragmentCollection
'update text and other properties
textFragment.Text = "New Phrase"
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)
Next TextFragment
pdfDocument.Save("output.pdf")


- Homepage of Aspose.Pdf for .NET: http://www.aspose.com/.net/pdf-component.aspx

- Read More about Working with Text in Pdf files: http://www.aspose.com/docs/display/pdfnet/Working+with+Text



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts