Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS上使用PDFKit.PDFView打开PDF_Ios_Pdf_Xamarin.ios_Ios Pdfkit - Fatal编程技术网

在iOS上使用PDFKit.PDFView打开PDF

在iOS上使用PDFKit.PDFView打开PDF,ios,pdf,xamarin.ios,ios-pdfkit,Ios,Pdf,Xamarin.ios,Ios Pdfkit,我有一个带有PDF viewer的应用程序,我尝试使用PDFKit.PDFView,它需要创建PDFKit.PDFDocument对象 我的代码是: var pdfview = new PdfKit.PdfView(); var path = NSBundle.MainBundle.PathForResource("Harvard", "pdf"); var urll = new NSUrl(path); var pdfdoc = new PdfDocument(urll); pdfview.D

我有一个带有PDF viewer的应用程序,我尝试使用PDFKit.PDFView,它需要创建PDFKit.PDFDocument对象

我的代码是:

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

我的pdf文件Harvard.pdf与BundleResource构建操作一起位于Resources文件夹和direct iOS项目目录中

这个PDF文件可以通过CGPDFDocument轻松阅读,但我需要使用PDFView来解决我在中之前要求它解决的问题 .
那么,有人能帮我吗?

试试下面的方法来检查URL是否返回零:

这是完整的示例代码:

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);
}
及效果:


好的,它工作得很好。但是我有一个问题,当我调试NSBundle.MainBundle.PathForResource和NSBundle.MainBundle.GetUrlForResource之间的区别时,第二个返回文件:///在URL的开头。现在我需要你的帮助从设备存储中打开PDF,如果你能帮我的话,模拟器中的URL是/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,我试图将文件:///添加到它的开头,但它不起作用。@LofiMAM如果在路径字符串之前添加file://它将与GetUrlForResource的作用相同。我想在这里再添加一个/。在路径字符串的开头,有一个/exists。因此,删除额外的/,它将工作:非常感谢,我知道它并尝试过,但不幸的是它不工作,所以我使用var data=new-NSDataurl;var文件=新的PDFDocumentdata;而且效果很好。谢谢你,伙计,你救了我。资源路径和存储路径之间存在差异,我的实际文件在存储中而不是在资源中,这意味着我不需要资源路径。@LofiMAM是的,它们不同。您可以在iOS中查看此关于路径的文档。
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);
}