简体   繁体   中英

open PDF with PDFKit.PDFView on iOS

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?

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 :

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM