How to control page display to PDF loaded in UIWebView for iOS (go to page)

前端 未结 5 849

I\'m developing an app which displays a PDF embedded in a WebView this is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *urlremoto = [NS         


        
5条回答
  •  再見小時候
    2020-12-30 16:16

    just as RFG said,using webview's scrollview is perfect,and the key step is to find out the height of pdf page,the follow code will get the whole height of pdf file and page num of pdf file .and then we got it!

    First Way:

    //get the total height
    CGFloat pageHeight = self.webView.scrollView.contentSize.height;
    //get page nums
    -(NSInteger)getTotalPDFPages:(NSString *)strPDFFilePath
    {
        NSURL *pdfUrl = [NSURL fileURLWithPath:strPDFFilePath];
        CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
        size_t pageCount = CGPDFDocumentGetNumberOfPages(document);
        return pageCount;
    }
    

    Second Way:

    //get the content info 
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        [self.okBtn setEnabled:YES];
        [self.signPdfBtn setEnabled:YES];
        NSArray* a = [self.webView.scrollView subviews];
        for (UIView* view in a) {
            if ([view isKindOfClass:NSClassFromString(@"UIWebPDFView")]) {
                NSArray* b = view.subviews;
                self.content_hetght = view.bounds.size.height;
                self.content_num = b.count;
                }
        }
    }
    //then you can jump any page,like the last page
    [self.webView.scrollView setContentOffset:CGPointMake(0, (self.content_num-1)*(self.content_hetght*1.0 /self.content_num)) animated:YES];
    

提交回复
热议问题