Q:
How to I create a Highlight, Underline, Squiggly, or StrikeOut
annotation using PDFNet?
-----
A:
The following code illustrates how to create highlight annotation.
Other annotations types can be created similarly (the difference being
only the annotation type in the call to Annot.Create() method):
// Use PDFNet to generate appearance stream for highlight
annotation.
static Obj CreateHighlightAppearance(PDFDoc doc, Rect bbox, ColorPt
higlight_color)
{
// Create a button appearance stream
------------------------------------
ElementBuilder build = new ElementBuilder();
ElementWriter writer = new ElementWriter();
writer.Begin(doc);
// Draw background
Element element = build.CreateRect(bbox.x1 - 2, bbox.y1, bbox.x2 +
2, bbox.y2);
element.SetPathFill(true);
element.SetPathStroke(false);
GState gs = element.GetGState();
gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
gs.SetFillColor(higlight_color);
gs.SetBlendMode(GState.BlendMode.e_bl_multiply);
writer.WriteElement(element);
Obj stm = writer.End();
// Set the bounding box
stm.Put("BBox", Rect.CreateSDFRect(bbox));
stm.Put("Subtype", Obj.CreateName("Form"));
return stm;
}
// Create Highlight Annotation.
static Annot CreateHighlightAnnot(PDFDoc doc, Rect bbox, ColorPt
highlight_color)
{
Annot a = Annot.Create(doc, Annot.Type.e_Highlight, bbox);
a.SetColor(highlight_color);
a.SetAppearance(CreateHighlightAppearance(doc, bbox,
highlight_color));
Obj quads = Obj.CreateArray();
a.GetSDFObj().Put("QuadPoints", quads);
quads.PushBack(Obj.CreateNumber(bbox.x1));
quads.PushBack(Obj.CreateNumber(bbox.y2));
quads.PushBack(Obj.CreateNumber(bbox.x2));
quads.PushBack(Obj.CreateNumber(bbox.y2));
quads.PushBack(Obj.CreateNumber(bbox.x1));
quads.PushBack(Obj.CreateNumber(bbox.y1));
quads.PushBack(Obj.CreateNumber(bbox.x2));
quads.PushBack(Obj.CreateNumber(bbox.y1));
return a;
}