测试测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新版本 | 2017 年 6 月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✓ |
由 Chris Anderson 维护。
该项目仍处于早期阶段。目前,PDF 阅读器既可以通过编程方式工作,也可以通过接口构建器工作。此 PDF 阅读器支持交互式表单,并提供将文本、签名和复选框元素叠加到页面上的方法,同时还可以将元素烤接到 PDF 上进行渲染。请参阅示例项目以了解如何实现。
UXMPDFKit 随附一个单页 PDF 阅读器,自带许多功能。只需创建一个新的 PDFViewController,传递一个文档并显示它,就像其他任何视图控制器一样。它包括表单支持、页面滚动条和页面滚动。
let path = Bundle.main.path(forResource: "sample", ofType: "pdf")!
let document = try! PDFDocument(filePath: path, password: "password_if_needed")
let pdf = PDFViewController(document: document)
self.navigationController?.pushViewController(pdf, animated: true)
虽然是用 Swift 编写的,但核心阅读器可以在 Objective-C 中使用。
NSError *error;
NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"];
PDFDocument *document = [[PDFDocument alloc] initWithFilePath:path password:@"password_if_needed" error:&error];
PDFViewController *pdfVC = [[PDFViewController alloc] initWithDocument:document];
[self.navigationController pushViewController:pdfVC animated:true];
此集合视图以幻灯片风格分页渲染整个 PDF,一次一页。
let collectionView = PDFSinglePageViewer(frame: self.view.bounds, document: self.document)
collectionView.singlePageDelegate = self
其代理方法实现如下:
func singlePageViewer(collectionView: PDFSinglePageViewer, didDisplayPage page: Int)
func singlePageViewer(collectionView: PDFSinglePageViewer, loadedContent content: PDFPageContentView)
func singlePageViewer(collectionView: PDFSinglePageViewer, selectedAction action: PDFAction)
UXMPDFKit 支持 User-interactable 表单,但只部分支持。目前,只有 PDF 版本 1.6 和 1.7 可以正确渲染。
已实现的表单功能
表单解析和处理由 PDFFormViewController 负责处理。它接受一个文档,然后将 PDFPageContentView 传递给它以渲染表单元素。
let formController = PDFFormViewController(document: self.document)
formController.showForm(contentView)
目前不支持 PDF 重写,但支持将输入数据平坦化到 PDF 上。为了将表单信息渲染到文档中,请调用
func renderFormOntoPDF() -> NSURL // Returns a temporary url
func save(url: NSURL) -> Bool // Writes
在基本级别上支持用户标注,然而,它们不是写入PDF,而是在保存时烧录。
当前可用的标注类型
所有标注都将存储在内存中,直到通过PDFRenderer渲染回PDF。
要创建新的标注类型,必须扩展以下协议
public protocol PDFAnnotation {
/// The page number the annotation is located on
var page: Int? { get set }
/// Unique identifier to be able to select annotation by
var uuid: String { get }
/// Boolean representing if the annotation has been saved
var saved: Bool { get set }
var delegate: PDFAnnotationEvent? { get set }
/// Force implementations to have an init
init()
/// A function to return a view composed of the annotations properties
func mutableView() -> UIView
/// Set of handlers to pass touches to annotation
func touchStarted(_ touch: UITouch, point: CGPoint)
func touchMoved(_ touch: UITouch, point: CGPoint)
func touchEnded(_ touch: UITouch, point: CGPoint)
/// Method to save annotation locally
func save()
func drawInContext(_ context: CGContext)
func didEnd()
func encode(with aCoder: NSCoder)
}
标注应是一个包含其位置和值的对象,而不是一个视图。因为标注写入到临时对象中,所以它们应该在每次调用mutableView()
时创建,而不是通过引用传递。
此外,建议传递给mutableView()
的视图扩展ResizableView
,因为这允许标注可以单独地移动、调整大小和删除。
为了让标注能够列在工具栏中,它们还必须扩展PDFAnnotationButtonable
。
public protocol PDFAnnotationButtonable: PDFAnnotation {
/// Name for UIBarButtonItem representation of annotation
static var name: String? { get }
/// Image for UIBarButtonItem representation of annotation
static var buttonImage: UIImage? { get }
}
部分动作支持在版本0.3.0中被添加,并将在未来版本中增加。
当前支持的动作
点击的动作将通过PDFSinglePageViewer的contentDelegate
传递给您的视图控制器
为了以高效格式将写操作回写到PDF,使用了渲染器。每种需要渲染回PDF的表单、标注等类型都应该扩展以下协议
protocol PDFRenderer {
func render(page: Int, context:CGContext, bounds: CGRect)
}
扩展此协议的控制器或对象可以然后传递给PDFRenderer以写入临时文档或永久保存到文档中。
let renderer = PDFRenderController(document: self.document, controllers: [
self.annotationController,
self.formController
])
let pdf = renderer.renderOntoPDF()
Chris Anderson
UXMPDFKit在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。