Q: I'm currently implementing a custom line annotation tool with
the .NET SDK. Using the PDFViewCS project, the viewer only displays
red square. So I save the file and open it in Reader it displays the
line I am expecting. I've tried browsing the online forums but the
examples appear to be out-dated. For instance,
http://groups.google.com/group/pdfnet-sdk/browse_thread/thread/d8e2aa9e3d3b362a/3b76f388bedb1607?lnk=gst&q=Line+Annotation#3b76f388bedb1607
makes references to Rect.CreateSDFRect and Obj.CreateNumber static
methods but this methods are not found. Do you have more recent
information?
-------
A: Please keep in mind that the next major version of PDFNet will
include a number of built-in tools (including AddLine tool) so you
will not need to write much code in order to take advantage of this
functionality. In case you still need to implement a custom tool, you
could do so as in previous version of PDFNet (and as shown in the code
below):
The following is a modified code snippet from (PDFView sample -
MyPDFView.cs) used to implement a custom line annotation tool. This
tool can be used to add PDF line annotations to existing PDF
documents:
protected override void OnMouseUp(MouseEventArgs e) {
if (_tool_mode == CustomToolMode.e_line_annot && Capture &&
_cur_page>0)
{
_end_pt.X = e.X; _end_pt.Y = e.Y;
PDFDoc doc = GetDoc();
doc.Lock();
Page pg = doc.GetPage(_cur_page);
//Calculate annotation position rectangle in PDF page coordinate
system.
Matrix screen2page = GetDeviceTransform(_cur_page);
screen2page.Invert();
PointF[] pt_arr = { _start_pt, _end_pt };
screen2page.TransformPoints(pt_arr);
Rect pos = new Rect(pt_arr[0].Y, pt_arr[0].X, pt_arr[1].Y,
pt_arr[1].X);
pos.Normalize();
// Create line annotation...
Annot kannot = Annot.Create(doc, Annot.Type.e_Line, pos);
// Set basic line annotation information
pdftron.SDF.Obj k = kannot.GetSDFObj();
k.PutNumber("F", 4); // NoZoom
// k.PutBool("Cap", true);
// k.PutText("Contents", "myScale");
// k.PutName("S", "D");
// k.PutText("DS", "font: arial 9pt; text-align:center; line-height:
10.35pt; color:#FF0000");
// k.PutText("Subj", "Length Measurement");
// k.PutString("IT", "LineDimension");
// Set the line coordinates
k.PutRect("L", pt_arr[0].Y, pt_arr[0].X, pt_arr[1].Y, pt_arr[1].X);
// Setting line end type
// pdftron.SDF.Obj le = k.PutArray("LE");
// le.PushBackName("Butt");
// le.PushBackName("Butt");
// pdftron.SDF.Obj lle = k.PutNumber("LLE", 2);
// Setting color (red)
ColorPt red = new ColorPt(1, 0, 0);
kannot.SetColor(red);
// Setting interior color (red)
// pdftron.SDF.Obj ic = k.PutArray("IC");
// ic.PushBackNumber(1);
// ic.PushBackNumber(0);
// ic.PushBackNumber(0);
// Generate the appearance stream for the annotation (recommended).
ElementWriter w = new ElementWriter();
ElementBuilder b = new ElementBuilder();
b.PathBegin();
b.MoveTo(pt_arr[0].Y, pt_arr[0].X);
b.LineTo(pt_arr[1].Y, pt_arr[1].X);
Element element = b.PathEnd();
element.SetPathFill(false);
element.SetPathStroke(true);
GState gs = element.GetGState();
gs.SetStrokeColorSpace(ColorSpace.CreateDeviceRGB());
gs.SetStrokeColor(red);
w.Begin(doc);
w.WriteElement(element);
pdftron.SDF.Obj normal_ap = w.End();
w.Dispose();
b.Dispose();
pdftron.SDF.Obj ap = k.PutDict("AP");
ap.Put("N", normal_ap);
normal_ap.PutRect("BBox", pos.x1, pos.y1, pos.x2, pos.y2);
pg.AnnotPushBack(kannot);
doc.Unlock();
Update();
Capture = false;
_cur_page = 0;
Invalidate();
}
...
}
For the full sample code, please take a look at
PDFViewCustomLineAnnotToolSample.zip in the Files section of the
Forum (http://groups.google.com/group/pdfnet-sdk/files).
In this sample we replaced the 'e_link_create' with 'e_line_annot'
tool. Please note that the toolbar icon for line annotation is still
using the old 'link' icon.