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 documentDocument 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 documentDim 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
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/
ReplyDeleteYou 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/
ReplyDeleteThe 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/.
ReplyDeleteThe 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