8

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.

I have searched endlessly for tutorials on annotating a PDF but I am coming back with not much at all!

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

evorg88
  • 215
  • 2
  • 12

1 Answers1

4

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

    }

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)

other pdfAnnotation you can use:

.underline
.strikeOut
.circle
.square

enter image description here

Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
  • my program annotate the pdf file and correctly show in the view also correctly save in the doc directory, but in debugging area gettingmsg Failed to load /System/Library/PrivateFrameworks/CorePDF.framework/Versions/A/CorePDF Error: Attempting to save dictionary with key: . Dictionary keys must be of type string Error: Attempting to save dictionary with key: . Dictionary keys must be of type string. Error: Could not create dictionary value for key: /DR. Invalid value. Error: Cannot save value for annotation key: /DR. Invalid type. – Codicil Feb 15 '20 at 05:39
  • I think it's pdfkit bug I'm not sure – Zouhair Sassi Feb 15 '20 at 08:53