简体   繁体   中英

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];

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM