Muhammad Irfan
Muhammad Irfan

Reputation: 280

PDFKit: How to move current page in PDFView to a specific offset

Assuming there is only one page in the PDF. What I am trying to achieve: Save Zoom and offset of a PDFPage currently being viewed and show the page at exact same offset and zoom level when user comes back to that page. What I have achieved: Calculated the offset and zoom and on page reload, successfully shown saved zoom level of page. Iam unable to set the offset. Tried using following methods, but no effect.

1)

[_pdfView goToRect:rect onPage:[_pdfView.document pageAtIndex: page.unsignedLongValue ]];

2)

PDFDestination *destination =  [_pdfView.currentDestination initWithPage:[_pdfView.document pageAtIndex: page.unsignedLongValue ] atPoint:viewPoint];
    [_pdfView goToDestination:destination];

Upvotes: 3

Views: 1907

Answers (1)

nc2000
nc2000

Reputation: 171

I was able to accomplish this using goToRect. I think there are issues with goToDestination. It always navigates to a different location than I pass into it.

Here's what I do to save the location when exiting the pdf (please note that I translated this code from Swift and haven't tested in Objective C):

    CGFloat savedScaleFactor = _pdfView.scaleFactor;
    //I use this to find the first page shown in my view. If you only have one page,
    //you can just use currentPage
    PDFPage *page = [_pdfView pageForPoint:CGPointZero nearest:YES];
    CGRect savedRect = [_pdfView convertRect:_pdfView.bounds toPage:page];
    NSInteger savedIndex = [_pdfView.document indexForPage:page];

Then to restore the location:

    _pdfView.scaleFactor = savedScaleFactor;
    PDFPage *page = [_pdfView.document pageAtIndex:savedIndex];
    [_pdfView goToRect:savedRect onPage:page];

Upvotes: 4

Related Questions