Is this an efficient method? Is there a better way?
Answer:
Yes, you can do this, though this is a more lightweight, preferred way.
ObjSet obj_set=new ObjSet();
Obj my_dict = obj_set.CreateDict();
If you ever want to add the object to a document, then you would have to call
Obj imported = SDFDoc.ImportObj(my_dict, true);
For example, here we use the same custom dictionary in two different documents.
ObjSet obj_set = new ObjSet();
Obj my_dict = obj_set.CreateDict();
my_dict.PutNumber("k1", 55.0);
my_dict.PutString("k2", "my_string_value");
using (PDFDoc doc = new PDFDoc(input_path + "numbered.pdf"))
{
doc.InitSecurityHandler();
Obj imported = doc.GetSDFDoc().ImportObj(my_dict, true);
doc.GetRoot().Put("test_dict", imported);
doc.Save(output_path + "annotation_test2.pdf", SDFDoc.SaveOptions.e_linearized);
}
using (PDFDoc doc = new PDFDoc(input_path + "numbered.pdf"))
{
doc.InitSecurityHandler();
Obj imported = doc.GetSDFDoc().ImportObj(my_dict, true);
doc.GetRoot().Put("test_dict", imported);
doc.Save(output_path + "annotation_test3.pdf", SDFDoc.SaveOptions.e_linearized);
}