作者:凡秘能 | 来源:互联网 | 2023-01-27 18:25
我正在尝试使用新的iOS 11框架pdfkit打开PDF文件.
但我无法做到这一点.
所以帮助我使用pdfkit框架打开文件.
先感谢您.
1> Parth Adroja..:
对于新的PDFKit,
你需要 import PDFKit
然后创建PDFView视图并提供PDFDocument以查看文档.请查看以下代码以供参考.
let pdfView = PDFView(frame: self.view.bounds)
let url = Bundle.main.url(forResource: "pdfFile", withExtension: "pdf")
pdfView.document = PDFDocument(url: url!)
self.view.addSubview(pdfView)
2> 小智..:
这是基于Objective-C的示例
确保将PDFKit框架添加到项目中
导入框架头
#import
创建PDFDocument实例。就我而言,它使用的是NSURL
// fileURL is NSURL file path instance created using base64 pdf content
PDFDocument *pdfDocument = [[PDFDocument alloc] initWithURL:fileURL];
创建PDFView实例并为其分配pdfDocument文档
PDFView *pdfView = [[PDFView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
pdfView.document = pdfDocument;
pdfView.displayMode = kPDFDisplaySinglePageContinuous;
pdfView.autoScales = true;
pdfView.layer.borderWidth = 2;
pdfView.layer.borderColor = [UIColor blueColor].CGColor;
正如许多其他人所做的那样,我们正在使用Webview来显示PDF内容并与之交互。仔细阅读框架并尝试一些示例,PDFKit就是一个经过深思熟虑的设计,可以立即使用多种功能,并简化了开发。
例如,我们在应用程序中具有AirPrint功能,该功能具有此检查以确定可打印性
if ([UIPrintInteractionController canPrintData: fileData]) {
用PDFKit替换webview实现后,我只需要使用dataRepresentation
method来获取fileData,其余的按原样工作
NSData *fileData = [pdfDocument dataRepresentation];
请检查此WWDC页面以获取更多详细信息