How do I keep track of my own annotations?


We want to add annotations, and then later on process them. However the document might already have its own annotations. How do we “keep track” our Annotations?


The basic way of tracking annotations is through the Annotations optional ID entry. This needs to be unique for the document, so generating a GUID would suffice.

Setting/Getting GUID of annotation


// and to get back
SDF.Obj obj = annot.GetUniqueID();
if(obj != null && obj.IsString())
Guid guid = new Guid(obj.GetAsPDFText());

Of course, later on finding a specific GUID requires parsing every page and every annotation. If you want to maintain a quick access map, then you can get the Object number of the annotation, and store that.

Storing reference to annotation for quick access

// get the object number
int obj_num = annot.GetSDFObj().GetObjNum();

// following two lines are how to get the annotation back
SDF.Obj obj = pdfdoc.GetSDFDoc().GetObj(obj_num);
Annot annot = new Annot(obj);

If you delete the annotation, using the object number is no longer safe, so remember to remove the reference from your map.

Finally, you can also store whatever meta data you like on your annotation object. I recomend putting all your custom data under a single dictionary.

Setting/Getting Custom Keys on an annotation

SDF.Obj custom_dict = annot.GetSDFObj().PutDict("my_custom_keys");
custom_dict.PutString("my_key", "my_value");

// then to retrieve later
SDF.Obj custom_dict = annot.GetSDFObj().FindObj("my_custom_keys");

For more info on creating, finding and searching keys, see the following.

is “Line” a tool variable?

Thank you for pointing this out. line was a Line class, a subclass of Annot.

I updated the variable name to annot.