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

时间:2013-10-07 17:59:23

标签: ios pdf uiwebview

我正在开发一个应用程序,它显示嵌入在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];
}

现在我想触摸一个按钮并将我加载的PDF更改为某个页面,我正在寻找能够实现此目的的东西,但仍无法正常工作

提前感谢支持

5 个答案:

答案 0 :(得分:2)

此时我认为您可以使用两种方法:

  1. 使用scrollView“浏览PDF:

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

    //例如,跳转到页面高度为1000像素的PDF文档中的第5页:

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

答案 1 :(得分:2)

它很晚但可能会有帮助,

对于低于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)

    }

适用于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)
        }


    }

答案 2 :(得分:1)

据我所知,Safari Kit不支持命名目的地(RFC 3778)。换句话说,如果您尝试这样做:

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

,它将无法正常工作。

据我所知,您跳转到PDF页面的唯一机会是使用Reader或其他类似的框架。

答案 3 :(得分:1)

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

第一种方式:

//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;
}

第二种方式:

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

答案 4 :(得分:-1)

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)