Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 909
Muhammad Irfan Avatar asked Oct 23 '25 06:10

Muhammad Irfan


1 Answers

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];
like image 105
nc2000 Avatar answered Oct 24 '25 20:10

nc2000