【问题标题】:Annotate/Draw on PDF in Swift在 Swift 中对 PDF 进行注释/绘图
【发布时间】:2017-07-16 16:20:21
【问题描述】:

我正在编写一个包含多个 PDF 文档的应用程序,我将根据用户的输入在屏幕上显示这些文档。 显示后,我想允许用户在 PDF 上绘制/注释。然后,我想保存带有图纸/注释的 PDF 以供以后使用。

我已经无休止地搜索有关注释 PDF 的教程,但我回来的根本没有!

我在 GitHub 上发现了一个名为“UXMPDF”的 cocoapod。有人用过吗?

任何有关执行此类操作的信息将不胜感激!谢谢

【问题讨论】:

  • 你得到答案了吗?

标签: ios swift pdf


【解决方案1】:

首先创建 PDFView 并加载 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:

@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)
        })
     }
   }

如果你想保存你做了什么:

self.pdfView.document.write(to:YOUR_FILE_URL)

您可以使用的其他 pdfAnnotation:

.underline
.strikeOut
.circle
.square

【讨论】:

  • 我的程序注释pdf文件并正确显示在视图中也正确保存在doc目录中,但是在调试区域gettingmsg Failed to load /System/Library/PrivateFrameworks/CorePDF.framework/Versions/A /CorePDF 错误:尝试使用键保存字典:。字典键必须是字符串类型错误:尝试使用键保存字典:。字典键必须是字符串类型。错误:无法为键创建字典值:/DR。无效值。错误:无法保存注释键的值:/DR。类型无效。
  • 我认为是 pdfkit 错误我不确定
猜你喜欢
  • 2021-03-14
  • 1970-01-01
  • 2022-11-11
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-16
  • 2017-08-26
相关资源
最近更新 更多