How can I build annotation appearances so that the PDF viewer properly renders the annotation?

222 views
Skip to first unread message

Support

unread,
Jun 20, 2007, 1:02:37 PM6/20/07
to PDFTron PDFNet SDK
Q:

We are working on PDF-annotations. We are able to insert annotations
in a PDF document but we are not able to view these annotations in
PDFView sample project. We can view these annotations if the document
is opened in Acrobat. Also if the document is saved from document we
can see the same annotation in PDFTron PDF viewer. I suspect that the
problem is due to missing annotation appearances. How can I build
missing annotation appearances?

----
A:

You can see the line annotation in PDFTron PDFNet viewer after the
file is edited in Acrobat because Acrobat rebuilds the missing
appearance streams during the save operation.

When you create annotation object using PDFNet you can also build its
appearance stream using ElementBuilder/Element writer and then set the
resulting appearance using 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);

Reply all
Reply to author
Forward
0 new messages