在Swift中对PDF进行注释/绘图 [英] Annotate/Draw on PDF in Swift
问题描述
我正在编写一个包含多个PDF文档的应用程序,我将根据用户的输入将其显示在屏幕上. 一旦显示,我希望允许用户在PDF上进行绘制/注释.然后,我想保存带有图纸/注释的PDF,以备将来使用.
I am writing an app that will contain multiple PDF documents that I will display on screen depending on the user's input. Once displayed, I would like to allow the user to draw/annotate on the PDF. I would then like to save the PDF with the drawings/annotations on for later use.
我一直在搜索有关注释PDF的教程,但是我回来的却不多!
I have searched endlessly for tutorials on annotating a PDF but I am coming back with not much at all!
我在GitHub上找到了一个名为'UXMPDF'的cocoapod.有人用过吗?
I have found a cocoapod on GitHub called 'UXMPDF'. Has anyone used this?
任何有关执行此类操作的信息将不胜感激!谢谢
Any information on performing this type of operation would be hugely appreciated! Thanks
推荐答案
首先创建PDFView并加载pdfFile:
First Create PDFView and Load pdfFile:
override func viewDidLoad() {
super.viewDidLoad()
let pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
guard let document = PDFDocument(url: YOUR_FILE_PATH) else {
return
}
pdfView.document = document
pdfView.displayMode = .singlePageContinuous
pdfView.autoScales = true
pdfView.displayDirection = .horizontal
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin]
pdfView.maxScaleFactor = 4
pdfView.delegate = self
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.usePageViewController(true, withViewOptions: nil)
pdfView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(pdfView)
self.pdfView = pdfView
self.pdfDocument = document
let highLightItem = UIMenuItem(title:"Highlight"), action: #selector(highlightFromMenuItem))
}
您可以像这样使用UIMenuItem
中的pdfAnnotation:
you can use pdfAnnotation from UIMenuItem
like that:
@objc func highlightFromMenuItem() {
if let document = self.pdfDocument {
let pdfSelection : PDFSelection = PDFSelection(document: document)
pdfSelection.add((self.pdfView?.currentSelection)!)
let arrayByLines = self.pdfView?.currentSelection?.selectionsByLine()
arrayByLines?.forEach({ (selection) in
let annotation = PDFAnnotation(bounds: selection.bounds(for: (self.pdfView?.currentPage)!), forType: .highlight, withProperties: nil)
annotation.color = .yellow
self.pdfView?.currentPage?.addAnnotation(annotation)
})
}
}
如果您想保存所做的事情,
If you want to save what did you did:
self.pdfView.document.write(to:YOUR_FILE_URL)
您可以使用的其他pdfAnnotation:
other pdfAnnotation you can use:
.underline
.strikeOut
.circle
.square
这篇关于在Swift中对PDF进行注释/绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!