RCTCPDFViewManager.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // RCTCPDFViewManager.swift
  3. // react-native-compdfkit-pdf
  4. //
  5. // Copyright © 2014-2025 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 setPreviewMode(forCPDFViewTag tag : Int, viewMode : String) {
  163. let rtcCPDFView = cpdfViews[tag]
  164. rtcCPDFView?.setPreviewMode(viewMode: viewMode)
  165. }
  166. func getPreviewMode(forCPDFViewTag tag : Int, completionHandler: @escaping (String) -> Void) {
  167. let rtcCPDFView = cpdfViews[tag]
  168. rtcCPDFView?.getPreviewMode(completionHandler: { mode in
  169. completionHandler(mode)
  170. })
  171. }
  172. func showThumbnailView(forCPDFViewTag tag : Int, isEditMode : Bool) {
  173. let rtcCPDFView = cpdfViews[tag]
  174. rtcCPDFView?.showThumbnailView(isEditMode: isEditMode)
  175. }
  176. func showBotaView(forCPDFViewTag tag : Int) {
  177. let rtcCPDFView = cpdfViews[tag]
  178. rtcCPDFView?.showBotaView()
  179. }
  180. func showAddWatermarkView(forCPDFViewTag tag : Int) {
  181. let rtcCPDFView = cpdfViews[tag]
  182. rtcCPDFView?.showAddWatermarkView()
  183. }
  184. func showSecurityView(forCPDFViewTag tag : Int) {
  185. let rtcCPDFView = cpdfViews[tag]
  186. rtcCPDFView?.showSecurityView()
  187. }
  188. func showDisplaySettingView(forCPDFViewTag tag : Int) {
  189. let rtcCPDFView = cpdfViews[tag]
  190. rtcCPDFView?.showDisplaySettingView()
  191. }
  192. func enterSnipMode(forCPDFViewTag tag : Int) {
  193. let rtcCPDFView = cpdfViews[tag]
  194. rtcCPDFView?.enterSnipMode()
  195. }
  196. func exitSnipMode(forCPDFViewTag tag : Int) {
  197. let rtcCPDFView = cpdfViews[tag]
  198. rtcCPDFView?.exitSnipMode()
  199. }
  200. func open(forCPDFViewTag : Int, document : URL, password : String, completionHandler: @escaping (Bool) -> Void) {
  201. let rtcCPDFView = cpdfViews[forCPDFViewTag]
  202. rtcCPDFView?.open(document: document, password: password, completionHandler: { success in
  203. completionHandler(success)
  204. })
  205. }
  206. func getFileName(forCPDFViewTag tag : Int, completionHandler: @escaping (String) -> Void){
  207. let rtcCPDFView = cpdfViews[tag]
  208. rtcCPDFView?.getFileName(completionHandler: completionHandler)
  209. }
  210. func isEncrypted(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  211. let rtcCPDFView = cpdfViews[tag]
  212. rtcCPDFView?.isEncrypted(completionHandler: completionHandler)
  213. }
  214. func isImageDoc(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  215. let rtcCPDFView = cpdfViews[tag]
  216. rtcCPDFView?.isImageDoc(completionHandler: completionHandler)
  217. }
  218. func getPermissions(forCPDFViewTag tag : Int, completionHandler: @escaping (NSNumber) -> Void) {
  219. let rtcCPDFView = cpdfViews[tag]
  220. rtcCPDFView?.getPermissions(completionHandler: completionHandler)
  221. }
  222. func getPageCount(forCPDFViewTag tag : Int, completionHandler: @escaping (NSNumber) -> Void) {
  223. let rtcCPDFView = cpdfViews[tag]
  224. rtcCPDFView?.getPageCount(completionHandler: completionHandler)
  225. }
  226. func checkOwnerUnlocked(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void) {
  227. let rtcCPDFView = cpdfViews[tag]
  228. rtcCPDFView?.checkOwnerUnlocked(completionHandler: completionHandler)
  229. }
  230. func checkOwnerPassword(forCPDFViewTag tag : Int, password : String, completionHandler: @escaping (Bool) -> Void) {
  231. let rtcCPDFView = cpdfViews[tag]
  232. rtcCPDFView?.checkOwnerPassword(password: password, completionHandler: completionHandler)
  233. }
  234. func removePassword(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void) {
  235. let rtcCPDFView = cpdfViews[tag]
  236. rtcCPDFView?.removePassword(completionHandler: { success in
  237. completionHandler(success)
  238. })
  239. }
  240. func setPassword(forCPDFViewTag tag : Int, info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
  241. let rtcCPDFView = cpdfViews[tag]
  242. rtcCPDFView?.setPassword(info: info, completionHandler: { success in
  243. completionHandler(success)
  244. })
  245. }
  246. func getEncryptAlgo(forCPDFViewTag tag : Int, completionHandler: @escaping (String) -> Void) {
  247. let rtcCPDFView = cpdfViews[tag]
  248. rtcCPDFView?.getEncryptAlgo(completionHandler: completionHandler)
  249. }
  250. // MARK: - RCTCPDFViewDelegate
  251. func cpdfViewAttached(_ cpdfView: RCTCPDFView) {
  252. cpdfViews[cpdfView.reactTag.intValue] = cpdfView
  253. }
  254. func saveDocumentChange(_ cpdfView: RCTCPDFView) {
  255. if let onChange = cpdfView.onChange {
  256. onChange(["saveDocument": "saveDocument"])
  257. }
  258. }
  259. func onPageChanged(_ cpdfView: RCTCPDFView, pageIndex: Int) {
  260. if let onChange = cpdfView.onChange {
  261. onChange(["onPageChanged": pageIndex])
  262. }
  263. }
  264. }