使用CGPDF滚动到特定页面

时间:2016-03-06 13:31:50

标签: ios objective-c xcode cgpdf

过去两天我很震惊,我没有找到任何与CGPDF有关的文件。我尝试了所有可能的方式,但我失败了。这是我想要做的。我有一个PDF显示,我在UIWebView中显示它。我使用pdf所在的路径(NSURL)创建了一个CORE GRAPHICS PDF文档参考。我在它上面创建了一个UIView并处理了一个触摸事件。当加载PDF并且用户单击视图时,我希望页面滚动到特定页面。我知道这可以通过计算页面高度和宽度来完成。我想知道CGPDF中是否有传递页码的方式,并滚动到相关页面。以下是我的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    // set the pdfPafeHeight to -1 so it gets calculated.
    self.pdfPageHeight = -1;

    // set the delegate of the UIWebView's underlying UIScrollView to self.
    self.webView.scrollView.delegate = self;

    // create an NSURLRequest to load the PDF file included with the project
    _filePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
    _url = [NSURL fileURLWithPath:_filePath isDirectory:NO];
    _urlRequest = [NSURLRequest requestWithURL:_url];

    // create a Core Graphics PDF Document ref using the same NSURL
     _pdf = CGPDFDocumentCreateWithURL((CFURLRef) _url);

    // use CGPDFDocumentGetNumberOfPages to get the number of pages in the document
    self.pdfPageCount = (int)CGPDFDocumentGetNumberOfPages(_pdf);

    // load the PDF file into the UIWebVie

    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    [self.view addGestureRecognizer:singleFingerTap];

    [self.webView loadRequest:_urlRequest];
}

这就是我如何想要单手操作。我只是想知道当我向其传递页码时,CGPDF中是否有任何方法可以滚动到特定页面

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {


}

1 个答案:

答案 0 :(得分:1)

你想要实现的目标没有API,即使你能够使用私有API,如果这个应用程序仅用于Enterprise安装而且不需要去App Store - 这将是棘手的任何解决方法都可能会因主要的iOS版本更新而中断。如果你想走这条路,你可以反省视图控制器层次结构,并会发现Apple内部有一个名为CorePDF的框架,它有一个你可能能够访问的内部私有API - 但要小心并知道这不是App Store应用程序的解决方案。

另一种方法是从头开始并使用CGContextDrawPDFPage绘制PDF页面,构建自己的缓存,滚动视图和查看控制器+手势处理。我们用自2010年开始研究的商业PSPDFKit SDK做到了这一点 - 这比你想象的要多得多。我们最终还是继续使用自定义渲染器来提高与各种可用PDF文件的兼容性,并提供比Apple渲染器更好的性能,但并非每个用例都需要这样。