0

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?

LofiMAM
  • 127
  • 1
  • 11

1 Answers1

2

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 :

enter image description here

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • well,it works fine.but I have a question, when I'm debugging to see the difference between NSBundle.MainBundle.PathForResource and NSBundle.MainBundle.GetUrlForResource, it was that the second returns "file:///" in the start of URL. Now I need your help to open the PDF from device storage if you can help me please, the URL in simulator is "/Users/haykalmedia/Library/Developer/CoreSimulator/Devices/DA58EBD1-0328-409A-8F95-63E3FFAE456F/data/Containers/Data/Application/1E65FD74-8080-4FE6-BF30-554B6E11424D/Documents/Harvard_26_M-1.pdf", I tried to add "file:///" to its start, but it does not work. – LofiMAM May 05 '20 at 07:16
  • @LofiMAM If add `file://` before path string , it will work as the same with `GetUrlForResource`. I think here you add one more `/` .At the start of path string , there is a `/` exists . So remove the extra `/` , it will works :) – Junior Jiang May 05 '20 at 08:07
  • thanks a lot, I knew it and tried it, but unfortunately it does not work, so I used var data = new NSData(url); var document = new PDFDocument(data); and it works fine. thank you man, you saved me. – LofiMAM May 05 '20 at 23:18
  • there is a difference between resources path and storage path (where my actual file is in storage not in resources which means I did not need resources path). – LofiMAM May 05 '20 at 23:23
  • @LofiMAM Yes , they are different . You can have a look at this doc about path in iOS .https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system#application-directories – Junior Jiang May 06 '20 at 01:24
  • Hello, please I need your help again. PDFView class has an function called UsePageViewController with two parameters, I need to use the second parameter which named viewOptions of type NSDictionary, I need use it to set the following UIPageViewContoller properties : "TransitionStyle, NavigationOrientation".That if I can. I made some searches and I did not get a useful results. And thank you. – LofiMAM May 10 '20 at 13:05
  • @LofiMAM You can create a new question in SO to explain this detailly, then I will check that in new question :-) – Junior Jiang May 11 '20 at 01:40
  • well, I will do it soon and I will tell you. – LofiMAM May 12 '20 at 07:45