Q: How do I create an keep track of a specific element on a page?
For example, let's say I want to create a path that I later might want to edit as a response to other changes.
A: Since paths aren't objects of themselves, you cannot add any custom entry or key by which to identify it later.
You can, however, create it as a Form XObject, give it a unique key and then reference this from the path you created.
Created the Form XObject is very similar to creating a regular object, it's just that you create it on the doc as opposed to the page.
Obj CreateMyFormXObject(string key, string val)
{
ElementWriter writer = new ElementWriter();
ElementBuilder eb = new ElementBuilder();
writer.Begin(doc);
eb.PathBegin();
eb.MoveTo(0, 0);
eb.CurveTo(500, 500, 125, 625, 0, 500);
eb.CurveTo(-125, 625, -500, 500, 0, 0);
Element heart = eb.PathEnd();
heart.SetPathFill(true);
heart.GetGState().SetFillColorSpace(ColorSpace.CreateDeviceRGB());
heart.GetGState().SetFillColor(new ColorPt(1, 0, 0));
writer.WriteElement(heart);
Obj form_xobj_dict = writer.End();
form_xobj_dict.Put("Subtype", Obj.CreateName("Form")); // Set bounding box/clipping rectangle.
form_xobj_dict.Put("BBox", Rect.CreateSDFRect(0, 0, 400, 600));
// Add a custom key and value:
form_xobj_dict.PutString(key, val);
return form_xobj_dict;
}
More info about creating and processing Form XObjects
More info about custom entries:
ElementWriter pageWriter = new ElementWriter();
ElementBuilder pageBuilder = new ElementBuilder();
pageWriter.Begin(page);
Obj form_xobj_dict = CreateMyFormXObject("MyUniqueKey", "MyUniqueObject1");
Element element = pageBuilder.CreateForm(form_xobj_dict);
pageWriter.WritePlacedElement(element);
pageWriter.End()
While looping through the elements, if the element is of type e_form, you can call element.GetXObject to get an pdftron.SDF.Obj object. To check if it's the object you created, you can use the following code:
try
{
DictIterator ditr = obj.Get("MyUniqueKey");
Obj val = ditr.Value();
string str = val.GetAsPDFText();
}