How can I implement a custom annotation markup tool?

Q: I'm currently implementing a custom line annotation tool with
the .NET SDK. Using the PDFViewCS project, the viewer only displays
red square. So I save the file and open it in Reader it displays the
line I am expecting. I've tried browsing the online forums but the
examples appear to be out-dated. For instance,
makes references to Rect.CreateSDFRect and Obj.CreateNumber static
methods but this methods are not found. Do you have more recent
A: Please keep in mind that the next major version of PDFNet will
include a number of built-in tools (including AddLine tool) so you
will not need to write much code in order to take advantage of this
functionality. In case you still need to implement a custom tool, you
could do so as in previous version of PDFNet (and as shown in the code

The following is a modified code snippet from (PDFView sample -
MyPDFView.cs) used to implement a custom line annotation tool. This
tool can be used to add PDF line annotations to existing PDF

protected override void OnMouseUp(MouseEventArgs e) {
  if (_tool_mode == CustomToolMode.e_line_annot && Capture &&
    _end_pt.X = e.X; _end_pt.Y = e.Y;

    PDFDoc doc = GetDoc();
    Page pg = doc.GetPage(_cur_page);

    //Calculate annotation position rectangle in PDF page coordinate
    Matrix screen2page = GetDeviceTransform(_cur_page);
    PointF[] pt_arr = { _start_pt, _end_pt };
    Rect pos = new Rect(pt_arr[0].Y, pt_arr[0].X, pt_arr[1].Y,

    // Create line annotation...
    Annot kannot = Annot.Create(doc, Annot.Type.e_Line, pos);

    // Set basic line annotation information
    pdftron.SDF.Obj k = kannot.GetSDFObj();
    k.PutNumber("F", 4); // NoZoom
    // k.PutBool("Cap", true);
    // k.PutText("Contents", "myScale");
    // k.PutName("S", "D");
    // k.PutText("DS", "font: arial 9pt; text-align:center; line-height:
10.35pt; color:#FF0000");
    // k.PutText("Subj", "Length Measurement");
    // k.PutString("IT", "LineDimension");

    // Set the line coordinates
    k.PutRect("L", pt_arr[0].Y, pt_arr[0].X, pt_arr[1].Y, pt_arr[1].X);

    // Setting line end type
    // pdftron.SDF.Obj le = k.PutArray("LE");
    // le.PushBackName("Butt");
    // le.PushBackName("Butt");

    // pdftron.SDF.Obj lle = k.PutNumber("LLE", 2);

    // Setting color (red)
    ColorPt red = new ColorPt(1, 0, 0);

    // Setting interior color (red)
    // pdftron.SDF.Obj ic = k.PutArray("IC");
    // ic.PushBackNumber(1);
    // ic.PushBackNumber(0);
    // ic.PushBackNumber(0);

    // Generate the appearance stream for the annotation (recommended).
    ElementWriter w = new ElementWriter();
    ElementBuilder b = new ElementBuilder();
    b.MoveTo(pt_arr[0].Y, pt_arr[0].X);
    b.LineTo(pt_arr[1].Y, pt_arr[1].X);
    Element element = b.PathEnd();
    GState gs = element.GetGState();
    pdftron.SDF.Obj normal_ap = w.End();

    pdftron.SDF.Obj ap = k.PutDict("AP");
    ap.Put("N", normal_ap);
    normal_ap.PutRect("BBox", pos.x1, pos.y1, pos.x2, pos.y2);


    Capture = false;
    _cur_page = 0;

For the full sample code, please take a look at in the Files section of the
Forum (
In this sample we replaced the 'e_link_create' with 'e_line_annot'
tool. Please note that the toolbar icon for line annotation is still
using the old 'link' icon.

Q: Ok, I have implemented a custom PDF Line annotation tool as
suggested and it is working fine. How would you implement circle and
box annotation?
A: For circle and square/box annotation simply replace the code
snippet between w.Begin() and w.End() with the following:

double rx = pos.Width()/2, ry = pos.Height()/2;
Element element = b.CreateEllipse(pos.x1+rx, pos.y1+ry, rx, ry);
//Element element = b.CreateRect(pos.x1, pos.y1, pos.Width(),
  GState gs = element.GetGState();
  gs.SetLineWidth(5); // Stroke thickness

  gs.SetFillColor(new ColorPt(0, 0, 1));