Free Support Forum - aspose.com

Copy and paste selected section of a pdf document .net

Hello,

In a PDF document. I would like to know if it is possible to copy a selected text area in the document.
Color this selection and repaste it at the same place.
I do manage to copy text and then paste it after I colored the zone of the text but the text format is not exactly the same than in the original document.
Can you please help ?
Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Pdf;
using Aspose.Pdf.Text;

namespace AsposePdf_TEST
{
class Program
{
static void Main(string[] args)
{

foreach (string item in System.IO.Directory.GetFiles(@“C:\Tampon\news”, “*.pdf”))
{
//open document
Document pdfDocument = new Document(item);
//create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(“win”);

int pageCount = 1;
//Loop through all the pages
foreach (Page pdfPage in pdfDocument.Pages)
{
bool Find = false;
double x;
double y;
pdfPage.Accept(textFragmentAbsorber);
//get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
//loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
x = textFragment.Position.XIndent;
y = textFragment.Position.YIndent;
foreach (TextSegment textSegment in textFragment.Segments)
{
Find = true;
textSegment.TextState.ForegroundColor = Color.Black;
textSegment.TextState.BackgroundColor = Color.Yellow;



// set the text position
TextFragment textFragment2 = new TextFragment(textFragment.Text);
textFragment2.Position = new Position(x, y);

textFragment2.TextState.FontSize = textFragment.TextState.FontSize;

TextBuilder textBuilder = new TextBuilder(pdfPage);
textBuilder.AppendText(textFragment2);
}
}
if (Find)
{

Document newDocument = new Document();
newDocument.Pages.Add(pdfPage);
newDocument.Save(item.Substring(0, item.Length - 3) + “_” + pageCount + “.pdf”);
}
pageCount++;
}
}

Console.ReadLine();
}
}
}

Hi Nicolas,


Thanks for your inquiry. You can get font name along with other text properties and use while updating the text. Moreover, if you are looking for the workaround to highlight text in scanned PDf documents then please use Highlight Annotation as suggested in your other post, it will resolve the issue.

Please feel free to contact us for any further assistance.

Best Regards,