The requirement is to replace a text in pdf document with paragraph. Search text is identified using TextFragmentAbsorber and it finds the word in the document.
TextFragments from TextFragmentAbsorber are iterated to replace the text. New text is assigned to the TextFragment and the document is saved to new location.
The output pdf shows the replaced text at the correct place, but in the first line it shows less content and in the second line the content is getting overflown. WholeWordsHyphenation option is used by the TextFragmentAbsorber.TextReplaceOptions.ReplaceAdjustmentAction. How to resolve the wrapping of the replaced text.
Aspose library version : 22.9
I have attached the code snippet, input pdf screebnshot and output pdf screenshot.
Code Snippet
public static void ReplaceTextOnAllPages()
{
// The path to the documents directory.
string oldFile = @"E:\Sample Applications\PDFEditor\Input PDF\pft-report2.pdf";
string newFile = @"E:\Sample Applications\PDFEditor\Output PDF\pft-report2.pdf";
// Open document
Document pdfDocument = new Document(oldFile);
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("[Interpretation]");
textFragmentAbsorber.TextReplaceOptions.ReplaceAdjustmentAction = TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation;
// 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)
{
textFragment.Text = "This is a inserted paragraph, I want to insert this paragraph in the start of document.This is a inserted paragraph, I want to insert this paragraph in the start of document.This is a inserted paragraph, I want to insert this paragraph in the start of document.";
}
pdfDocument.Save(newFile);
Process.Start(newFile);
}
Input PDF :
image.png (45.3 KB)
Output PDF:
image.png (53.2 KB)