How can I create and keep my own Objects outside of any document?

,

Question:

I’m trying to find a way to create direct objects from scratch. Essentially what I’d like to do is:

`
var tronObject = new Obj();

tronObject.Put(…)
tronObject.Put(…)
`

// and hang on to obj for an undetermined amount of time…

// then add it to an existing document.

What I’ve come up with is this… and I think it might be “cheating”.

`
var tronSdf = new SDFDoc();
var tronObject = tronSdf.CreateIndirectArray().PushBackDict();

tronObject.Put(…)
tronObject.Put(…)

tronSdf.Dispose();
`

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](https://www.pdftron.com/pdfnet/docs/PDFNet/?topic=html/M_pdftron_SDF_SDFDoc_ImportObj.htm)(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);
}
`