Skip to content

A PDF viewer and annotator that can be embedded in iOS applications

License

Notifications You must be signed in to change notification settings

scoreyou/UXMPDFKit

 
 

Repository files navigation

UXM Token Field

Version License Platform

Requirements

  • iOS 9 or above
  • Xcode 8 or above
  • Swift 3.0

Note

This project is still in early stages. Right now the PDF reader works both programmatically and through interface builder. This PDF reader supports interactive forms and provides methods for overlaying text, signature and checkbox elements onto the page, as well as rendering a PDF with the elements burned back onto the PDF. See the example project for how to implement.

Installation

UXMPDFKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "UXMPDFKit"

If you wish to use the Swift 2.3 version, use the following instead:

pod "UXMPDFKit", "~> 0.3.0"

Usage

Simple Usage

UXMPDFKit comes with a single page PDF reader with many features implemented right out of the box. Simply create a new PDFViewController, pass it a document and display it like any other view controller. It includes support for forms, a page scrubber and page scrolling.

let url = NSBundle.mainBundle().pathForResource("sample", ofType: "pdf")!
let document = try! PDFDocument(filePath: url, password: "password_if_needed")
let pdf = PDFViewController(document: document)

self.navigationController?.pushViewController(pdf, animated: true)

Single Page Collection View

This collection view renders a PDF in its entirety one page at a time in photo-slideshow style.

let collectionView = PDFSinglePageViewer(frame: self.view.bounds, document: self.document)
collectionView.singlePageDelegate = self

Its delegate methods are implemented as follows:

func singlePageViewer(collectionView: PDFSinglePageViewer, didDisplayPage page:Int)
func singlePageViewer(collectionView: PDFSinglePageViewer, loadedContent content:PDFPageContentView)
func singlePageViewer(collectionView: PDFSinglePageViewer, selectedAction action:PDFAction)

Forms

User-interactable forms are supported by UXMPDFKit, but only partially. Currently only PDF's versions 1.6 & 1.7 render correctly.

Form features implemented:

  • Signatures
  • Text Fields
  • Checkboxes
  • Radio Buttons
  • Choice Boxes

Form parsing and handling is taken care of by the PDFFormViewController. It takes a document, and then is passed a PDFPageContentView to render form elements onto.

let formController = PDFFormViewController(document: self.document)
formController.showForm(contentView)

PDF rewriting is not currently supported, but flattening inputed data onto the PDF is. To render the form information onto the document, call:

func renderFormOntoPDF() -> NSURL // Returns a temporary url
func save(url: NSURL) -> Bool // Writes 

Annotations

User annotations are supported at a basic level, however instead of being written onto the PDF, are burned on at the time of saving.

Current annotation types available:

  • Pen
  • Highlighter
  • Textbox

All annotations are stored in memory until being rendered back onto the PDF by the PDFRenderer.

To create a new annotation type, you must extend the following protocol:

protocol PDFAnnotation {

    func mutableView() -> UIView
    func touchStarted(touch: UITouch, point:CGPoint)
    func touchMoved(touch:UITouch, point:CGPoint)
    func touchEnded(touch:UITouch, point:CGPoint)
    func drawInContext(context: CGContextRef)
}

An annotation should be an object that contains its position and value, not a view. Because annotations are written onto temporary objects, they should be created, not passed by reference each time mutableView() is called.

Actions

Partial action support was added in version 0.3.0 and will be increased upon in future versions.

Currently supported actions:

  • External URL
  • Go To (internal jump to page index)
  • Remote Go To
  • Named
  • Launch
  • Javascript
  • Rich Media

Tapped actions are passed to your view controller by the PDFSinglePageViewer in its contentDelegate

Renderer

In order to perform write operations back onto a PDF in an efficient format, a renderer is used. Each type of form, annotation, etc that needs to be rendered back onto the PDF should extend the following protocol:

protocol PDFRenderer {
    func render(page: Int, context:CGContext, bounds: CGRect)
}

Controllers or objects that extend this protocol can then be passed to the PDFRenderer to be written onto a temporary document or saved permanently onto the document.

let renderer = PDFRenderController(document: self.document, controllers: [
    self.annotationController,
    self.formController
])
let pdf = renderer.renderOntoPDF()

Author

Chris Anderson:

License

UXMPDFKit is available under the MIT license. See the LICENSE file for more info.

About

A PDF viewer and annotator that can be embedded in iOS applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 93.3%
  • Shell 5.6%
  • Other 1.1%