How do I create or process existing digital signature annotations?

Q:

How do I create or process digital signature annotations using PDFNet?
----
A:
The process of creating digital signature annotations is similar to
the one used to create other types of widgets (e.g. see
InteractiveForms sample project). For example:

// Assuming C# pseudocode ----------
Obj sig_dict = pdfdoc.CreateIndirectDict();
... fill our signature dictionary...
... for more information, please see Table 8.102 in PDF Reference
1.7...

// Create a signature field.
Field sig = pdfdoc.FieldCreate("MySig", Field.Type.e_signature,
sig_dict));

// Create a signature annotation
Annot sig_annot = Annot.CreateWidget(pdfdoc, new Rect(64, 356, 120,
410), sig);
sig_annot.SetAppearance(CreateSignatureAppearance(pdfdoc),
Annot.AnnotationState.e_normal);

// Add the signature annotation to an existing page...
page.AnnotPushBack(sig_annot);
//-----------------------------------

CreateSignatureAppearance is a utility function used to create
signature appearance (e.g. a scanned signature image). It may look
along the following lines:

static Obj CreateButtonAppearance(PDFDoc doc) {
  ElementBuilder build = new ElementBuilder();
  ElementWriter writer = new ElementWriter();
  writer.Begin(doc);

  Image img = Image.Create(doc, "my_signature.png");
  int w=img.GetImageWidth(), h=img.GetImageHeight();
  Element img_element = build.CreateImage(img, 0, 0, w, h);
  writer.WritePlacedElement(img_element);

   Obj stm = writer.End();

   // Set the bounding box
   stm.Put("BBox", Rect.CreateSDFRect(0, 0, w, h));
   stm.Put("Subtype", Obj.CreateName("Form"));
   return stm;
}

Similarly, digital signatures annotations can be accessed like other
widgets (e.g. using pdfdoc.FieldFind()/FieldBeign()/FieldEnd()).

If the field has e_signature type, you can access its Cos/SDF
dictionary using GetSDFObj() method. For example,

//C#
field = pdfdoc.FieldFind("MySig");
...
pdftron.SDF.Obj form_dict = field.GetSDFObj(); // get signature value
DictIterator itr = form_dict.Find("V");
if (itr != form_dict.End()) {
  Obj dig_sig = itr.Value();
  // Access any key/value pair...
  // See Section 8.7 'Digital Signatures' in PDF
  // Reference Manual.
  itr = dig_sig.Find("SubFilter");
  if (itr != dig_sig.DictEnd()) {
  }
  // ...
  itr = dig_sig.Find("XSigner"); // get a custom property
  if (itr != dig_sig.End()) {
    dig_sig.Put("XSigner", Obj.CreateString("foo@bar.com"));
  }
  // ...
}

Or to traverse all digital signatures in the document:

//C#
FieldIterator itr = doc.FieldBegin();
FieldIterator end = doc.FieldEnd();
for( ; itr != end; itr.Next()){
  Field field = itr.Current();
  Console.WriteLine("Field name: {0}", field.GetName());
  Field.FieldType type = field.GetType();
  if (type == Field.FieldType.e_signature) {
  Obj field_dict = field.GetSDFObj();
      // Perform COS/SDF read/write/edit operations on the signature.
  }
}