簡體   English   中英

在Swift中對PDF進行注釋/繪制

[英]Annotate/Draw on PDF in Swift

我正在編寫一個包含多個PDF文檔的應用程序,我將根據用戶的輸入顯示在屏幕上。 一旦顯示,我想允許用戶在PDF上繪制/注釋。 然后我想保存帶有圖紙/注釋的PDF以供以后使用。

我已經無休止地搜索了關於注釋PDF的教程,但我回來的時間並不多!

我在GitHub上發現了一個名為'UXMPDF'的cocoapod。 有沒有人用過這個?

有關執行此類操作的任何信息都將非常感謝! 謝謝

首先創建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中的UIMenuItem

@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

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM