OpenPDFModule.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // OpenPDFModule.swift
  3. // ComPDFKit_RN
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import Foundation
  13. import ComPDFKit
  14. import ComPDFKit_Tools
  15. @objc(OpenPDFModule)
  16. class OpenPDFModule: NSObject, CPDFViewBaseControllerDelete {
  17. @objc(openPDF:)
  18. func openPDF(_ configurationJson: String) -> Void {
  19. DispatchQueue.main.async {
  20. let documentPath = Bundle.main.path(forResource: "developer_guide_ios", ofType: "pdf") ?? ""
  21. let rootNav = OpenPDFModule.presentedViewController()
  22. let jsonDic = self.readJSON(configurationJson)
  23. let configuration = self.parseJSON(jsonDic)
  24. let pdfViewController = CPDFViewController(filePath: documentPath, password: "", configuration: configuration)
  25. pdfViewController.delegate = self
  26. let nav = CNavigationController(rootViewController: pdfViewController)
  27. nav.modalPresentationStyle = .fullScreen
  28. rootNav?.present(nav, animated: true)
  29. }
  30. }
  31. @objc(openPDFByConfiguration: password: configurationJson:)
  32. func configurationJson(filePath: String, password: String, configurationJson: String) {
  33. let rootNav = OpenPDFModule.presentedViewController()
  34. let jsonDic = self.readJSON(configurationJson)
  35. let configuration = self.parseJSON(jsonDic)
  36. let pdfViewController = CPDFViewController(filePath: filePath, password: password, configuration: configuration)
  37. pdfViewController.delegate = self
  38. let nav = CNavigationController(rootViewController: pdfViewController)
  39. nav.modalPresentationStyle = .fullScreen
  40. rootNav?.present(nav, animated: true)
  41. }
  42. @objc static func requiresMainQueueSetup() -> Bool {
  43. return true
  44. }
  45. // MARK: - Private Methods
  46. func readJSON(_ jsonFilePath: String) -> Dictionary<String, Any> {
  47. do {
  48. if let jsonData = jsonFilePath.data(using: .utf8) {
  49. let jsonDic = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: Any] ?? [String: Any]()
  50. return jsonDic
  51. }
  52. } catch{
  53. }
  54. return Dictionary()
  55. }
  56. func parseJSON(_ jsonDic: Dictionary<String, Any>) -> CPDFConfiguration {
  57. let configuration = CPDFConfiguration()
  58. for (key, value) in jsonDic {
  59. print("Key: \(key)")
  60. if let innerDict = value as? [String: Any] {
  61. for (innerKey, innerValue) in innerDict {
  62. if let innerArray = innerValue as? [Any] {
  63. if innerKey == "iosRightBarAvailableActions" {
  64. for (_, item) in innerArray.enumerated() {
  65. if item as! String == "search" {
  66. let search = CNavBarButtonItem(viewRightBarButtonItem: .search)
  67. configuration.showRightItems.append(search)
  68. } else if item as! String == "bota" {
  69. let bota = CNavBarButtonItem(viewRightBarButtonItem: .bota)
  70. configuration.showRightItems.append(bota)
  71. } else if item as! String == "menu" {
  72. let more = CNavBarButtonItem(viewRightBarButtonItem: .more)
  73. configuration.showRightItems.append(more)
  74. }
  75. }
  76. } else if innerKey == "iosLeftBarAvailableActions" {
  77. for (_, item) in innerArray.enumerated() {
  78. if item as! String == "back" {
  79. let back = CNavBarButtonItem(viewLeftBarButtonItem: .back)
  80. configuration.showleftItems.append(back)
  81. } else if item as! String == "thumbnail" {
  82. let thumbnail = CNavBarButtonItem(viewLeftBarButtonItem: .thumbnail)
  83. configuration.showleftItems.append(thumbnail)
  84. }
  85. }
  86. } else if innerKey == "availableMenus" {
  87. for (_, item) in innerArray.enumerated() {
  88. if item as! String == "viewSettings" {
  89. configuration.showMoreItems.append(.setting)
  90. } else if item as! String == "documentEditor" {
  91. configuration.showMoreItems.append(.pageEdit)
  92. } else if item as! String == "security" {
  93. configuration.showMoreItems.append(.security)
  94. } else if item as! String == "watermark" {
  95. configuration.showMoreItems.append(.watermark)
  96. } else if item as! String == "documentInfo" {
  97. configuration.showMoreItems.append(.info)
  98. } else if item as! String == "save" {
  99. configuration.showMoreItems.append(.save)
  100. } else if item as! String == "share" {
  101. configuration.showMoreItems.append(.share)
  102. } else if item as! String == "openDocument" {
  103. configuration.showMoreItems.append(.addFile)
  104. }
  105. }
  106. }
  107. } else {
  108. if innerKey == "initialViewMode" {
  109. if innerValue as! String == "viewer" {
  110. configuration.enterToolModel = .viewer
  111. } else if innerValue as! String == "annotations" {
  112. configuration.enterToolModel = .annotation
  113. } else if innerValue as! String == "contentEditor" {
  114. configuration.enterToolModel = .edit
  115. } else if innerValue as! String == "forms" {
  116. configuration.enterToolModel = .form
  117. } else if innerValue as! String == "digitalSignatures" {
  118. configuration.enterToolModel = .signature
  119. }
  120. }
  121. }
  122. }
  123. } else if let innerArray = value as? [Any] {
  124. for (index, item) in innerArray.enumerated() {
  125. print(" Item \(index): \(item)")
  126. }
  127. }
  128. }
  129. return configuration
  130. }
  131. func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  132. baseControllerDelete.dismiss(animated: true)
  133. }
  134. class func presentedViewController() -> UIViewController? {
  135. var rootViewController: UIViewController? = nil
  136. if let appDelegate = UIApplication.shared.delegate as? NSObject {
  137. if appDelegate.responds(to: Selector(("viewController"))) {
  138. rootViewController = appDelegate.value(forKey: "viewController") as? UIViewController
  139. }
  140. }
  141. if rootViewController == nil, let appDelegate = UIApplication.shared.delegate as? NSObject, appDelegate.responds(to: #selector(getter: UIApplicationDelegate.window)) {
  142. if let window = appDelegate.value(forKey: "window") as? UIWindow {
  143. rootViewController = window.rootViewController
  144. }
  145. }
  146. if rootViewController == nil {
  147. if let window = UIApplication.shared.keyWindow {
  148. rootViewController = window.rootViewController
  149. }
  150. }
  151. guard let finalRootViewController = rootViewController else {
  152. return nil
  153. }
  154. var currentViewController = finalRootViewController
  155. while let presentedViewController = currentViewController.presentedViewController {
  156. if !(presentedViewController is UIAlertController) && currentViewController.modalPresentationStyle != .popover {
  157. currentViewController = presentedViewController
  158. } else {
  159. return currentViewController
  160. }
  161. }
  162. return currentViewController
  163. }
  164. }