Skip to content

Latest commit

 

History

History
executable file
·
215 lines (155 loc) · 7.45 KB

README.md

File metadata and controls

executable file
·
215 lines (155 loc) · 7.45 KB

UXM Token Field

CI Status Version Swift Carthage Compatible 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

CocoaPods

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"

Carthage

UXMPDFKit is also available through Carthage. To install just write into your Cartfile:

github "uxmstudio/UXMPDFKit"

Run carthage update to build the framework and drag the built UXMPDFKit.framework into your Xcode project.

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.

Swift

let path = Bundle.main.path(forResource: "sample", ofType: "pdf")!
let document = try! PDFDocument(filePath: path, password: "password_if_needed")
let pdf = PDFViewController(document: document)

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

Objective-C

Although written in Swift, the core reader can be used in Objective-C.

NSError *error;
NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"];
PDFDocument *document = [[PDFDocument alloc] initWithFilePath:path password:@"password_if_needed" error:&error];
PDFViewController *pdfVC = [[PDFViewController alloc] initWithDocument:document];

[self.navigationController pushViewController:pdfVC 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:

public protocol PDFAnnotation {

    /// The page number the annotation is located on
    var page: Int? { get set }

    /// Unique identifier to be able to select annotation by
    var uuid: String { get }

    /// Boolean representing if the annotation has been saved
    var saved: Bool { get set }

    var delegate: PDFAnnotationEvent? { get set }

    /// Force implementations to have an init
    init()

    /// A function to return a view composed of the annotations properties
    func mutableView() -> UIView

    /// Set of handlers to pass touches to annotation
    func touchStarted(_ touch: UITouch, point: CGPoint)
    func touchMoved(_ touch: UITouch, point: CGPoint)
    func touchEnded(_ touch: UITouch, point: CGPoint)

    /// Method to save annotation locally
    func save()
    func drawInContext(_ context: CGContext)

    func didEnd()

    func encode(with aCoder: NSCoder)
}

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.

Additionally, it is recommended that the view passed by mutableView() extend ResizableView as this allows the annotation to be moved, resized and deleted individually.

In order for annotations to be able to be listed inside of the toolbar, they must also extend PDFAnnotationButtonable.

public protocol PDFAnnotationButtonable: PDFAnnotation {

    /// Name for UIBarButtonItem representation of annotation
    static var name: String? { get }

    /// Image for UIBarButtonItem representation of annotation 
    static var buttonImage: UIImage? { get }
}

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.