-
What's new in PDFKit
Discover PDFKit — a full-featured framework that helps your app view, edit, and save PDF documents. We'll take you through the latest features in PDFKit, including support for live text and forms, creating PDFs from images, building interactive overlays, and saving annotations.
Ressources
Vidéos connexes
WWDC22
WWDC19
-
Rechercher dans cette vidéo…
-
-
6:54 - Implementing the overlay protocol
class Coordinator: NSObject, PDFPageOverlayViewProvider { var pageToViewMapping = [PDFPage: UIView]() func pdfView(_ view: PDFView, overlayViewFor page: PDFPage) -> UIView? { var resultView: PKCanvasView? = nil if let overlayView = pageToViewMapping[page] { resultView = overlayView } else { let canvasView = PKCanvasView(frame: .zero) canvasView.drawingPolicy = .anyInput canvasView.tool = PKInkingTool(.pen, color: .systemYellow, width: 20) canvasView.backgroundColor = UIColor.clear pageToViewMapping[page] = canvasView resultView = canvasView } // If we have stored a drawing on the page, set it on the canvas let page = page as! MyPDFPage if let drawing = page.drawing { resultView?.drawing = drawing; } return resultView } func pdfView(_ pdfView: PDFView, willEndDisplayingOverlayView overlayView: UIView, for page: PDFPage) { let overlayView = overlayView as! PKCanvasView let page = page as! MyPDFPage page.drawing = overlayView.drawing pageToViewMapping.removeValue(forKey: page) } } -
10:22 - Create a subclass of PDFAnnotation
// Implement a subclass with a drawing override class MyPDFAnnotation: PDFAnnotation { override func draw(with box: PDFDisplayBox, in context: CGContext) { UIGraphicsPushContext(context) context.saveGState() let page = self.page as! MyPDFPage if let drawing = page.drawing { let image = drawing.image(from: drawing.bounds, scale: 1) image.draw(in: drawing.bounds) } context.restoreGState() UIGraphicsPopContext() } } -
10:35 - Add annotations to your document when saving
override func contents(forType typeName: String) throws -> Any { if let pdfDocument = pdfDocument { // Go through all pages in the document for i in 0...pdfDocument.pageCount-1 { if let page = pdfDocument.page(at: i) { if let drawing = (page as! MyPDFPage).drawing { // Create an annotation of our custom subclass let newAnnotation = MyPDFAnnotation(bounds: drawing.bounds, forType: .stamp, withProperties: nil) // Add our custom data let codedData = try! NSKeyedArchiver.archivedData(withRootObject: drawing, requiringSecureCoding: true) newAnnotation.setValue(codedData, forAnnotationKey: PDFAnnotationKey(rawValue: "drawingData")) // Add our annotation to the page page.addAnnotation(newAnnotation) } } } // -- Option #1: Save the document to a data representation if let resultData = pdfDocument.dataRepresentation() { return resultData } // -- Option #2: Save the document to a data representation and "burn in" annotations let options = [PDFDocumentWriteOption.burnInAnnotationsOption: true] if let resultData = pdfDocument.dataRepresentation(options: options) { return resultData } } // Fall through to returning empty data return Data() }
-