Tuesday, October 8, 2013

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


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")


4 comments:

  1. You can also read an interesting c# acrobat remove text from pdf on this page http://www.rasteredge.com/how-to/csharp-imaging/pdf-text-edit-delete/

    ReplyDelete
  2. You will need to download the free vb.net acrobat remove text from pdf and use the vb.net acrobat delete text in pdf for you would need a PDF developement library on rasteredge page http://www.rasteredge.com/how-to/vb-net-imaging/pdf-text-edit-delete/

    ReplyDelete
  3. The first and easiest way to kown how to replace pages in vn.net pdf document is to use the rasteredage page http://www.rasteredge.com/how-to/vb-net-imaging/pdf-text-search/.

    ReplyDelete
  4. The first and easiest way to kown how to replace pages in vn.net pdf document is to use the rasteredage page http://www.rasteredge.com/how-to/vb-net-imaging/pdf-text-search/.

    ReplyDelete