iOS - Get page number for canvas Y position

Hi -

Fundamentally, here’s what I want to know:

Given a VScrollPos or Canvas Y Offset, how to I get a page number?

If I scroll to an offset, I can query GetCurrentPage - but I want to get a page number without having to have scrolled to the offset.

In more detail…

I’m trying to implement a Scrubber Bar (a UISlider) to allow easy scrolling through a document.

My slider is configured like so:
self.slider.minimumValue = 0.0;
self.slider.maximumValue = self.pdfView.GetCanvasHeight - self.pdfView.frame.size.height;

As the user slides the slider, I show a thumbnail & label, to preview the page. Upon releasing the slider, the PDF should jump to the page.

My problem is determining that page, given the slider value.

Since the slider’s value should represent some Canvas Y Offset between 0 and the (Canvas Height - Frame Height), I thought I could calculate the page like so (note, I’m using the vertical continuous scrolling presentation mode):

  • (NSInteger)pageForCanvasYOffset:(CGFloat)canvasYOffset

{

PTPDFPoint *canvasPoint = [[PTPDFPoint alloc] initWithPx:self.pdfView.GetCanvasWidth / 2.0 py:canvasYOffset];

PTPDFPoint *screenPoint = [self.pdfView ConvCanvasPtToScreenPt:canvasPoint];

NSInteger page = (NSInteger)[self.pdfView GetPageNumberFromScreenPt:screenPoint.getX y:screenPoint.getY];

return page;

}

However, this function appears to be broken somehow.

As an investigation, I decided to print the following:

  • (void)pdfScrollViewDidScroll:(UIScrollView *)scrollView

{

NSLog(@“GetVScrollPos: %0.1f - GetCurrentPage: %d - pageForCanvasYOffset: %ld”,

self.pdfView.GetVScrollPos,

self.pdfView.GetCurrentPage,

[self pageForCanvasYOffset:self.pdfView.GetVScrollPos]);

}

With results as I scrolled, illustrating the discrepancy:

GetVScrollPos: 0.0 - GetCurrentPage: 1 - pageForCanvasYOffset: 1

GetVScrollPos: 377.0 - GetCurrentPage: 2 - pageForCanvasYOffset: 1

GetVScrollPos: 502.0 - GetCurrentPage: 2 - pageForCanvasYOffset: 2

GetVScrollPos: 807.0 - GetCurrentPage: 2 - pageForCanvasYOffset: 3

GetVScrollPos: 1001.0 - GetCurrentPage: 3 - pageForCanvasYOffset: 3

GetVScrollPos: 1210.0 - GetCurrentPage: 3 - pageForCanvasYOffset: 4

GetVScrollPos: 1582.0 - GetCurrentPage: 3 - pageForCanvasYOffset: -1

GetVScrollPos: 1790.0 - GetCurrentPage: 4 - pageForCanvasYOffset: -1

What am I doing wrong? Thank you for your help!

Hi Brad,

What you have should work, except in the case that you provide a screen point that lands outside of a page, for instance in the gap between two pages. In this case it will return -1, indicating no page. If you get a -1, you might want to look close to this point to see what page is actually nearby. Does this explain what you’re seeing?

Thank you,

James