open PDF with PDFKit.PDFView on iOS

一个人想着一个人 提交于 2020-05-17 05:58:08

问题


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

My code is:

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;

I get an exception at line 4, which says :

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

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

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?


回答1:


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

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

This is full sample code :

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

And effect :



来源:https://stackoverflow.com/questions/61600221/open-pdf-with-pdfkit-pdfview-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!