Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.81 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // PDFAttempt
  4. //
  5. // Created by Glen Hinkle on 3/17/15.
  6. // Copyright (c) 2015 Zombie Dolphin. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12. var pdfVC: PDFViewController!
  13.  
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. // Do any additional setup after loading the view, typically from a nib.
  17.  
  18. //_pdfViewController = [[PDFViewController alloc] initWithResource:@"test.pdf"];
  19. //
  20. //// Manually set a form value
  21. //[_pdfViewController.document.forms setValue:@"Derek" ForFormWithName:@"Contacts.FirstName"];
  22. //
  23. //// Save via a flat PDF.
  24. //NSData* flatPDF = [_pdfViewController.document flattenedData]
  25. //self.copyDBFromBundleToApplicationSupportDir("Prospective Customer Consent Form_100.pdf")
  26. //self.copyDBFromBundleToApplicationSupportDir("Electronic Records Consent101 - FINAL.pdf")
  27. }
  28.  
  29. @IBAction func save(sender: AnyObject) {
  30. let pdfVC = self.pdfVC
  31.  
  32. //pdfVC.document.saveFormsToDocumentData { (success) -> Void in
  33. // println(pdfVC.document.documentData)
  34. //}
  35.  
  36. println(pdfVC.document.formXML())
  37. }
  38.  
  39. @IBAction func showThumbnails(sender: AnyObject) {
  40. //let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CollectionVC") as CollectionVC
  41. let vc = self.storyboard?.instantiateViewControllerWithIdentifier("CollectionVC") as! CollectionVC
  42. vc.pdfDocument = self.pdfVC.document
  43. self.presentViewController(vc, animated: true) { () -> Void in
  44. //
  45. }
  46. }
  47.  
  48. override func didReceiveMemoryWarning() {
  49. super.didReceiveMemoryWarning()
  50. // Dispose of any resources that can be recreated.
  51. }
  52.  
  53. override func viewDidAppear(animated: Bool) {
  54. super.viewDidAppear(animated)
  55.  
  56. let imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100))
  57. imageView.backgroundColor = UIColor.greenColor()
  58. imageView.image = UIImage(named: "signature3")
  59. view.addSubview(imageView)
  60.  
  61. let path = self.copyDBFromBundleToApplicationSupportDir("Prospective Customer Consent Form_100.pdf")!
  62. //let path = self.copyDBFromBundleToApplicationSupportDir("ACE_Level 1_Part 1_102.pdf")!
  63.  
  64. let pdfVC = PDFViewController(path: path)
  65.  
  66. let pdfview = PDFWidgetAnnotationView(frame: CGRectMake(0, 0, 100, 100))
  67. //pdfVC.pdfView.addPDFWidgetAnnotationView(pdfview)
  68.  
  69.  
  70. pdfVC.view.frame = CGRectMake(0, 0, pdfVC.view.frame.width, pdfVC.view.frame.height)
  71.  
  72. self.view.addSubview(pdfVC.view)
  73. self.addChildViewController(pdfVC)
  74. pdfVC.didMoveToParentViewController(self)
  75.  
  76. self.pdfVC = pdfVC
  77.  
  78. //self.presentViewController(pdfVC, animated: true) { () -> Void in
  79. for (index, element) in enumerate(pdfVC.document.forms) {
  80. println("FORM \(index): \(element.name), \(element.defaultValue), \((element.formType as PDFFormType).rawValue), \(element.pageFrame) \(element.uiBaseFrame)")
  81. if let element = element as? PDFForm where element.name == "Signature9" || element.name == "Signature10" {
  82. drawOnPDF(path, element: element, point: CGPoint(x: element.pageFrame.origin.x, y: element.pageFrame.origin.y))
  83. }
  84. }
  85. //}
  86.  
  87. }
  88.  
  89. func drawOnPDF(path: String, element: PDFForm, point: CGPoint) {
  90.  
  91. // Get existing Pdf reference
  92. let pdf = CGPDFDocumentCreateWithURL(NSURL(fileURLWithPath: path))
  93.  
  94. // Get page count of pdf, so we can loop through pages and draw them accordingly
  95. let pageCount = CGPDFDocumentGetNumberOfPages(pdf);
  96.  
  97. // Write to file
  98. UIGraphicsBeginPDFContextToFile(path, CGRectZero, nil)
  99.  
  100. // Write to data
  101. //var data = NSMutableData()
  102. //UIGraphicsBeginPDFContextToData(data, CGRectZero, nil)
  103.  
  104. for index in 1...pageCount {
  105. let page = CGPDFDocumentGetPage(pdf, index)
  106. let pageFrame = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
  107.  
  108. UIGraphicsBeginPDFPageWithInfo(pageFrame, nil)
  109.  
  110. var ctx = UIGraphicsGetCurrentContext()
  111.  
  112. // Draw existing page
  113. CGContextSaveGState(ctx);
  114. CGContextScaleCTM(ctx, 1, -1);
  115. CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
  116. CGContextDrawPDFPage(ctx, page);
  117. CGContextRestoreGState(ctx);
  118.  
  119. if index == pageCount {
  120. // Draw image on top of page
  121. if let image = UIImage(named: "signature3") {
  122. let scaledImage = scaleUIImageToSize(image, size: element.frame.size)
  123.  
  124. //point: CGPoint(x: element.frame.origin.x, y: element.frame.origin.y))
  125.  
  126. //let rect = CGRectMake(200, 635, scaledImage.size.width, scaledImage.size.height)
  127. let rect = CGRectMake(129/2, 698, scaledImage.size.width, scaledImage.size.height)
  128.  
  129. image.drawInRect(rect)
  130. }
  131. }
  132.  
  133. // Draw red box on top of page
  134. //UIColor.redColor().set()
  135. //UIRectFill(CGRectMake(20, 20, 100, 100));
  136. }
  137.  
  138. UIGraphicsEndPDFContext()
  139. }
  140.  
  141. func scaleUIImageToSize(image: UIImage, size: CGSize) -> UIImage {
  142. let hasAlpha = false
  143. let scale: CGFloat = 0.0 // Automatically use scale factor of main screen
  144.  
  145. UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
  146. image.drawInRect(CGRect(origin: CGPointZero, size: size))
  147.  
  148. let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
  149. UIGraphicsEndImageContext()
  150.  
  151. return scaledImage
  152. }
  153.  
  154. func copyDBFromBundleToApplicationSupportDir(file: String!) -> String? {
  155. let applicationSupportDir = self.createApplicationSupportDirIfNotExist()
  156.  
  157. let fileManager = NSFileManager.defaultManager()
  158.  
  159. let fromPath = NSBundle.mainBundle().pathForResource(file, ofType: "")
  160. let toPath = applicationSupportDir! + file
  161.  
  162. if (fromPath == nil) {
  163. println("*** File \(file) does not exist in bundle")
  164. }
  165.  
  166. var error: NSError?
  167.  
  168. if (fileManager.fileExistsAtPath(toPath)) {
  169. if !fileManager.removeItemAtPath(toPath, error:&error) {
  170. println("*** Could not remove \(toPath)")
  171. return nil
  172. }
  173. }
  174.  
  175. if !fileManager.copyItemAtPath(fromPath!, toPath: toPath, error: &error) {
  176. println("*** Could not copy database from \(fromPath) to \(toPath)")
  177. return nil
  178. }
  179.  
  180. println("✓ Copied file to \(toPath)")
  181.  
  182. return toPath
  183. }
  184.  
  185. func createApplicationSupportDirIfNotExist() -> String? {
  186. let applicationSupportDir = NSSearchPathForDirectoriesInDomains(
  187. NSSearchPathDirectory.ApplicationSupportDirectory,
  188. NSSearchPathDomainMask.UserDomainMask,
  189. true)[0] as! String + "/"
  190.  
  191. let fileManager = NSFileManager.defaultManager()
  192. if fileManager.fileExistsAtPath(applicationSupportDir) { return applicationSupportDir }
  193.  
  194. var error: NSError?
  195.  
  196. if !fileManager.createDirectoryAtPath(applicationSupportDir, withIntermediateDirectories: false, attributes: nil, error: &error) {
  197. println("*** Could not create \(applicationSupportDir): \(error)")
  198. }
  199.  
  200. return applicationSupportDir
  201. }
  202. }
  203.  
  204. //public struct GenericGenerator<T> : GeneratorType, SequenceType {
  205. // let enumerator: NSEnumerator
  206. //
  207. // init(_ enumerator: NSEnumerator) {
  208. // self.enumerator = enumerator
  209. // }
  210. //
  211. // mutating public func next() -> T? {
  212. // return self.enumerator.nextObject() as T?
  213. // }
  214. //
  215. // public func generate() -> GenericGenerator<T> {
  216. // return GenericGenerator<T>(self.enumerator)
  217. // }
  218. //}
  219. //
  220. //extension PDFFormContainer : SequenceType {
  221. // public func generate() -> GenericGenerator<PDFForm> {
  222. // return GenericGenerator<PDFForm>(self)
  223. // }
  224. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement