Creating and keeping track of unique element on a page

觀看次數:73 次
跳至第一則未讀訊息

Tomas Hofmann

未讀,
2013年7月22日 下午5:35:5122/7/2013
收件者︰ pdfne...@googlegroups.com
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() 


Later, when we want to edit this path, we can edit this Form XObject instead. To edit the page where this is displayed, check the ElementEdit sample: http://www.pdftron.com/pdfnet/samplecode.html#ElementEdit
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();

        // If str is "MyUniqueObject1" then you can edit obj using standard methods.
}
回覆所有人
回覆作者
轉寄
0 則新訊息