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,