How to let PDFView/PDFViewCtrl focus to a point on a page

Q: How can I make PDFView/PDFViewCtrl focus to a point on a specified page

A:

PDFView/PDFViewCtrl defines several spaces and it is important to understand their differences:

  • Page Space refers to the space in which a PDF page is defined. It is determined by

a page itself and the origin is at the lower-left corner of the page. Note that Page

Space is independent of how a page is viewed in PDFView and each page has its own Page

space.

  • Canvas Space refers to the tightest axis-aligned bounding box of all the pages given

the current page presentation mode in PDFView. For example, if the page presentation

mode is e_single_continuous, all the pages are arranged vertically with one page in each

row, and therefore the Canvas Space is rectangle with possibly large height value. For

this reason, Canvas Space is also, like Page Space, independent of the zoom factor. Also

note that since PDFView adds gaps between adjacent pages, the Canvas Space is larger than

the space occupied by all the pages. The origin of the Canvas Space is located at the

upper-left corner.

  • Screen Space (or Client Space) is the space occupied by PDFView and its origin is at

the upper-left corner. Note that the virtual size of this space can extend beyond the

visible region.

  • Scrollable Space is the virtual space within which PDFView can scroll. It is determined

by the Canvas Space and the current zoom factor. Roughly speaking, the dimensions of the

Scrollable Space is the dimensions of the Canvas Space timed by the zoom. Therefore, a large

zoom factor will result in a larger Scrollable region given the same Canvas region. For this

reason, Scrollable Space might also be referred to as Zoomed Canvas Space. Note that since

PDFView adds gaps between pages in Canvas Space and these gaps are not scaled when rendered,

the scrollable range is not exactly what the zoom factor times the Canvas range. For

functions such as SetHScrollPos(), SetVScrollPos(), GetCanvasHeight(), and

GetCanvasWidth(), it is the Scrollable Space that is involved.

With this being said, you can achieve this goal by using the following code snippet:

//focus to the page point

view.SetCurrentPage(page_num);

view.ConvPagePtToScreenPt(px, py, page_num);

view.SetZoom((int)px, (int)py, view.GetZoom());

//make it at the center of the screen

int width = view.GetBufferWidth();

int height = view.GetBufferHeight();

int dx = (int)(px - (double)width/2 + 0.5);

int dy = (int)(py - (double)height/2 + 0.5);

view.OnScroll(dx, dy);