We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace text and inherit previous font/size/weight

Is there any way with this product that I can replace text on a PDF and have the new text inherit what was there as far as font/size? Basically, I have a multi-page PDF that has several placeholders on it (of varying font sizes and weight/bold). I'd like to be able to tell whether I am replacing text that was Verdana 10 or 12 and whether is was bold or not, is this possible with Aspose?

Thanks!

Hi Brian,

Thank you for considering Aspose.Pdf.

Please check the following documentation links for “Replace Text” feature in Aspose.Pdf for .NET. You can find the sample code regarding replacing text in an existing pdf file and also Apis to manipulate the formatting of the text like font, size and color etc.

http://www.aspose.com/documentation/.net-components/aspose.pdf-for-.net/replace-text-in-all-pages-of-a-pdf-document.html

http://www.aspose.com/documentation/.net-components/aspose.pdf-for-.net/replace-text-in-an-existing-pdf-file-facades.html

In case you need any further assistance or have any other requirement, please feel free to contact support.

Thank You & Best Regards,

Hi,

Adding more to Nausherwan’s comments, your requirement can be fulfilled in two steps i.e. first you need to get the formatting information related to specific text string that you need to replace and then use the same formatting information while replacing the text string. Please take a look over following code snippet on how to get the formatting information regarding specific text string present inside particular page of the PDF document.

[C#]


// Open document
Document doc = new Document(@“D:\pdftest\HelloWorldSample.pdf”);
// Create TextFragmentAbsorber object to find all “hello world” text occurrences
TextFragmentAbsorber absorber = new TextFragmentAbsorber(“hello World”);
// Accept the absorber for first page
doc.Pages[1].Accept(absorber);
// get the font name information for specific text string
MessageBox.Show(""+absorber.TextFragments[1].TextState.Font.FontName);
// get the information related to font size
MessageBox.Show("" + absorber.TextFragments[1].TextState.FontSize);
// get the information related to text fore ground color
MessageBox.Show("" + absorber.TextFragments[1].TextState.ForegroundColor);

In order to update the text string with different formatting information, please try using the following code snippet

//get the extracted text fragments
TextFragmentCollection textFragmentCollection = absorber.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 = System.Drawing.Color.Blue;
textFragment.TextState.BackgroundColor = System.Drawing.Color.Green;
}
// save the updated PDF document
doc.Save(“d:/pdftest/UpdateText-output.pdf”);

In the event of any further query, please feel free to contact.