问题
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