Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // PDFAttempt
- //
- // Created by Glen Hinkle on 3/17/15.
- // Copyright (c) 2015 Zombie Dolphin. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController {
- var pdfVC: PDFViewController!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- //_pdfViewController = [[PDFViewController alloc] initWithResource:@"test.pdf"];
- //
- //// Manually set a form value
- //[_pdfViewController.document.forms setValue:@"Derek" ForFormWithName:@"Contacts.FirstName"];
- //
- //// Save via a flat PDF.
- //NSData* flatPDF = [_pdfViewController.document flattenedData]
- //self.copyDBFromBundleToApplicationSupportDir("Prospective Customer Consent Form_100.pdf")
- //self.copyDBFromBundleToApplicationSupportDir("Electronic Records Consent101 - FINAL.pdf")
- }
- @IBAction func save(sender: AnyObject) {
- let pdfVC = self.pdfVC
- //pdfVC.document.saveFormsToDocumentData { (success) -> Void in
- // println(pdfVC.document.documentData)
- //}
- println(pdfVC.document.formXML())
- }
- @IBAction func showThumbnails(sender: AnyObject) {
- //let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CollectionVC") as CollectionVC
- let vc = self.storyboard?.instantiateViewControllerWithIdentifier("CollectionVC") as! CollectionVC
- vc.pdfDocument = self.pdfVC.document
- self.presentViewController(vc, animated: true) { () -> Void in
- //
- }
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- override func viewDidAppear(animated: Bool) {
- super.viewDidAppear(animated)
- let imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100))
- imageView.backgroundColor = UIColor.greenColor()
- imageView.image = UIImage(named: "signature3")
- view.addSubview(imageView)
- let path = self.copyDBFromBundleToApplicationSupportDir("Prospective Customer Consent Form_100.pdf")!
- //let path = self.copyDBFromBundleToApplicationSupportDir("ACE_Level 1_Part 1_102.pdf")!
- let pdfVC = PDFViewController(path: path)
- let pdfview = PDFWidgetAnnotationView(frame: CGRectMake(0, 0, 100, 100))
- //pdfVC.pdfView.addPDFWidgetAnnotationView(pdfview)
- pdfVC.view.frame = CGRectMake(0, 0, pdfVC.view.frame.width, pdfVC.view.frame.height)
- self.view.addSubview(pdfVC.view)
- self.addChildViewController(pdfVC)
- pdfVC.didMoveToParentViewController(self)
- self.pdfVC = pdfVC
- //self.presentViewController(pdfVC, animated: true) { () -> Void in
- for (index, element) in enumerate(pdfVC.document.forms) {
- println("FORM \(index): \(element.name), \(element.defaultValue), \((element.formType as PDFFormType).rawValue), \(element.pageFrame) \(element.uiBaseFrame)")
- if let element = element as? PDFForm where element.name == "Signature9" || element.name == "Signature10" {
- drawOnPDF(path, element: element, point: CGPoint(x: element.pageFrame.origin.x, y: element.pageFrame.origin.y))
- }
- }
- //}
- }
- func drawOnPDF(path: String, element: PDFForm, point: CGPoint) {
- // Get existing Pdf reference
- let pdf = CGPDFDocumentCreateWithURL(NSURL(fileURLWithPath: path))
- // Get page count of pdf, so we can loop through pages and draw them accordingly
- let pageCount = CGPDFDocumentGetNumberOfPages(pdf);
- // Write to file
- UIGraphicsBeginPDFContextToFile(path, CGRectZero, nil)
- // Write to data
- //var data = NSMutableData()
- //UIGraphicsBeginPDFContextToData(data, CGRectZero, nil)
- for index in 1...pageCount {
- let page = CGPDFDocumentGetPage(pdf, index)
- let pageFrame = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
- UIGraphicsBeginPDFPageWithInfo(pageFrame, nil)
- var ctx = UIGraphicsGetCurrentContext()
- // Draw existing page
- CGContextSaveGState(ctx);
- CGContextScaleCTM(ctx, 1, -1);
- CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
- CGContextDrawPDFPage(ctx, page);
- CGContextRestoreGState(ctx);
- if index == pageCount {
- // Draw image on top of page
- if let image = UIImage(named: "signature3") {
- let scaledImage = scaleUIImageToSize(image, size: element.frame.size)
- //point: CGPoint(x: element.frame.origin.x, y: element.frame.origin.y))
- //let rect = CGRectMake(200, 635, scaledImage.size.width, scaledImage.size.height)
- let rect = CGRectMake(129/2, 698, scaledImage.size.width, scaledImage.size.height)
- image.drawInRect(rect)
- }
- }
- // Draw red box on top of page
- //UIColor.redColor().set()
- //UIRectFill(CGRectMake(20, 20, 100, 100));
- }
- UIGraphicsEndPDFContext()
- }
- func scaleUIImageToSize(image: UIImage, size: CGSize) -> UIImage {
- let hasAlpha = false
- let scale: CGFloat = 0.0 // Automatically use scale factor of main screen
- UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
- image.drawInRect(CGRect(origin: CGPointZero, size: size))
- let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- return scaledImage
- }
- func copyDBFromBundleToApplicationSupportDir(file: String!) -> String? {
- let applicationSupportDir = self.createApplicationSupportDirIfNotExist()
- let fileManager = NSFileManager.defaultManager()
- let fromPath = NSBundle.mainBundle().pathForResource(file, ofType: "")
- let toPath = applicationSupportDir! + file
- if (fromPath == nil) {
- println("*** File \(file) does not exist in bundle")
- }
- var error: NSError?
- if (fileManager.fileExistsAtPath(toPath)) {
- if !fileManager.removeItemAtPath(toPath, error:&error) {
- println("*** Could not remove \(toPath)")
- return nil
- }
- }
- if !fileManager.copyItemAtPath(fromPath!, toPath: toPath, error: &error) {
- println("*** Could not copy database from \(fromPath) to \(toPath)")
- return nil
- }
- println("✓ Copied file to \(toPath)")
- return toPath
- }
- func createApplicationSupportDirIfNotExist() -> String? {
- let applicationSupportDir = NSSearchPathForDirectoriesInDomains(
- NSSearchPathDirectory.ApplicationSupportDirectory,
- NSSearchPathDomainMask.UserDomainMask,
- true)[0] as! String + "/"
- let fileManager = NSFileManager.defaultManager()
- if fileManager.fileExistsAtPath(applicationSupportDir) { return applicationSupportDir }
- var error: NSError?
- if !fileManager.createDirectoryAtPath(applicationSupportDir, withIntermediateDirectories: false, attributes: nil, error: &error) {
- println("*** Could not create \(applicationSupportDir): \(error)")
- }
- return applicationSupportDir
- }
- }
- //public struct GenericGenerator<T> : GeneratorType, SequenceType {
- // let enumerator: NSEnumerator
- //
- // init(_ enumerator: NSEnumerator) {
- // self.enumerator = enumerator
- // }
- //
- // mutating public func next() -> T? {
- // return self.enumerator.nextObject() as T?
- // }
- //
- // public func generate() -> GenericGenerator<T> {
- // return GenericGenerator<T>(self.enumerator)
- // }
- //}
- //
- //extension PDFFormContainer : SequenceType {
- // public func generate() -> GenericGenerator<PDFForm> {
- // return GenericGenerator<PDFForm>(self)
- // }
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement