如何使用pdfkit ios 11打开pdf文件?

KAR*_*KAR 5 objective-c swift ios11

我正在尝试使用新的iOS 11框架pdfkit打开PDF文件.

但我无法做到这一点.

所以帮助我使用pdfkit框架打开文件.

先感谢您.

Par*_*oja 7

对于新的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)
Run Code Online (Sandbox Code Playgroud)

  • @Ron 根据您的设计添加关闭按钮或手势由您决定 (2认同)

bha*_*eda 7

这是基于Objective-C的示例

  1. 确保将PDFKit框架添加到项目中 在此处输入图片说明

  2. 导入框架头 #import <PDFKit/PDFKit.h>

  3. 创建PDFDocument实例。就我而言,它使用的是NSURL

    // fileURL is NSURL file path instance created using base64 pdf content PDFDocument *pdfDocument = [[PDFDocument alloc] initWithURL:fileURL];

  4. 创建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]) {
Run Code Online (Sandbox Code Playgroud)

用PDFKit替换webview实现后,我只需要使用dataRepresentationmethod来获取fileData,其余的按原样工作

NSData *fileData = [pdfDocument dataRepresentation];
Run Code Online (Sandbox Code Playgroud)

请检查此WWDC页面以获取更多详细信息


Rav*_*ran 6

这是基于 Objective-C 的示例

#import <PDFKit/PDFKit.h>



PDFView *View = [[PDFView alloc] initWithFrame: self.view.bounds];
View.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
View.autoScales = NO ;
View.displayDirection = kPDFDisplayDirectionHorizontal;
View.displayMode = kPDFDisplaySinglePageContinuous;
View.displaysRTL = YES ;
[View setDisplaysPageBreaks:YES];
[View setDisplayBox:kPDFDisplayBoxTrimBox];
[View zoomIn:self];
[self.view addSubview:View];

NSURL * URL = [[NSBundle mainBundle] URLForResource: @"test" withExtension: @ "pdf" ];
PDFDocument * document = [[PDFDocument alloc] initWithURL: URL];
View.document = document;
Run Code Online (Sandbox Code Playgroud)