Q: I am trying to place a shape on the PDFViewCtrl (or PDFViewWPF) and I want it to move with the control as I scroll, zoom, and re-size. How can I do this?
On some viewers, shapes can be placed directly on the DPFViewCtrl, using PDFViewCtrl.GetCanvas(). Doing this means that scrolling will be taken care of automatically. This is possible for example on our WinRT and WPF solutions. If a shape is placed on a canvas, you can translate it’s offset to screen coordinates by using PDFViewCtrl.GetHScrollPos() and PDFViewCtrl.GetVScrollPos(). That is:
CanvasX = ScreenX + PDFViewCtrl.GetHScrollPos()
CanvasY = ScreenY + PDFViewCtrl.GetVSCrollPos()
Once the shape is added, you can store a point representing the top left corner and, for example, a height and width normalized by zoom factor (e.g. width or height at zoom factor 1).
Assuming your shape is somewhere on page 5, then you can get the page point as follows:
double x = ScreenX;
double y = ScreenY;
PDFViewCtrl.ConvScreenPtToPagePt(ref x, ref y, 5);
PageX = x;
PageY = y;
When the control has been scaled or re-sized (or scrolled if you don’t have access to the canvas) you can do the following:
double x = PageX;
double y = PageY;
PDFViewCtrl.ConvPAgePtToScreeenPt(ref x, ref y, 5);
ScreenX = x;
ScreenY = y;
// get the new width and height using the new zoom factor.
They way to receive notifications for when the control has re-sized or zoomed will vary depending on platform. For example, in WinRT and WPF, you can subscribe to the PDFViewCtrl’s (PDFViewWPF’s) events directly, while on Android you have to create a custom tool.
A: On all our platforms, the most reliable way to do this is to store the widget’s position relative to the underlying document. For example, if you want a rectangle to black out or highlight a specific part of a page, you can store the rectangle’s top left corner in page space and then each time the control is scrolled, zoomed, or re-sized, you can calculate the new position in screen space and move your shape there.