Aspose.PDF - PDF highlighting

Hi,

I wanna know if I can add text highlighting to a PDF document through Aspose.PDF.

For example : Its is a dog.

The word dog should be highlighted (Yellow background color)

@Muzna_Tariq

Thanks for contacting support.

In order to add highlighted text inside PDF, there are two ways which can serve the purpose as per your requirement. In case if you wanted to highlight some text while PDF generation, you can simply use BackgroundColor property of TextState to achieve that, whereas in case you want to highlight some text in an existing PDF, you may add highlight annotation at the same position where text exists.

Please check following code snippet where I have shown both ways to add highlighted text inside new/existing PDF. For your reference, I have also attached output(s), generated by below code snippet(s).

Case: New PDF

Document doc = new Document();
doc.Pages.Add();
TextFragment tf = new TextFragment("Its a ");
TextSegment t = new TextSegment("dog.");
t.TextState = new TextState { BackgroundColor = Color.Yellow };
tf.Segments.Add(t);
doc.Pages[1].Paragraphs.Add(tf);
doc.Save(dataDir + "PDF_Highlighting.pdf");

Case: Existing PDF

Document doc2 = new Document(dataDir + "PDF_Highlighting.pdf");
TextFragmentAbsorber tfa = new TextFragmentAbsorber("Its");
doc2.Pages[1].Accept(tfa);
HighlightAnnotation ha = new HighlightAnnotation(doc2.Pages[1], tfa.TextFragments[1].Rectangle);
ha.Color = Color.Yellow;
doc2.Pages[1].Annotations.Add(ha);
doc2.Save(dataDir + "PDF_Highlighting_2.pdf");

Output file(s)

PDF_Highlighting_2.pdf (2.6 KB)
PDF_Highlighting.pdf (1.9 KB)

In case of any further assistance, please feel free to contact us.


Best Regards,
Asad Ali

Thanks, this was helpful.

@Muzna_Tariq

Thanks for your kind feedback.

Please keep using our API and in case of any further assistance, please feel free to contact us.