How to create implement 'Notes' annotation tool using PDFNet SDK?

Q:

One of the requirements for us it to create Notes annot via the UI
where the user clicks on a place on the PDF page and a note annot gets
created right there. In order to do so we need to create PDF
appearance stream for the note icon. Consider the following code:

protected override void OnMouseUp(MouseEventArgs e) {
  Matrix scr2PageMatrix = GetDeviceTransform(_drawingPageNum);
  PointF[] pointArray = { new PointF(e.X, e.Y) };
  scr2PageMatrix.TransformPoints(pointArray);
  CreateTextAnnot(pointArray);
}

void CreateTextAnnot(PointF[] pointArray) {
  //Create the Annot with Annot.CreateText()
  //Set the appearance stream for note. We need help here
}

A:

There are several ways you could create the appearance stream for
'Notes' and other annotation types.

One approach is to build the appearance programmatically using
ElementBuilder and ElementWriter. These are the same classes that can
be used to create new page content or to append new content to
existing PDF pages (see http://www.pdftron.com/net/
samplecode.html#ElementBuilder). For example:

ElementWriter writer = new ElementWriter();
ElementBuilder eb = new ElementBuilder();
writer.Begin(doc);
Image img = Image.Create(doc, "butterfly.png");
Element img_element = eb.CreateImage(img, 0, 0, img.GetImageWidth(),
img.GetImageHeight());
writer.WritePlacedElement(img_element);
...
Obj appearance_stream = writer.End();

// Optional add clip Bbox ...
appearance_stream.Put("BBox", Rect.CreateSDFRect(0, 0, 500, 500));

// Set annotation appearance...
myannot.SetAppearance(appearance_stream);

Another approach is to create the appearance stream directly from a
buffer with PDF content stream. This will work if the content stream
does not depend on additional resources. For example,

String content_str = "0 1 1 rg 0 G 0 i 0.61 w 4 M 0 j 0 J []0 d 11.28
0.54 m 11.70 0.88 11.65 7.15 y 17.69 6.79 17.53 6.54 v 17.44 6.41
11.07 0.36 11.28 0.54 c h B 10.97 0.41 m 11.48 0.41 11.80 1.25 11.79
2.30 c 11.78 3.98 11.79 5.98 y 14.71 5.98 16.36 6.00 v 17.11 6.00
17.61 6.41 17.62 6.84 c 17.62 8.26 17.62 19.65 y 0.38 19.65 l 0.38
0.40 l 10.97 0.42 l B 0 g 4.36 15.91 m 4.23 15.91 4.12 15.75 4.12
15.55 c 4.12 15.36 4.23 15.20 4.36 15.20 c 13.79 15.20 l 13.92 15.20
14.03 15.36 14.03 15.55 c 14.03 15.75 13.92 15.91 13.79 15.91 c 4.36
15.91 l h f 4.36 12.16 m 4.23 12.16 4.12 12.00 4.12 11.81 c 4.12 11.61
4.23 11.45 4.36 11.45 c 13.79 11.45 l 13.92 11.45 14.03 11.61 14.03
11.81 c 14.03 12.00 13.92 12.16 13.79 12.16 c 4.36 12.16 l h f 4.36
8.41 m 4.23 8.41 4.12 8.25 4.12 8.06 c 4.12 7.86 4.23 7.70 4.36 7.70 c
11.27 7.70 l 11.41 7.70 11.51 7.86 11.51 8.06 c 11.51 8.25 11.41 8.41
11.27 8.41 c 4.36 8.41 l h f";

byte[] constentbuf = convert content_str to ASCII byte stream;
Obj appearance_stream = doc.CreateIndirectStream(constentbuf);
appearance_stream.Put("BBox", Rect.CreateSDFRect(0, 0, 18, 20));

// Set annotation appearance...
myannot.SetAppearance(appearance_stream);

The above content stream was extracted from an existing PDF document
with 'Notes' annotation using CosEdit utility (www.pdftron.com/
cosedit).
----

Since you are planning to develop annotation creation tools you may
also want to take a look at the following variation to PDFView sample
project:
  http://www.pdftron.com/zzDF-Da434/PDFNetPDFViewDemo.zip

This sample illustrates:
  - how to create new link annotations (using the tool with the pencil
icon).
  - mouse cursor changes when hovering over link annotations (in 'Pan'
tool mode).
  - link border drawing
  - ZoomRect tool (similar to the one in Acrobat)
  - link navigation (in 'Pan' tool mode), etc...