1

I'm using the Android version of PDFNet 6.5.0.

I need to set the vertical scroll of the PDFViewCtrl to an absolute point in the pdf, expressed in PDF Canvas coordinates.

For example, say that I want to zoom to the middle of the second page.

I could get the y coordinate, in PDF Canvas position like:

int y = doc.getPage(1).getPageHeight() + (doc.getPage(2).getPageHeight() / 2)

How could I scroll to the y position?

I'm trying to do it with PDFViewCtrl#setVScrollPos(), but I don't know how to convert y to a valid parameter for this method.

GaRRaPeTa
  • 5,459
  • 4
  • 37
  • 61

2 Answers2

1

Have you tried to use PDFViewCtrl#scrollTo(int, int)?

i.e. first convert points from PDF space to canvas space(PDFViewCtrl#convPagePtToCanvasPt), then scrollTo the position.

Shirley G
  • 352
  • 2
  • 9
  • You can indeed scroll using PDFViewCtrl#scrollTo(int, int) or PDFViewCtrl#setVScrollPos(int, int). The tricky thing that I was missing is that apart of needing to convert page position to screen position, you need to add the current scroll position to the point you are scrolling to. – GaRRaPeTa Sep 03 '15 at 09:12
1

Although I have accepted Shirley G's answer I am posting this other answer for clarification.

The tricky bit is which number to pass as parameter to PDFViewCtrl#setVScrollPos(int) or PDFViewCtrl#scrollTo(int, int)

  • You need to use PDFViewCtrl#convPagePtToScreenPt() to convert the page position into a physical screen point.

  • But you also need to take into account the current scroll offset of the scroll view pdfViewCtrl#getVScrollPos()

So, given you want to scroll to verticalPosition in page:

public void scrollToDocPosition( final int verticalPosition, final int page) {

            double[] screenPos = pdfViewCtrl.convPagePtToScreenPt(0, verticalPosition, page);
            pdfViewCtrl.setVScrollPos((int) screenPos[1] + pdfViewCtrl.getVScrollPos());

    }

I came to this thanks to https://groups.google.com/forum/?fromgroups#!searchin/pdfnet-sdk/scroll/pdfnet-sdk/7Wy00goWfyQ/t1tvApNDrr8J, where Thomas Hoffman explains:

The first thing to look at is the scroll position. y after converting is in screen space, while SetX/YScrollPos is in the coordinates of the scroll viewer. That is, the top left corner of the PDFViewCtrl is at position (0, 0) in screen space, but at position (GetHScrollPos and GetVScrollPos) in scroll coordinates

GaRRaPeTa
  • 5,459
  • 4
  • 37
  • 61