New: Create and edit Word documents with DOCX Editor in WebViewer
By Jamie Dassoulas | 2019 Apr 25
3 min
Tags
open source
swift
annotation
tutorial
In this article we describe how to add text highlight and ink drawing annotations to a PDF using Swift with PDFKit, Apple's built-in PDF SDK introduced in iOS 11.
For info on getting started with PDFKit and Swift, see our guide on How to add a PDF viewer to your app using Swift and PDFKit.
After you have configured a PDF viewer following the guide above, we can get started!
To create a highlight annotation from the selected text, we need to get the PDFView
's current selection on a line-by-line basis, then loop over all the pages contained in the selection. We can then get the bounds of the selection for that page, create a highlight annotation with those bounds, and add it to the page.
// Get an array of selections where each selection corresponds to a single line of the selected text
guard let selections = pdfView.currentSelection?.selectionsByLine()
else { return }
// Loop over the selections line by line
selections.forEach({ selection in
// Loop over the pages encompassed by each selection
selection.pages.forEach({ page in
// Create a new highlight annotation with the selection's bounds and add it to the page
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.color = .yellow
page.addAnnotation(highlight)
})
})
Now your selected text has been highlighted.
Highlighting text with PDFKit for iOS.
To create an ink annotation in PDFKit, we need to create a path to save to the annotation. For example to draw an ink annotation with a rectangular path in the bottom left corner of the PDF page, we can do the following:
// Get the current page
guard let page = pdfView.currentPage else {return}
// Create a rectangular path
// Note that in PDF page coordinate space, (0,0) is the bottom left corner of the page
let path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 100, height: 100))
let inkAnnotation = PDFAnnotation(bounds: page.bounds(for: pdfView.displayBox), forType: .ink, withProperties: nil)
inkAnnotation.add(path)
page.addAnnotation(inkAnnotation)
Creating an ink annotation with PDFKit for iOS.
And that's it! You now have basic PDF viewing and annotating functionality.
This is a great solution if you're not too concerned with performance or rendering and you just need basic PDF functionality. However for more advanced features such as creating a highlight of ink annotation based on a user's input you would have to build that from scratch, as well as the UI and UX for those features.
For more a reliable, powerful and easy-to-use solution, you might want to consider our iOS PDF SDK.
Apryse for iOS is a fully supported SDK that provides robust functionality, reliable high-performance rendering, and powerful annotation capabilities out-of-the–box. It also includes a highly-customizable UI which gives you control over the look and feel of your app.
How to Add Annotations to PDFs Using Swift and PDFKit also supports Microsoft Office formats allowing you to display Word, Excel, and Powerpoint documents without needing to install those apps or any other third-party dependencies.
To show a PDF with Apryse, download and integrate the Apryse SDK for iOS into your project.
The Apryse PTDocumentViewController
is a full-featured PDF viewer that can be added to your app with a few lines of code. In the following sample, a PTDocumentViewController
is shown from a UIViewController
subclass's viewDidAppear(_:)
method:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// ...
// Create PTDocumentViewController.
let documentController = PTDocumentViewController()
// Load Sample.pdf file from app bundle.
let fileURL = Bundle.main.url(forResource: "Sample", withExtension: "pdf")
documentController.openDocument(with: fileURL!)
// Show PTDocumentViewController in UINavigationController.
let navigationController = UINavigationController(rootViewController: documentController)
self.present(navigationController, animated: true, completion: nil)
}
In the sample above, a PTDocumentViewController
is created and added to a UINavigationController
, a sample document is loaded from the app bundle, and the PDF is presented in a view with powerful viewing and annotating functionality — all with just a few lines of code!
Showing a PDF with Apryse.
Adding highlight and ink annotations is as easy as tapping a button. Our APIs give you even more control over how your users interact with PDFs.
Some of the features in the SDK and ready-to-use in PTDocumentViewController
are:
Apryse for iOS not only supports PDF documents but also other formats including .docx
, .doc
, .pptx
, .xlsx
, Markdown, and various image formats.
Apple's PDFKit framework is a relatively easy way to add basic PDF viewing and interaction capabilities to your iOS app but it is not a complete solution and requires extra work to develop the UI and annotation capabilities.
Apryse's iOS PDF library provides many more features, high-performance and high-fidelity rendering, easy customization, as well as support for 30+ file formats. It's easy to integrate and includes comprehensive tech support.
Get started by downloading a free trial!
Tags
open source
swift
annotation
tutorial
Jamie Dassoulas
Related Products
Share this post
PRODUCTS
Popular Content