RCTCPDFViewManager.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // RCTCPDFViewManager.swift
  3. // react-native-compdfkit-pdf
  4. //
  5. // Copyright © 2014-2024 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 UIKit
  13. import ComPDFKit
  14. @objc(RCTCPDFReaderView)
  15. class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
  16. @objc override static func requiresMainQueueSetup() -> Bool {
  17. return true
  18. }
  19. var cpdfViews: Dictionary<Int, RCTCPDFView> = [:]
  20. @objc override func view() -> UIView! {
  21. let rtcCPDFView = RCTCPDFView()
  22. rtcCPDFView.delegate = self
  23. return rtcCPDFView
  24. }
  25. func saveDocument(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
  26. let rtcCPDFView = cpdfViews[tag]
  27. rtcCPDFView?.saveDocument(completionHandler: { success in
  28. completionHandler(success)
  29. if (success) {
  30. if let onChange = rtcCPDFView?.onChange {
  31. onChange(["saveDocument": "saveDocument"])
  32. }
  33. }
  34. })
  35. }
  36. func setMargins(forCPDFViewTag tag: Int, left : Int, top : Int, right : Int, bottom : Int) {
  37. let rtcCPDFView = cpdfViews[tag]
  38. rtcCPDFView?.setMargins(left: left, top: top, right: right, bottom: bottom)
  39. }
  40. func removeAllAnnotations(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
  41. let rtcCPDFView = cpdfViews[tag]
  42. rtcCPDFView?.removeAllAnnotations(completionHandler: { success in
  43. completionHandler(success)
  44. })
  45. }
  46. func importAnnotations(forCPDFViewTag tag: Int, xfdfFile : URL, completionHandler: @escaping (Bool) -> Void) {
  47. let rtcCPDFView = cpdfViews[tag]
  48. rtcCPDFView?.importAnnotations(xfdfFile: xfdfFile, completionHandler: { success in
  49. completionHandler(success)
  50. })
  51. }
  52. func exportAnnotations(forCPDFViewTag tag: Int, completionHandler: @escaping (String) -> Void) {
  53. let rtcCPDFView = cpdfViews[tag]
  54. rtcCPDFView?.exportAnnotations(completionHandler: { success in
  55. completionHandler(success)
  56. })
  57. }
  58. func setDisplayPageIndex(forCPDFViewTag tag : Int, pageIndex : Int) {
  59. let rtcCPDFView = cpdfViews[tag]
  60. rtcCPDFView?.setDisplayPageIndex(pageIndex: pageIndex)
  61. }
  62. func getCurrentPageIndex(forCPDFViewTag tag: Int, completionHandler: @escaping (Int) -> Void) {
  63. let rtcCPDFView = cpdfViews[tag]
  64. rtcCPDFView?.getCurrentPageIndex(completionHandler: { pageIndex in
  65. completionHandler(pageIndex)
  66. })
  67. }
  68. func hasChange(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
  69. let rtcCPDFView = cpdfViews[tag]
  70. rtcCPDFView?.hasChange(completionHandler: { success in
  71. completionHandler(success)
  72. })
  73. }
  74. func setScale(forCPDFViewTag tag : Int, scale : NSNumber) {
  75. let rtcCPDFView = cpdfViews[tag]
  76. rtcCPDFView?.setScale(scale: scale)
  77. }
  78. func getScale(forCPDFViewTag tag : Int, completionHandler: @escaping (NSNumber) -> Void) {
  79. let rtcCPDFView = cpdfViews[tag]
  80. rtcCPDFView?.getScale(completionHandler: { success in
  81. completionHandler(success)
  82. })
  83. }
  84. func setReadBackgroundColor(forCPDFViewTag tag : Int, displayMode : NSString) {
  85. let rtcCPDFView = cpdfViews[tag]
  86. rtcCPDFView?.setReadBackgroundColor(displayMode: displayMode)
  87. }
  88. func getReadBackgroundColor(forCPDFViewTag tag : Int, completionHandler: @escaping (NSString) -> Void){
  89. let rtcCPDFView = cpdfViews[tag]
  90. rtcCPDFView?.getReadbackgroundColor(completionHandler: {color in
  91. completionHandler(color)
  92. })
  93. }
  94. func setFormFieldHighlight(forCPDFViewTag tag : Int, formFieldHighlight : Bool) {
  95. let rtcCPDFView = cpdfViews[tag]
  96. rtcCPDFView?.setFormFieldHighlight(formFieldHighlight: formFieldHighlight)
  97. }
  98. func isFormFieldHighlight(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  99. let rtcCPDFView = cpdfViews[tag]
  100. rtcCPDFView?.isFormFieldHighlight(completionHandler: {highlight in
  101. completionHandler(highlight)
  102. })
  103. }
  104. func setLinkHighlight(forCPDFViewTag tag : Int, linkHighlight : Bool) {
  105. let rtcCPDFView = cpdfViews[tag]
  106. rtcCPDFView?.setLinkHighlight(linkHighlight: linkHighlight)
  107. }
  108. func isLinkHighlight(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  109. let rtcCPDFView = cpdfViews[tag]
  110. rtcCPDFView?.isLinkHighlight(completionHandler: {highlight in
  111. completionHandler(highlight)
  112. })
  113. }
  114. func setVerticalMode(forCPDFViewTag tag : Int, isVerticalMode : Bool) {
  115. let rtcCPDFView = cpdfViews[tag]
  116. rtcCPDFView?.setVerticalMode(isVerticalMode: isVerticalMode)
  117. }
  118. func isVerticalMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  119. let rtcCPDFView = cpdfViews[tag]
  120. rtcCPDFView?.isVerticalMode(completionHandler: {isVertical in
  121. completionHandler(isVertical)
  122. })
  123. }
  124. func setContinueMode(forCPDFViewTag tag : Int, isContinueMode : Bool) {
  125. let rtcCPDFView = cpdfViews[tag]
  126. rtcCPDFView?.setContinueMode(isContinueMode: isContinueMode)
  127. }
  128. func isContinueMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  129. let rtcCPDFView = cpdfViews[tag]
  130. rtcCPDFView?.isContinueMode(completionHandler: {isContinue in
  131. completionHandler(isContinue)
  132. })
  133. }
  134. func setDoublePageMode(forCPDFViewTag tag : Int, isDoublePageMode : Bool) {
  135. let rtcCPDFView = cpdfViews[tag]
  136. rtcCPDFView?.setDoublePageMode(isDoublePageMode: isDoublePageMode)
  137. }
  138. func isDoublePageMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  139. let rtcCPDFView = cpdfViews[tag]
  140. rtcCPDFView?.isDoublePageMode(completionHandler: {isDoublePage in
  141. completionHandler(isDoublePage)
  142. })
  143. }
  144. func setCoverPageMode(forCPDFViewTag tag : Int, isCoverPageMode : Bool) {
  145. let rtcCPDFView = cpdfViews[tag]
  146. rtcCPDFView?.setCoverPageMode(isCoverPageMode: isCoverPageMode)
  147. }
  148. func isCoverPageMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  149. let rtcCPDFView = cpdfViews[tag]
  150. rtcCPDFView?.isCoverPageMode(completionHandler: { isCoverPageMode in
  151. completionHandler(isCoverPageMode)
  152. })
  153. }
  154. func setCropMode(forCPDFViewTag tag : Int, isCropMode : Bool) {
  155. let rtcCPDFView = cpdfViews[tag]
  156. rtcCPDFView?.setCropMode(isCropMode: isCropMode)
  157. }
  158. func isCropMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  159. let rtcCPDFView = cpdfViews[tag]
  160. rtcCPDFView?.isCropMode(completionHandler: completionHandler)
  161. }
  162. func getFileName(forCPDFViewTag tag : Int, completionHandler: @escaping (String) -> Void){
  163. let rtcCPDFView = cpdfViews[tag]
  164. rtcCPDFView?.getFileName(completionHandler: completionHandler)
  165. }
  166. func isEncrypted(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  167. let rtcCPDFView = cpdfViews[tag]
  168. rtcCPDFView?.isEncrypted(completionHandler: completionHandler)
  169. }
  170. func isImageDoc(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  171. let rtcCPDFView = cpdfViews[tag]
  172. rtcCPDFView?.isImageDoc(completionHandler: completionHandler)
  173. }
  174. func getPermissions(forCPDFViewTag tag : Int, completionHandler: @escaping (NSNumber) -> Void) {
  175. let rtcCPDFView = cpdfViews[tag]
  176. rtcCPDFView?.getPermissions(completionHandler: completionHandler)
  177. }
  178. func getPageCount(forCPDFViewTag tag : Int, completionHandler: @escaping (NSNumber) -> Void) {
  179. let rtcCPDFView = cpdfViews[tag]
  180. rtcCPDFView?.getPageCount(completionHandler: completionHandler)
  181. }
  182. func checkOwnerUnlocked(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void) {
  183. let rtcCPDFView = cpdfViews[tag]
  184. rtcCPDFView?.checkOwnerUnlocked(completionHandler: completionHandler)
  185. }
  186. func checkOwnerPassword(forCPDFViewTag tag : Int, password : String, completionHandler: @escaping (Bool) -> Void) {
  187. let rtcCPDFView = cpdfViews[tag]
  188. rtcCPDFView?.checkOwnerPassword(password: password, completionHandler: completionHandler)
  189. }
  190. func getEncryptAlgo(forCPDFViewTag tag : Int, completionHandler: @escaping (String) -> Void) {
  191. let rtcCPDFView = cpdfViews[tag]
  192. rtcCPDFView?.getEncryptAlgo(completionHandler: completionHandler)
  193. }
  194. // MARK: - RCTCPDFViewDelegate
  195. func cpdfViewAttached(_ cpdfView: RCTCPDFView) {
  196. cpdfViews[cpdfView.reactTag.intValue] = cpdfView
  197. }
  198. func saveDocumentChange(_ cpdfView: RCTCPDFView) {
  199. if let onChange = cpdfView.onChange {
  200. onChange(["saveDocument": "saveDocument"])
  201. }
  202. }
  203. func onPageChanged(_ cpdfView: RCTCPDFView, pageIndex: Int) {
  204. if let onChange = cpdfView.onChange {
  205. onChange(["onPageChanged": pageIndex])
  206. }
  207. }
  208. }