在iOS上使用PDFKit.PDFView打开PDF [英] open PDF with PDFKit.PDFView on iOS

查看:814
本文介绍了在iOS上使用PDFKit.PDFView打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有PDF查看器的应用程序,我尝试使用需要PDFKit.PDFDocument对象的PDFKit.PDFView.

I have an application with PDF viewer, and I try to use PDFKit.PDFView which need to PDFKit.PDFDocument object.

我的代码是:

var pdfview = new PdfKit.PdfView();
var path = NSBundle.MainBundle.PathForResource("Harvard", "pdf");
var urll = new NSUrl(path);
var pdfdoc = new PdfDocument(urll);
pdfview.Document = pdfdoc;

第4行出现异常:

无法初始化类型为'PdfKit.PdfDocument'的实例: 原生的'initWithURL:'方法返回nil

Could not initialize an instance of the type 'PdfKit.PdfDocument': the native 'initWithURL:' method returned nil

我的pdf文件Harvard.pdf处于Resources文件夹中,并通过BundleResource构建动作位于直接的iOS项目目录中.

My pdf file, Harvard.pdf, is in Resources folder and in direct iOS project directory with BundleResource build action.

使用CGPDFDocument可以轻松阅读此PDF文件,但是我需要使用PDFView解决我之前提出的问题 处理在iOS上的PDF查看器中快速单击的链接. 所以,有人可以帮我吗?

this PDF file can be read easily with CGPDFDocument, but I need to use PDFView to solve problem I asked for it before in handle links clicking inside PDF viewer on iOS with swift. So, can anyone help me please?

推荐答案

尝试以下方法检查URL是否返回nil:

Have a try with follow way to check whether URL return nil:

var documentURL = NSBundle.MainBundle.GetUrlForResource("Harvard", "pdf");
var pdfdoc = new PdfDocument(documentURL);

这是完整的示例代码:

var documentURL = NSBundle.MainBundle.GetUrlForResource("MyForm", "pdf");
if (documentURL != null)
{
    var document = new PdfDocument(documentURL);
    //var page = document.GetPage(0);

    var pdfview = new PdfKit.PdfView();
    pdfview.Frame = View.Frame;
    pdfview.Document = document;
    pdfview.AutoScales = true;
    pdfview.UserInteractionEnabled = true;
    pdfview.BackgroundColor = UIColor.Gray;

    View.AddSubview(pdfview);
}

效果:

这篇关于在iOS上使用PDFKit.PDFView打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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