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

Free Support Forum - aspose.com

Error on pdf highlighted

hi we try your library for highlight some words in pdf everythings works well but on some pdf we have an error message on pdf open i attach the two pdf one is before “test.pdf” and one is after “output.pdf” also i attach the error message when i open “output.pdf” the words highlighted is “adidas”



best regards

Hi Francisco,


Thanks for your inquiry. I have tested your scenario using Aspose.Pdf for .NET 10.0.0 over Win 7 64 bit with VS2010, Please use following code snippet for the purpose. If issue persist then please share some more details to replicate the issue at our end.

Document document = new Document(myDir

  • “test (8).pdf”);<o:p></o:p>

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("(?i)adidas");

//set text search option to specify regular expression usage

TextSearchOptions textSearchOptions = new TextSearchOptions(true);

textFragmentAbsorber.TextSearchOptions = textSearchOptions;

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,

i try your code with
win7 machine and vs2008 with 3.5 framework
and everythings seems to working well
did you have the same code in vb ?
my project is written in vb and i have afraid to write wrong code in traduction from another language

best regards
Frnacesco

Hi Frnacesco,


Thanks for your feedback. It is good to know that sample code snippet helped you to accomplish the task. Please check VB code snippet for searching and highlight the text.

Dim document As New Document(myDir + “test (8).pdf”)<o:p></o:p>

Dim textFragmentAbsorber As New TextFragmentAbsorber("(?i)adidas")

'set text search option to specify regular expression usage

Dim textSearchOptions As New TextSearchOptions(True)

textFragmentAbsorber.TextSearchOptions = textSearchOptions

document.Pages.Accept(textFragmentAbsorber)

Dim textFragmentCollection1 As TextFragmentCollection = textFragmentAbsorber.TextFragments

For Each textFragment As TextFragment In textFragmentCollection1

Dim freeText As New Aspose.Pdf.InteractiveFeatures.Annotations.HighlightAnnotation(textFragment.Page, New Aspose.Pdf.Rectangle(textFragment.Position.XIndent, textFragment.Position.YIndent, textFragment.Position.XIndent + textFragment.Rectangle.Width, textFragment.Position.YIndent + textFragment.Rectangle.Height))

freeText.Opacity = 0.5

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

textFragment.Page.Annotations.Add(freeText)

Next

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

Please feel free to contact us for any further assistance.


Best Regards,

perfect !

everythings works.
A last question if i want to add more than one keywords to highlight?
how can change the code ?

best regards
Francesco

Hi Francisco,


Thanks for your feedback. Please note to highlight more than one keywords you can use a string array to keep keywords and loop through the array values as following. Hopefully it will help you to accomplish the task.

Dim keyWords As String() = New String() {“word1”,
“word2”}<o:p></o:p>

Dim document As New Document("input.pdf")

For Each value As String In keyWords

Dim key As String = "(?i)" & value

Dim textFragmentAbsorber As New TextFragmentAbsorber(key)

'set text search option to specify regular expression usage

Dim textSearchOptions As New TextSearchOptions(True)

textFragmentAbsorber.TextSearchOptions = textSearchOptions

document.Pages.Accept(textFragmentAbsorber)

Dim textFragmentCollection1 As TextFragmentCollection = textFragmentAbsorber.TextFragments

For Each textFragment As TextFragment In textFragmentCollection1

Dim freeText As New Aspose.Pdf.InteractiveFeatures.Annotations.HighlightAnnotation(textFragment.Page, New Aspose.Pdf.Rectangle(textFragment.Position.XIndent, textFragment.Position.YIndent, textFragment.Position.XIndent + textFragment.Rectangle.Width, textFragment.Position.YIndent + textFragment.Rectangle.Height))

freeText.Opacity = 0.5

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

textFragment.Page.Annotations.Add(freeText)

Next

Next

document.Save("output.pdf")


Please feel free to contact us for any further assistance.


Best Regards,

i found a solution by myself i use regular expression and passing this phrase to library i give you an example
"(?i)adidas | (?)reebook" .

the procedure seems to working well, is it the same ? the code is more readable in this mode but if is not the same i change my code .

best regards
Francesco

Hi Francesco,


Thanks for your inquiry. It is good to know you have also find the workflow. Both solutions are fine, using a valid regular expression. You can use either of these for your requirements.

Please feel free to contact us for any further assistance.

Best Regards,