有没有办法以编程方式滚动到UIWebView中的PDF页面? [英] Is there a way to programmatically scroll to a PDF page within a UIWebView?

查看:154
本文介绍了有没有办法以编程方式滚动到UIWebView中的PDF页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用JavaScript设置 UIWebView 的像素y偏移量,例如:

It is possible to use JavaScript to set the pixel y-offset of a UIWebView, e.g.:

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"scrollTo(0, %d)", offset]];

所以有办法获得:


  1. 网页视图中PDF的单个页面的像素高度?

  2. 页面之间差距的大小?

这些信息是否来自 UIWebView ,还是可以通过其他方式计算出来?

Is this information available from the UIWebView or can it be calculated through alternative means?

我想如果我有页数(通过 CGPDFDocumentGetNumberOfPages ),像素高度页面之间的间隙,或单个页面的像素高度,我可以计算用于JavaScript调用的 offset 。然后我只需连接一个 UIButton UISlider 来在页面之间移动。

I'm thinking that if I have the number of pages (via CGPDFDocumentGetNumberOfPages), the pixel height gap between pages, or the pixel height of an individual page, I can calculate the offset to use with the JavaScript call. Then I just wire up a UIButton or UISlider to move between pages.

编辑我

我有一个解决方案,但它使用 UIWebDocumentView UIWebView的私人子视图

I have a solution, but it uses UIWebDocumentView, a private subview of UIWebView.

我创建了一个视图控制器名为 PDFViewerViewController ,它是 WebViewerViewController 的子类,它本身是一个包含<$ c的视图控制器$ c> UIToolbar ,一个 UIWebView ,并且符合 UIWebViewDelegate 协议。

I create a view controller called PDFViewerViewController, which is a subclass of WebViewerViewController, which itself is a view controller that contains a UIToolbar, a UIWebView, and conforms to the UIWebViewDelegate protocol.

我的 PDFViewerViewController 在Web视图委托方法<$ c之后计算有关封闭Web视图和PDF数据的一些信息$ c> -webViewDidFinishLoad:被调用。

My PDFViewerViewController calculates some information about the enclosing web view and the PDF data, after the web view delegate method -webViewDidFinishLoad: gets called.

此信息用于计算通过JavaScript提供给网页视图的大致每页偏移量。

This information is used to calculate an approximate per-page offset that gets fed to the web view via JavaScript.

PDFViewerViewController.h

#import <UIKit/UIKit.h>
#import "WebViewerViewController.h"

@interface UIWebDocumentView : NSObject {}
@end

@interface PDFViewerViewController : WebViewerViewController {
    NSUInteger offset;
    NSUInteger currentPage;
    NSUInteger documentPages;
    CGFloat documentHeight;
    CGFloat pageHeight;
}

@property (assign) NSUInteger offset;
@property (assign) NSUInteger currentPage;
@property (assign) NSUInteger documentPages;
@property (assign) CGFloat documentHeight;
@property (assign) CGFloat pageHeight;

@end

PDFViewerViewController.m

#import "PDFViewerViewController.h"

@implementation PDFViewerViewController

@synthesize offset;
@synthesize currentPage;
@synthesize documentPages;
@synthesize documentHeight;
@synthesize pageHeight;

- (void) viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *_leftArrow = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ArrowLeft.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftArrow:)];
    UIBarButtonItem *_flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *_rightArrow = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ArrowRight.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightArrow:)];
    [[super.viewerToolbarView toolbar] setItems:[NSArray arrayWithObjects:_leftArrow, _flexibleSpace, _rightArrow, nil]];
    [_leftArrow release];
    [_flexibleSpace release];
    [_rightArrow release];

    self.currentPage = 0;
}

- (void) webViewDidFinishLoad:(UIWebView *)_webView {
    for (UIView *_subview in [[[_webView subviews] objectAtIndex:0] subviews]) {
        if ([_subview isKindOfClass:[UIWebDocumentView class]]) {
            self.documentHeight = _subview.bounds.size.height;
        }
    }

    CGPDFDocumentRef pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef)baseURL);
    self.documentPages = CGPDFDocumentGetNumberOfPages(pdfDocument);
    CGPDFDocumentRelease(pdfDocument);

    self.pageHeight = (self.documentHeight + (10 * self.documentPages)) / self.documentPages;
    self.currentPage = 1;
    self.offset = 0;
}

- (void) leftArrow:(id)_param {
    if (self.currentPage == 1) 
        return;
    self.offset -= (NSUInteger)self.pageHeight;
    self.currentPage--;
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"scrollTo(0, %d)", (self.offset * 2)]];
}

- (void) rightArrow:(id)_param {
    if (self.currentPage == self.documentPages) 
        return;
    self.offset += (NSUInteger)self.pageHeight;
    self.currentPage++;
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"scrollTo(0, %d)", (self.offset * 2)]];
}

一些观察结果


  1. 偏移计算不是页面完美的。如果PDF文档不是8.5 x 11(例如A4),则偏移误差会更快地恶化。

  1. The offset calculation isn't page-perfect. If the PDF document isn't 8.5 x 11 (e.g. A4) then the offset error gets worse more quickly.

self。通过触摸拖动滚动浏览Web视图时,不会更新currentPage 属性。可能会拖动几页,然后触摸工具栏上的左箭头或右箭头会导致偏移量意外移动到上一页。

The self.currentPage property doesn't get updated when scrolling through the web view by way of touch-drag. One might drag a few pages, and then touching the left or right arrow on the toolbar will cause the offset to unexpectedly move to a previous page.

此解决方案使用 UIWebDocumentView ,这是私有的,可能会导致应用拒绝。

This solution uses UIWebDocumentView, which is private and may likely cause app rejection.

我想我会向Apple提交功能增强请求。

I think I'll file a feature enhancement request with Apple.

有没有人构建一个非 UIWebView 的PDF查看器(带源代码)?

Has anyone built a non-UIWebView-based PDF viewer (with source code)?

推荐答案

所以这篇文章已经开放了很长一段时间但是因为它是谷歌搜索滚动pdf uiwebview的第一个条目,我想我为未来的读者提供我的看法。

So this post has been open for quite a while now but since it's the first entry on a Google search for "scroll pdf uiwebview" I thought I'd provide my take at this for future readers.

您确实可以使用纯Java脚本滚动到PDF文档中的特定页面。我在我们的博客上发布了示例代码( http:// apptech .next-munich.com / 2011/02 / pdf-in-uiwebview.html )但这里是您需要做的简短摘要:

You can indeed scroll to a particular page in a PDF document using nothing but pure Java Script. I've posted sample code on our blog (http://apptech.next-munich.com/2011/02/pdf-in-uiwebview.html) but here's a short summary of what you need to do:


  1. 使用window.outerHeight DOM属性查找呈现的PDF文档的总高度。

  2. 使用window.innerHeight DOM属性查找浏览器的高度浏览器坐标中的窗口。

  3. 使用window.scrollTo(x,y)函数跳转到页面。

但是,有一个问题,你不能只通过页数来设置outerHeight并跳转到你得到的内容。相反,您必须乘以innerHeight / webView.bounds.size.height比率。

There is one catch, though, that you cannot just device the outerHeight by the number of pages and jump to what you get. Instead, you will have to multiply by the innerHeight/webView.bounds.size.height ratio.

这篇关于有没有办法以编程方式滚动到UIWebView中的PDF页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆