简体   繁体   English

如何控制在UIWebView for iOS中加载的页面显示为PDF(转到页面)

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

I'm developing an app which displays a PDF embedded in a WebView this is my code: 我正在开发一个应用程序,它显示嵌入在WebView中的PDF,这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *urlremoto = [NSURL URLWithString:@"https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/TransitionGuide.pdf"];

    request = [NSURLRequest requestWithURL:urlremoto];

    [self.webView loadRequest:request];
    [self.webView setScalesPageToFit:YES];
}

-(void)webViewDidStartLoad:(UIWebView *)webView{

    [self.activityIndicator startAnimating];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    //TODO: describir el error
    [self.activityIndicator stopAnimating];        
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    [self.activityIndicator stopAnimating];
}

Now I want to touch a button and change my loaded PDF to certain page, I'm looking for something to achieve this but still got nothing working 现在我想触摸一个按钮并将我加载的PDF更改为某个页面,我正在寻找能够实现此目的的东西,但仍然无法正常工作

thanks in advance for the support 在此先感谢您的支持

Its late but may be helpful, 它很晚但可能会有帮助,

For iOS less than 11.0 对于低于11.0的iOS

func webViewDidFinishLoad(_ webView: UIWebView)
    {

        var pdfPageHeight:CGFloat = 0

        let a = webView.scrollView.subviews

        for var view in a
        {
            if view.isKind(of: NSClassFromString("UIWebPDFView")!)
            {
                let b = view.subviews

                for var iview in b
                {
                    if iview.isKind(of: NSClassFromString("UIPDFPageView")!)
                    {
                        pdfPageHeight = iview.bounds.size.height

                        break
                    }
                }
            }
        }

        webView.scrollView.setContentOffset(CGPoint(x: 0, y: CGFloat((pagenumber - 1)   + 1 )*pdfPageHeight), animated: true)

    }

For iOS 11 and greater 适用于iOS 11及更高版本

var pdfdocumentURL: URL?

    @IBOutlet weak var titleLbl: UILabel!
    @IBOutlet weak var pdfview: PDFView!
    var pagenumber = 1



    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.titleLbl.text = titleStr

       let pdfdocument = PDFDocument(url: pdfdocumentURL!)

        pdfview.document = pdfdocument
        pdfview.displayMode = PDFDisplayMode.singlePageContinuous
        pdfview.autoScales = true

        pagenumber = pagenumber - 1
        if let page = pdfdocument?.page(at: pagenumber) {
            pdfview.go(to: page)
        }


    }

At this moment I think you can use two approaches: 此刻我认为你可以使用两种方法:

  1. Use scrollView to 'navigate, through PDF: 使用scrollView“导航,通过PDF:

    [[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES]; [[webView scrollView] setContentOffset:CGPointMake(0,y)animated:YES];

    // For example, jumping to page 5 in a PDF document with 1000 px page height: //例如,跳转到页面高度为1000像素的PDF文档中的第5页:

     int selectedPag = 5; // ie Go to page 5 float pageHeight = 1000.0; // ie Height of PDF page = 1000 px; float y = pageHeight * selectedPag; [[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES]; 
  2. Split PDF individual pages. 拆分PDF单页。

As far as I know, Safari Kit does not support named destinations ( RFC 3778 ). 据我所知,Safari Kit不支持命名目的地( RFC 3778 )。 In other words, if you try this: 换句话说,如果你试试这个:

<a href="http://www.domain.com/file.pdf#page=3">Link text</a>

in Safari, it will not work. 在Safari中,它不起作用。

The only chance for you to jump to a PDF page, as far as I can see, is using a framework like Reader , or other equivalent. 据我所知,你跳转到PDF页面的唯一机会是使用像Reader或其他类似的框架。

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! 正如RFG所说,使用webview的scrollview是完美的,关键的一步是找出pdf页面的高度,下面的代码将获得pdf文件的整个高度和pdf文件的页面数量。然后我们得到它!

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

Swift 3 version: Swift 3版本:

    let selectedPag: CGFloat = 5; // i.e. Go to page 5

    let pageHeight: CGFloat = 1000.0; // i.e. Height of PDF page = 1000 px;

    let y: CGFloat = pageHeight * selectedPag;

    let pageOffset = CGPoint(x: 0, y: y)
    WebViewMore.scrollView.setContentOffset(pageOffset, animated: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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