Creating a Highlight Annotation Programmatically

187 views
Skip to first unread message

Matt Parizeau

unread,
Dec 11, 2014, 12:16:12 PM12/11/14
to
Q:

I can create text highlight annotations programmatically by setting the x, y, width and height properties on the annotation, however the fill color doesn't render!

A: 

It turns out it's a little bit trickier than just setting those properties.  Setting the properties you've mentioned almost works, however internally it's missing some extra information we use about the text, for example to highlight multiple lines at a time with a single highlight annotation.

Since text annotations (highlight, underline, strikeout) are associated with text on the page, I've found that the best way to programmatically add them is to simulate the annotation tool's selection with the top left and bottom right points and then having the tool's code figure out how to highlight any text in between.  This way multiple lines will automatically be highlighted with a single annotation.  To do this you can use code like the following:

var docViewer = this.docViewer;
var am = docViewer.GetAnnotationManager();

var topLeft = { x: 200, y: 100, pageIndex: 0};
var bottomRight = { x: 300, y: 200, pageIndex: 0};

var annot = new Annotations.TextHighlightAnnotation();
annot.SetPageNumber(1);
annot.StrokeColor = new Annotations.Color(0, 255, 255);
am.AddAnnotation(annot);

var textHighlightTool = new Tools.TextHighlightCreate(docViewer);
textHighlightTool.annotation = annot;
textHighlightTool.pageCoordinates[0] = topLeft;
textHighlightTool.select(topLeft, bottomRight);

You can do the same thing with other text annotations (underline, strikeout)

Reply all
Reply to author
Forward
0 new messages