RCTCPDFViewManager.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. // MARK: - Document Methods
  26. func saveDocument(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
  27. let rtcCPDFView = cpdfViews[tag]
  28. rtcCPDFView?.saveDocument(completionHandler: { success in
  29. completionHandler(success)
  30. if (success) {
  31. if let onChange = rtcCPDFView?.onChange {
  32. onChange(["saveDocument": "saveDocument"])
  33. }
  34. }
  35. })
  36. }
  37. func setMargins(forCPDFViewTag tag: Int, left : Int, top : Int, right : Int, bottom : Int) {
  38. let rtcCPDFView = cpdfViews[tag]
  39. rtcCPDFView?.setMargins(left: left, top: top, right: right, bottom: bottom)
  40. }
  41. func removeAllAnnotations(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
  42. let rtcCPDFView = cpdfViews[tag]
  43. rtcCPDFView?.removeAllAnnotations(completionHandler: { success in
  44. completionHandler(success)
  45. })
  46. }
  47. func importAnnotations(forCPDFViewTag tag: Int, xfdfFile : URL, completionHandler: @escaping (Bool) -> Void) {
  48. let rtcCPDFView = cpdfViews[tag]
  49. rtcCPDFView?.importAnnotations(xfdfFile: xfdfFile, completionHandler: { success in
  50. completionHandler(success)
  51. })
  52. }
  53. func exportAnnotations(forCPDFViewTag tag: Int, completionHandler: @escaping (String) -> Void) {
  54. let rtcCPDFView = cpdfViews[tag]
  55. rtcCPDFView?.exportAnnotations(completionHandler: { success in
  56. completionHandler(success)
  57. })
  58. }
  59. func setDisplayPageIndex(forCPDFViewTag tag : Int, pageIndex : Int) {
  60. let rtcCPDFView = cpdfViews[tag]
  61. rtcCPDFView?.setDisplayPageIndex(pageIndex: pageIndex)
  62. }
  63. func getCurrentPageIndex(forCPDFViewTag tag: Int, completionHandler: @escaping (Int) -> Void) {
  64. let rtcCPDFView = cpdfViews[tag]
  65. rtcCPDFView?.getCurrentPageIndex(completionHandler: { pageIndex in
  66. completionHandler(pageIndex)
  67. })
  68. }
  69. func hasChange(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
  70. let rtcCPDFView = cpdfViews[tag]
  71. rtcCPDFView?.hasChange(completionHandler: { success in
  72. completionHandler(success)
  73. })
  74. }
  75. func setScale(forCPDFViewTag tag : Int, scale : NSNumber) {
  76. let rtcCPDFView = cpdfViews[tag]
  77. rtcCPDFView?.setScale(scale: scale)
  78. }
  79. func getScale(forCPDFViewTag tag : Int, completionHandler: @escaping (NSNumber) -> Void) {
  80. let rtcCPDFView = cpdfViews[tag]
  81. rtcCPDFView?.getScale(completionHandler: { success in
  82. completionHandler(success)
  83. })
  84. }
  85. func setReadBackgroundColor(forCPDFViewTag tag : Int, displayMode : NSString) {
  86. let rtcCPDFView = cpdfViews[tag]
  87. rtcCPDFView?.setReadBackgroundColor(displayMode: displayMode)
  88. }
  89. func getReadBackgroundColor(forCPDFViewTag tag : Int, completionHandler: @escaping (NSString) -> Void){
  90. let rtcCPDFView = cpdfViews[tag]
  91. rtcCPDFView?.getReadbackgroundColor(completionHandler: {color in
  92. completionHandler(color)
  93. })
  94. }
  95. func setFormFieldHighlight(forCPDFViewTag tag : Int, formFieldHighlight : Bool) {
  96. let rtcCPDFView = cpdfViews[tag]
  97. rtcCPDFView?.setFormFieldHighlight(formFieldHighlight: formFieldHighlight)
  98. }
  99. func isFormFieldHighlight(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  100. let rtcCPDFView = cpdfViews[tag]
  101. rtcCPDFView?.isFormFieldHighlight(completionHandler: {highlight in
  102. completionHandler(highlight)
  103. })
  104. }
  105. func setLinkHighlight(forCPDFViewTag tag : Int, linkHighlight : Bool) {
  106. let rtcCPDFView = cpdfViews[tag]
  107. rtcCPDFView?.setLinkHighlight(linkHighlight: linkHighlight)
  108. }
  109. func isLinkHighlight(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  110. let rtcCPDFView = cpdfViews[tag]
  111. rtcCPDFView?.isLinkHighlight(completionHandler: {highlight in
  112. completionHandler(highlight)
  113. })
  114. }
  115. func setVerticalMode(forCPDFViewTag tag : Int, isVerticalMode : Bool) {
  116. let rtcCPDFView = cpdfViews[tag]
  117. rtcCPDFView?.setVerticalMode(isVerticalMode: isVerticalMode)
  118. }
  119. func isVerticalMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  120. let rtcCPDFView = cpdfViews[tag]
  121. rtcCPDFView?.isVerticalMode(completionHandler: {isVertical in
  122. completionHandler(isVertical)
  123. })
  124. }
  125. func setContinueMode(forCPDFViewTag tag : Int, isContinueMode : Bool) {
  126. let rtcCPDFView = cpdfViews[tag]
  127. rtcCPDFView?.setContinueMode(isContinueMode: isContinueMode)
  128. }
  129. func isContinueMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  130. let rtcCPDFView = cpdfViews[tag]
  131. rtcCPDFView?.isContinueMode(completionHandler: {isContinue in
  132. completionHandler(isContinue)
  133. })
  134. }
  135. func setDoublePageMode(forCPDFViewTag tag : Int, isDoublePageMode : Bool) {
  136. let rtcCPDFView = cpdfViews[tag]
  137. rtcCPDFView?.setDoublePageMode(isDoublePageMode: isDoublePageMode)
  138. }
  139. func isDoublePageMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  140. let rtcCPDFView = cpdfViews[tag]
  141. rtcCPDFView?.isDoublePageMode(completionHandler: {isDoublePage in
  142. completionHandler(isDoublePage)
  143. })
  144. }
  145. func setCoverPageMode(forCPDFViewTag tag : Int, isCoverPageMode : Bool) {
  146. let rtcCPDFView = cpdfViews[tag]
  147. rtcCPDFView?.setCoverPageMode(isCoverPageMode: isCoverPageMode)
  148. }
  149. func isCoverPageMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  150. let rtcCPDFView = cpdfViews[tag]
  151. rtcCPDFView?.isCoverPageMode(completionHandler: { isCoverPageMode in
  152. completionHandler(isCoverPageMode)
  153. })
  154. }
  155. func setCropMode(forCPDFViewTag tag : Int, isCropMode : Bool) {
  156. let rtcCPDFView = cpdfViews[tag]
  157. rtcCPDFView?.setCropMode(isCropMode: isCropMode)
  158. }
  159. func isCropMode(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  160. let rtcCPDFView = cpdfViews[tag]
  161. rtcCPDFView?.isCropMode(completionHandler: completionHandler)
  162. }
  163. func setPreviewMode(forCPDFViewTag tag : Int, viewMode : String) {
  164. let rtcCPDFView = cpdfViews[tag]
  165. rtcCPDFView?.setPreviewMode(viewMode: viewMode)
  166. }
  167. func getPreviewMode(forCPDFViewTag tag : Int, completionHandler: @escaping (String) -> Void) {
  168. let rtcCPDFView = cpdfViews[tag]
  169. rtcCPDFView?.getPreviewMode(completionHandler: { mode in
  170. completionHandler(mode)
  171. })
  172. }
  173. func showThumbnailView(forCPDFViewTag tag : Int, isEditMode : Bool) {
  174. let rtcCPDFView = cpdfViews[tag]
  175. rtcCPDFView?.showThumbnailView(isEditMode: isEditMode)
  176. }
  177. func showBotaView(forCPDFViewTag tag : Int) {
  178. let rtcCPDFView = cpdfViews[tag]
  179. rtcCPDFView?.showBotaView()
  180. }
  181. func showAddWatermarkView(forCPDFViewTag tag : Int, saveAsNewFile : Bool) {
  182. let rtcCPDFView = cpdfViews[tag]
  183. rtcCPDFView?.showAddWatermarkView(saveAsNewFile: saveAsNewFile)
  184. }
  185. func showSecurityView(forCPDFViewTag tag : Int) {
  186. let rtcCPDFView = cpdfViews[tag]
  187. rtcCPDFView?.showSecurityView()
  188. }
  189. func showDisplaySettingView(forCPDFViewTag tag : Int) {
  190. let rtcCPDFView = cpdfViews[tag]
  191. rtcCPDFView?.showDisplaySettingView()
  192. }
  193. func enterSnipMode(forCPDFViewTag tag : Int) {
  194. let rtcCPDFView = cpdfViews[tag]
  195. rtcCPDFView?.enterSnipMode()
  196. }
  197. func exitSnipMode(forCPDFViewTag tag : Int) {
  198. let rtcCPDFView = cpdfViews[tag]
  199. rtcCPDFView?.exitSnipMode()
  200. }
  201. func open(forCPDFViewTag : Int, document : URL, password : String, completionHandler: @escaping (Bool) -> Void) {
  202. let rtcCPDFView = cpdfViews[forCPDFViewTag]
  203. rtcCPDFView?.open(document: document, password: password, completionHandler: { success in
  204. completionHandler(success)
  205. })
  206. }
  207. func getFileName(forCPDFViewTag tag : Int, completionHandler: @escaping (String) -> Void){
  208. let rtcCPDFView = cpdfViews[tag]
  209. rtcCPDFView?.getFileName(completionHandler: completionHandler)
  210. }
  211. func isEncrypted(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  212. let rtcCPDFView = cpdfViews[tag]
  213. rtcCPDFView?.isEncrypted(completionHandler: completionHandler)
  214. }
  215. func isImageDoc(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void){
  216. let rtcCPDFView = cpdfViews[tag]
  217. rtcCPDFView?.isImageDoc(completionHandler: completionHandler)
  218. }
  219. func getPermissions(forCPDFViewTag tag : Int, completionHandler: @escaping (NSNumber) -> Void) {
  220. let rtcCPDFView = cpdfViews[tag]
  221. rtcCPDFView?.getPermissions(completionHandler: completionHandler)
  222. }
  223. func getPageCount(forCPDFViewTag tag : Int, completionHandler: @escaping (NSNumber) -> Void) {
  224. let rtcCPDFView = cpdfViews[tag]
  225. rtcCPDFView?.getPageCount(completionHandler: completionHandler)
  226. }
  227. func checkOwnerUnlocked(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void) {
  228. let rtcCPDFView = cpdfViews[tag]
  229. rtcCPDFView?.checkOwnerUnlocked(completionHandler: completionHandler)
  230. }
  231. func checkOwnerPassword(forCPDFViewTag tag : Int, password : String, completionHandler: @escaping (Bool) -> Void) {
  232. let rtcCPDFView = cpdfViews[tag]
  233. rtcCPDFView?.checkOwnerPassword(password: password, completionHandler: completionHandler)
  234. }
  235. func removePassword(forCPDFViewTag tag : Int, completionHandler: @escaping (Bool) -> Void) {
  236. let rtcCPDFView = cpdfViews[tag]
  237. rtcCPDFView?.removePassword(completionHandler: { success in
  238. completionHandler(success)
  239. })
  240. }
  241. func setPassword(forCPDFViewTag tag : Int, info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
  242. let rtcCPDFView = cpdfViews[tag]
  243. rtcCPDFView?.setPassword(info: info, completionHandler: { success in
  244. completionHandler(success)
  245. })
  246. }
  247. func getEncryptAlgo(forCPDFViewTag tag : Int, completionHandler: @escaping (String) -> Void) {
  248. let rtcCPDFView = cpdfViews[tag]
  249. rtcCPDFView?.getEncryptAlgo(completionHandler: completionHandler)
  250. }
  251. func printDocument(forCPDFViewTag tag : Int){
  252. let rtcCPDFView = cpdfViews[tag]
  253. rtcCPDFView?.printDocument()
  254. }
  255. func importWidgets(forCPDFViewTag tag: Int, xfdfFile : URL, completionHandler: @escaping (Bool) -> Void) {
  256. let rtcCPDFView = cpdfViews[tag]
  257. rtcCPDFView?.importWidgets(xfdfFile: xfdfFile, completionHandler: { success in
  258. completionHandler(success)
  259. })
  260. }
  261. func getDocumentPath(forCPDFViewTag tag : Int, completionHandler: @escaping (String) -> Void){
  262. let rtcCPDFView = cpdfViews[tag]
  263. rtcCPDFView?.getDocumentPath(completionHandler: { path in
  264. completionHandler(path)
  265. })
  266. }
  267. func exportWidgets(forCPDFViewTag tag: Int, completionHandler: @escaping (String) -> Void) {
  268. let rtcCPDFView = cpdfViews[tag]
  269. rtcCPDFView?.exportWidgets(completionHandler: { success in
  270. completionHandler(success)
  271. })
  272. }
  273. func flattenAllPages(forCPDFViewTag tag : Int, savePath: URL, fontSubset: Bool, completionHandler: @escaping (Bool) -> Void) {
  274. let rtcCPDFView = cpdfViews[tag]
  275. rtcCPDFView?.flattenAllPages(savePath: savePath, fontSubset: fontSubset, completionHandler: { success in
  276. completionHandler(success)
  277. })
  278. }
  279. func saveAs(forCPDFViewTag tag : Int, savePath: URL, removeSecurity: Bool, fontSubset: Bool, completionHandler: @escaping (Bool) -> Void) {
  280. let rtcCPDFView = cpdfViews[tag]
  281. rtcCPDFView?.saveAs(savePath: savePath, removeSecurity: removeSecurity, fontSubset: fontSubset, completionHandler: { success in
  282. completionHandler(success)
  283. })
  284. }
  285. func importDocument(forCPDFViewTag tag : Int, filePath: URL, info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
  286. let rtcCPDFView = cpdfViews[tag]
  287. rtcCPDFView?.importDocument(filePath, info, completionHandler: { success in
  288. completionHandler(success)
  289. })
  290. }
  291. func splitDocumentPages(forCPDFViewTag tag : Int, savePath: URL, pages: [Int], completionHandler: @escaping (Bool) -> Void) {
  292. let rtcCPDFView = cpdfViews[tag]
  293. rtcCPDFView?.splitDocumentPages(savePath: savePath, pages: pages, completionHandler: { success in
  294. completionHandler(success)
  295. })
  296. }
  297. // MARK: - Pages Methods
  298. func getAnnotations(forCPDFViewTag tag : Int, pageIndex: Int, completionHandler: @escaping ([Dictionary<String, Any>]) -> Void) {
  299. let rtcCPDFView = cpdfViews[tag]
  300. let page = rtcCPDFView?.getPage(UInt(pageIndex))
  301. let pageUtil = RCTCPDFPageUtil(page: page)
  302. pageUtil.pageIndex = pageIndex
  303. let annotations = pageUtil.getAnnotations()
  304. completionHandler(annotations)
  305. }
  306. func getWidgets(forCPDFViewTag tag : Int, pageIndex: Int, completionHandler: @escaping ([Dictionary<String, Any>]) -> Void) {
  307. let rtcCPDFView = cpdfViews[tag]
  308. let page = rtcCPDFView?.getPage(UInt(pageIndex))
  309. let pageUtil = RCTCPDFPageUtil(page: page)
  310. pageUtil.pageIndex = pageIndex
  311. let widgets = pageUtil.getForms()
  312. completionHandler(widgets)
  313. }
  314. func setWidgetIsChecked(forCPDFViewTag tag :Int, pageIndex: Int, uuid: String, isChecked: Bool) {
  315. let rtcCPDFView = cpdfViews[tag]
  316. let page = rtcCPDFView?.getPage(UInt(pageIndex))
  317. let pageUtil = RCTCPDFPageUtil(page: page)
  318. pageUtil.pageIndex = pageIndex
  319. pageUtil.setWidgetIsChecked(uuid: uuid, isChecked: isChecked)
  320. }
  321. func setTextWidgetText(forCPDFViewTag tag :Int, pageIndex: Int, uuid: String, text: String) {
  322. let rtcCPDFView = cpdfViews[tag]
  323. let page = rtcCPDFView?.getPage(UInt(pageIndex))
  324. let pageUtil = RCTCPDFPageUtil(page: page)
  325. pageUtil.pageIndex = pageIndex
  326. pageUtil.setTextWidgetText(uuid: uuid, text: text)
  327. }
  328. func addWidgetImageSignature(forCPDFViewTag tag :Int, pageIndex: Int, uuid: String, imagePath: URL, completionHandler: @escaping (Bool) -> Void) {
  329. let rtcCPDFView = cpdfViews[tag]
  330. let page = rtcCPDFView?.getPage(UInt(pageIndex))
  331. let pageUtil = RCTCPDFPageUtil(page: page)
  332. pageUtil.pageIndex = pageIndex
  333. pageUtil.addWidgetImageSignature(uuid: uuid, imagePath: imagePath) { success in
  334. completionHandler(success)
  335. }
  336. }
  337. func updateAp(forCPDFViewTag tag : Int, pageIndex: Int, uuid: String) {
  338. let rtcCPDFView = cpdfViews[tag]
  339. let page = rtcCPDFView?.getPage(UInt(pageIndex))
  340. let pageUtil = RCTCPDFPageUtil(page: page)
  341. pageUtil.pageIndex = pageIndex
  342. pageUtil.updateAp(uuid: uuid)
  343. rtcCPDFView?.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
  344. }
  345. func reloadPages(forCPDFViewTag tag : Int) {
  346. let rtcCPDFView = cpdfViews[tag]
  347. rtcCPDFView?.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
  348. rtcCPDFView?.pdfViewController?.pdfListView?.layoutDocumentView()
  349. }
  350. // MARK: - RCTCPDFViewDelegate
  351. func cpdfViewAttached(_ cpdfView: RCTCPDFView) {
  352. cpdfViews[cpdfView.reactTag.intValue] = cpdfView
  353. }
  354. func saveDocumentChange(_ cpdfView: RCTCPDFView) {
  355. if let onChange = cpdfView.onChange {
  356. onChange(["saveDocument": "saveDocument"])
  357. }
  358. }
  359. func onPageChanged(_ cpdfView: RCTCPDFView, pageIndex: Int) {
  360. if let onChange = cpdfView.onChange {
  361. onChange(["onPageChanged": pageIndex])
  362. }
  363. }
  364. }