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

Free Support Forum - aspose.com

Help newbie for highlight phrase

I just try to search and highlight some phrase in pdf document using Aspose API
My way is

public static Color ColorFabrics(string color)
{
switch (color.ToUpper())
{
case “YLW”:
return Color.Yellow;
case “RED”:
return Color.Red;
case “GRN”:
return Color.Green;
}
return Color.Pink;
}

public static TextStamp Accept(TextFragment victim,string backgroundColor)
{
TextStamp textStamp = new TextStamp(victim.Text);
textStamp.XIndent = victim.Position.XIndent;
textStamp.YIndent = victim.Position.YIndent;
textStamp.TextState.Font = victim.TextState.Font;
textStamp.TextState.FontSize = victim.TextState.FontSize;
textStamp.TextState.FontStyle = FontStyles.Bold;
textStamp.TextState.ForegroundColor = Color.Black;
textStamp.TextState.BackgroundColor = ColorFabrics(backgroundColor);
return textStamp;
}

public static void FindAll(string phrase, Document document, string color)
{
TextFragmentAbsorber absorber = new TextFragmentAbsorber(phrase);
document.Pages.Accept(absorber);
foreach (TextFragment textFragment in absorber.TextFragments)
{
textFragment.Page.AddStamp(Accept(textFragment, color));
}
}
Am I right in my choise or exists another way to highlight particular phrase?
PS There is some trouble in my solution - I think that Aspose API does not recognize document font (Debugger tells me that font name is RT8789BP- so
textStamp.TextState.Font = victim.TextState.Font;
from my code does not match my goal)
Could anyone help me?

So sorry for my stupidness
Just

foreach (TextFragment textFragment in absorber.TextFragments)
textFragment.TextState.BackgroundColor = ColorFabrics(color);
was necessary

Hi Pavel,


Thanks for your inquiry. You can use HighlightAnnotation for the purpose. Please check following documentation link for details, it will help you to accomplish the task.

Document document = new Document(myDir

  • “HelloWorld.pdf”);<o:p></o:p>

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Pdf");

document.Pages.Accept(textFragmentAbsorber);

TextFragmentCollection textFragmentCollection1 = textFragmentAbsorber.TextFragments;

foreach (TextFragment textFragment in textFragmentCollection1)

{

Aspose.Pdf.InteractiveFeatures.Annotations.HighlightAnnotation freeText = new Aspose.Pdf.InteractiveFeatures.Annotations.HighlightAnnotation(textFragment.Page, new Aspose.Pdf.Rectangle((float)textFragment.Position.XIndent,

(float)textFragment.Position.YIndent, (float)textFragment.Position.XIndent + (float)textFragment.Rectangle.Width,

(float)textFragment.Position.YIndent + (float)textFragment.Rectangle.Height));

freeText.Opacity = 0.5;

freeText.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Yellow);

textFragment.Page.Annotations.Add(freeText);

}

document.Save(myDir + "texthighlight_output.pdf");

Please feel free to contact us for any further assistance.


Best Regards,