How do I create a line annotation and set its appearance?

已查看 402 次
跳至第一个未读帖子

Support

未读,
2007年11月27日 20:41:572007/11/27
收件人 PDFTron PDFNet SDK
Q: We are working on creating Line Annot from pdftron library. For the
same we need to create Appearance Stream. Could you please provide us
with some sample code for

1. Setting the line color
2. To draw a line on given page in the apearance stream

-------------

A:
Like with other annotation types you can build the appearance stream
for line annotation using ElementBuilder/ElementWriter. To set the
resulting appearance use annot.SetAppearance(appearance_stream).

For example:

// Create 'line' annotation -------------------------
Obj line_annot = doc.CreateIndirectDict();
line_annot.Put("Subtype", Obj.CreateName("Line"));
line_annot.Put("Open", Obj.CreateBool(false));

line_annot.Put("Contents", Obj.CreateString("This is a Line
Annotation"));
line_annot.Put("Rect", Rect.CreateSDFRect(300, 200, 600, 700));

// Setting color
Obj annot_color = Obj.CreateArray();
line_annot.Put("C", annot_color);
annot_color.PushBack(Obj.CreateNumber(0.8));
annot_color.PushBack(Obj.CreateNumber(0.3));
annot_color. PushBack(Obj.CreateNumber(0.4));

// Setting Line Coordinates
Obj coords = Obj.CreateArray();
line_annot.Put("L", coords);
coords.PushBack(Obj.CreateNumber(300));
coords.PushBack(Obj.CreateNumber(200));
coords.PushBack(Obj.CreateNumber(600));
coords.PushBack(Obj.CreateNumber(700));


// Create appearance stream for the annotation --------
ElementWriter writer = new ElementWriter();
ElementBuilder eb = new ElementBuilder();
writer.Begin(doc);

eb.PathBegin(); // start constructing the path
eb.MoveTo(300, 200);
eb.LineTo(600, 700);
Element element = eb.PathEnd();
element.SetPathFill(false);
element.GetGState().SetStrokeColorSpace(ColorSpace.CreateDeviceRGB());
element.GetGState().SetStrokeColor(new ColorPt(1, 0, 0)); // red
writer.WriteElement(element);
Obj appearance_stream = writer.End();

// Set annotation appearance...
myannot.SetAppearance(appearance_stream);

//-----------------------------------------------------
// Append the annotation to the first page...
Page page = doc.PageBegin.Current();
Obj annots = page.GetAnnots();
annots.PushBack(line_annot);
回复全部
回复作者
转发
0 个新帖子