Hi Vasanth,
Thanks for sharing the details.
I have gone through your requirements and I have also managed to reproduce the same problem that when setting background color for text fragment, it covers the contents and textfragment is not visible. So in current circumstances, creating a Highlight markup annotation is viable solution. Please take a look over following code snippet where i have first searched the text fragment, extracted its coordinates (rectangular coordinates) and then have used the same values to create markup annotation.
I am afraid when using the below approach, the markup annotation is quite large (not according to coordinates specified). For the sake of correction, I have logged this
issue as PDFNEWNET-36059 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.
[C#]
//open document<o:p></o:p>
Document pdfDocument = new Document("c:/pdftest/test_from-graphic_ocr (1).pdf");
//create TextAbsorber object to find
all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Test");
//accept the absorber for all the pages
pdfDocument.Pages[1].Accept(textFragmentAbsorber);
//get the extracted text fragments
TextFragmentCollection textFragmentCollection =
textFragmentAbsorber.TextFragments;
// create Rectangle object to hold
TextFragment rectangular coordinates
System.Drawing.Rectangle
text_rect = new System.Drawing.Rectangle();
//loop through the fragments
foreach (TextFragment
textFragment in textFragmentCollection)
{
foreach (TextSegment
textSegment in textFragment.Segments)
{
// print text segment
Console.WriteLine("Text :
{0} ", textSegment.Text);
Console.WriteLine("XIndent
: {0} ",textSegment.Position.XIndent);
Console.WriteLine("YIndent
: {0} ",textSegment.Position.YIndent);
// create Rectangle object based on
TextFragment rectangle coordinates
text_rect = new
System.Drawing.Rectangle((int)textFragment.Rectangle.LLX, (int)textFragment.Rectangle.LLY, (int)textFragment.Rectangle.URX, (int)textFragment.Rectangle.URY);
// print Rectangular coordinates of
text fragment
Console.WriteLine("LLX =
"+(int)textFragment.Rectangle.LLX +
" LLY = " + (int)textFragment.Rectangle.LLY +" URX = " + (int)textFragment.Rectangle.URX
+" URY = "+ (int)textFragment.Rectangle.URY);
}
}
// create content editor object
Aspose.Pdf.Facades.PdfContentEditor
editor = new PdfContentEditor();
// bind the source PDF file
editor.BindPdf(pdfDocument);
// create markup
editor.CreateMarkup(text_rect, "", 0, 1, System.Drawing.Color.Yellow);
// save updated file
editor.Save(@“C:\pdftest\test_Highlight.pdf”);
As a workaround, please try using the following code line, instead. For your reference, I have also attached the resultant PDF generted with this workaround.
[C#]
text_rect = new System.Drawing.Rectangle((int)textFragment.Rectangle.LLX,
(int)textFragment.Rectangle.LLY, (int)textFragment.Rectangle.Width, (int)textFragment.Rectangle.Height);