How do I keep track of my own annotations?

267 views
Skip to first unread message

Ryan

unread,
Sep 30, 2016, 3:47:28 PM9/30/16
to pdfne...@googlegroups.com
Question:

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?

Answer:

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
annot.SetUniqueID(Guid.NewGuid().ToString());

// 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.


Claire Poza

unread,
Oct 4, 2017, 3:38:47 PM10/4/17
to PDFTron PDFNet SDK
is "Line" a tool variable?


On Friday, September 30, 2016 at 3:47:28 PM UTC-4, Ryan wrote:
Question:

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?

Answer:

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
annot.SetUniqueID(Guid.NewGuid().ToString());

// 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 = line.GetSDFObj().PutDict("my_custom_keys");

custom_dict
.PutString("my_key", "my_value");

// then to retrieve later

SDF
.Obj custom_dict = line.GetSDFObj().FindObj("my_custom_keys");

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

Ryan

unread,
Oct 4, 2017, 8:31:12 PM10/4/17
to PDFTron PDFNet SDK
Thank you for pointing this out. line was a Line class, a subclass of Annot.

I updated the variable name to annot.
Reply all
Reply to author
Forward
0 new messages