How to use PDFView/PDFViewCtrl to focus on a point after zooming

Q: How can I make PDFView/PDFViewCtrl focus on a specific point after zooming?

A:

PDFView and PDFViewCtrl offers convenient function SetZoom(int x, int y, double zoom) for this purpose. It sets a new zoom factor using (x,y) as the zoom center, which will stay fixed on the screen. The zoom center (x,y) is represented in the client coordinate system, which starts in the upper-left corner of the client window. Should you want to move (x, y) to the center of the client window after zooming, you can use OnScroll() subsequently:

int x, y;

double zoom;

PDFView view;

//set up view, ( x, y) and zoom

view.SetZoom(x, y, zoom);

int width = view.GetBufferWidth();

int height = view.GetBufferHeight();

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

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

view.OnScroll(dx, dy);