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

Free Support Forum - aspose.com

Move text

Hello ASPOSE,

is there a way to find a text on PDF page and to move this text to another position in this page?

Regards
Gerd

Hi Gerd,


Thanks for using our API’s.

Yes, you can find a text on PDF page and then move this text to another position in the same page. In order to search text segments from a page, you first need to get the TextFragment objects from the document . TextFragmentAbsorber allows you to find text, matching a particular phrase, from a page of a PDF document.

The following code snippet shows you how to search text segments from a page and move it to another position in the page. I have attached input/output PDF documents for your reference. In below example we are searching for “electronic” word in the 1st page of the PDF document and move / appending it with “versions of Lorem Ipsum” on the same page. You can also check our documentation for more details on Working with Text

C#
string findWord = “electronic”;
string append_move_with = “versions of Lorem Ipsum”;
// Open document
Document pdfDocument = new Document(dataDir + “pdfsamplefile.pdf”);
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(findWord);
// Accept the absorber for all the pages
pdfDocument.Pages[1].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 = “”;
}

TextFragmentAbsorber textFragmentAbsorber1 = new TextFragmentAbsorber(append_move_with);
// Accept the absorber for all the pages
pdfDocument.Pages[1].Accept(textFragmentAbsorber1);
// Get the extracted text fragments
TextFragmentCollection textFragmentCollection1 = textFragmentAbsorber1.TextFragments;
// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection1)
{
// Update text and other properties
textFragment.Text = append_move_with + ’ ’ + findWord;
}

dataDir = dataDir + “output_file.pdf”;
// Save resulting PDF document.
pdfDocument.Save(dataDir);

If you still face any issue, please feel free to contact us.

Best Regards,