RCTDocumentManager.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // RCTDocumentManager.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(CPDFViewManager)
  15. class RCTDocumentManager: NSObject, RCTBridgeModule {
  16. static func moduleName() -> String! {
  17. return "RCTDocumentManager"
  18. }
  19. internal var bridge: RCTBridge!
  20. @objc func readerView() -> RCTCPDFReaderView {
  21. self.bridge.module(for: RCTCPDFReaderView.self) as! RCTCPDFReaderView
  22. }
  23. @objc(save: withResolver: withRejecter:)
  24. func save(tag: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
  25. DispatchQueue.main.async {
  26. let reader = self.readerView()
  27. reader.saveDocument(forCPDFViewTag: tag) { success in
  28. if success {
  29. resolve(success)
  30. } else {
  31. reject("save_failed", "Failed to save document", nil);
  32. }
  33. }
  34. }
  35. }
  36. @objc(setMargins: withEdges:)
  37. func setMargins(tag : Int, edges: [Int]) -> Void {
  38. DispatchQueue.main.async {
  39. let reader = self.readerView()
  40. if edges.count == 4 {
  41. reader.setMargins(forCPDFViewTag: tag, left: edges[0], top: edges[1], right: edges[2], bottom: edges[3])
  42. }
  43. }
  44. }
  45. @objc(removeAllAnnotations: withResolver: withRejecter:)
  46. func removeAllAnnotations(tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  47. DispatchQueue.main.async {
  48. let reader = self.readerView()
  49. reader.removeAllAnnotations(forCPDFViewTag: tag) { success in
  50. if success {
  51. resolve(success)
  52. } else {
  53. reject("remove_all_annotation_failed", "Failed to remove all annotation", nil);
  54. }
  55. }
  56. }
  57. }
  58. @objc(importAnnotations: withXfdfFile: withResolver: withRejecter:)
  59. func importAnnotations(tag : Int, xfdfFile : URL, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
  60. DispatchQueue.main.async {
  61. let reader = self.readerView()
  62. reader.importAnnotations(forCPDFViewTag: tag, xfdfFile: xfdfFile) { success in
  63. if success {
  64. resolve(success)
  65. } else {
  66. reject("import_annotation_failed", "Failed to import annotation", nil);
  67. }
  68. }
  69. }
  70. }
  71. @objc(exportAnnotations: withResolver: withRejecter:)
  72. func exportAnnotations(tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  73. DispatchQueue.main.async {
  74. let reader = self.readerView()
  75. reader.exportAnnotations(forCPDFViewTag: tag) { xfdfFilePath in
  76. resolve(xfdfFilePath)
  77. }
  78. }
  79. }
  80. @objc(setDisplayPageIndex: withPageIndex:)
  81. func setDisplayPageIndex(tag : Int, pageIndex : Int) -> Void {
  82. DispatchQueue.main.async {
  83. let reader = self.readerView()
  84. reader.setDisplayPageIndex(forCPDFViewTag: tag, pageIndex: pageIndex)
  85. }
  86. }
  87. @objc(getCurrentPageIndex: withResolver: withRejecter:)
  88. func getCurrentPageIndex(tag : Int,resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
  89. DispatchQueue.main.async {
  90. let reader = self.readerView()
  91. reader.getCurrentPageIndex(forCPDFViewTag: tag) { pageIndex in
  92. resolve(pageIndex)
  93. }
  94. }
  95. }
  96. @objc(hasChange: withResolver: withRejecter:)
  97. func hasChange(tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  98. DispatchQueue.main.async {
  99. let reader = self.readerView()
  100. reader.hasChange(forCPDFViewTag: tag) { success in
  101. resolve(success)
  102. }
  103. }
  104. }
  105. @objc(setScale: withScaleValue:)
  106. func setScale(tag : Int, scale : NSNumber) -> Void {
  107. DispatchQueue.main.async {
  108. let reader = self.readerView()
  109. reader.setScale(forCPDFViewTag: tag, scale: scale)
  110. }
  111. }
  112. @objc(getScale: withResolver: withRejecter:)
  113. func getScale(tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
  114. DispatchQueue.main.async {
  115. let reader = self.readerView()
  116. reader.getScale(forCPDFViewTag: tag) { scale in
  117. resolve(scale)
  118. }
  119. }
  120. }
  121. @objc(setReadBackgroundColor: withThemes:)
  122. func setReadBackgroundColor(tag : Int, themes : NSDictionary) -> Void{
  123. DispatchQueue.main.async {
  124. let reader = self.readerView()
  125. let displayMode = themes["displayMode"] as? NSString ?? "light"
  126. reader.setReadBackgroundColor(forCPDFViewTag: tag, displayMode: displayMode)
  127. }
  128. }
  129. @objc(getReadBackgroundColor: withResolver: withRejecter:)
  130. func getReadBackgroundColor(tag: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
  131. DispatchQueue.main.async {
  132. let reader = self.readerView()
  133. reader.getReadBackgroundColor(forCPDFViewTag: tag, completionHandler: {color in
  134. resolve(color)
  135. })
  136. }
  137. }
  138. @objc(setFormFieldHighlight: withFormFieldHighlight:)
  139. func setFormFieldHighlight(tag: Int, formFieldHighlight : Bool) {
  140. DispatchQueue.main.async {
  141. let reader = self.readerView()
  142. reader.setFormFieldHighlight(forCPDFViewTag: tag, formFieldHighlight: formFieldHighlight)
  143. }
  144. }
  145. @objc(isFormFieldHighlight: withResolver: withRejecter:)
  146. func isFormFieldHighlight(tag: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  147. DispatchQueue.main.async {
  148. let reader = self.readerView()
  149. reader.isFormFieldHighlight(forCPDFViewTag: tag, completionHandler: {highlight in
  150. resolve(highlight)
  151. })
  152. }
  153. }
  154. @objc(setLinkHighlight: withLinkHighlight:)
  155. func setLinkHighlight(tag: Int, linkHighlight : Bool) {
  156. DispatchQueue.main.async {
  157. let reader = self.readerView()
  158. reader.setLinkHighlight(forCPDFViewTag: tag, linkHighlight: linkHighlight)
  159. }
  160. }
  161. @objc(isLinkHighlight: withResolver: withRejecter:)
  162. func isLinkHighlight(tag: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  163. DispatchQueue.main.async {
  164. let reader = self.readerView()
  165. reader.isLinkHighlight(forCPDFViewTag: tag, completionHandler: {highlight in
  166. resolve(highlight)
  167. })
  168. }
  169. }
  170. @objc(setVerticalMode: withVerticalMode:)
  171. func setVerticalMode(tag : Int, isVerticalMode : Bool){
  172. DispatchQueue.main.async {
  173. let reader = self.readerView()
  174. reader.setVerticalMode(forCPDFViewTag: tag, isVerticalMode: isVerticalMode)
  175. }
  176. }
  177. @objc(isVerticalMode: withResolver: withRejecter:)
  178. func isVerticalMode(tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
  179. DispatchQueue.main.async {
  180. let reader = self.readerView()
  181. reader.isVerticalMode(forCPDFViewTag: tag, completionHandler: {isVertical in
  182. resolve(isVertical)
  183. })
  184. }
  185. }
  186. @objc(setContinueMode: withContiueMode:)
  187. func setContinueMode(forCPDFViewTag tag : Int, isContinueMode : Bool) {
  188. DispatchQueue.main.async {
  189. let reader = self.readerView()
  190. reader.setContinueMode(forCPDFViewTag: tag, isContinueMode: isContinueMode)
  191. }
  192. }
  193. @objc(isContinueMode: withResolver: withRejecter:)
  194. func isContinueMode(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  195. DispatchQueue.main.async {
  196. let reader = self.readerView()
  197. reader.isContinueMode(forCPDFViewTag: tag, completionHandler: {isContinue in
  198. resolve(isContinue)
  199. })
  200. }
  201. }
  202. @objc(setDoublePageMode: withDoublePageMode:)
  203. func setDoublePageMode(forCPDFViewTag tag : Int, isDoublePageMode : Bool) {
  204. DispatchQueue.main.async {
  205. let reader = self.readerView()
  206. reader.setDoublePageMode(forCPDFViewTag: tag, isDoublePageMode: isDoublePageMode)
  207. }
  208. }
  209. @objc(isDoublePageMode: withResolver: withRejecter:)
  210. func isDoublePageMode(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  211. DispatchQueue.main.async {
  212. let reader = self.readerView()
  213. reader.isDoublePageMode(forCPDFViewTag: tag, completionHandler: {isDoublePageMode in
  214. resolve(isDoublePageMode)
  215. })
  216. }
  217. }
  218. @objc(setCoverPageMode: withCoverPageMode:)
  219. func setCoverPageMode(forCPDFViewTag tag : Int, isCoverPageMode : Bool) {
  220. DispatchQueue.main.async {
  221. let reader = self.readerView()
  222. reader.setCoverPageMode(forCPDFViewTag: tag, isCoverPageMode: isCoverPageMode)
  223. }
  224. }
  225. @objc(isCoverPageMode: withResolver: withRejecter:)
  226. func isCoverPageMode(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  227. DispatchQueue.main.async {
  228. let reader = self.readerView()
  229. reader.isCoverPageMode(forCPDFViewTag: tag, completionHandler: {isCoverPageMode in
  230. resolve(isCoverPageMode)
  231. })
  232. }
  233. }
  234. @objc(setCropMode: withCropMode:)
  235. func setCropMode(forCPDFViewTag tag : Int, isCropMode : Bool) {
  236. DispatchQueue.main.async {
  237. let reader = self.readerView()
  238. reader.setCropMode(forCPDFViewTag: tag, isCropMode: isCropMode)
  239. }
  240. }
  241. @objc(isCropMode: withResolver: withRejecter:)
  242. func isCropMode(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  243. DispatchQueue.main.async {
  244. let reader = self.readerView()
  245. reader.isCropMode(forCPDFViewTag: tag, completionHandler: {isCropMode in
  246. resolve(isCropMode)
  247. })
  248. }
  249. }
  250. @objc(getFileName: withResolver: withRejecter:)
  251. func getFileName(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  252. DispatchQueue.main.async {
  253. let reader = self.readerView()
  254. reader.getFileName(forCPDFViewTag: tag, completionHandler: {fileName in
  255. resolve(fileName)
  256. })
  257. }
  258. }
  259. @objc(isEncrypted: withResolver: withRejecter:)
  260. func isEncrypted(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  261. DispatchQueue.main.async {
  262. let reader = self.readerView()
  263. reader.isEncrypted(forCPDFViewTag: tag, completionHandler: {isEncrypted in
  264. resolve(isEncrypted)
  265. })
  266. }
  267. }
  268. @objc(isImageDoc: withResolver: withRejecter:)
  269. func isImageDoc(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  270. DispatchQueue.main.async {
  271. let reader = self.readerView()
  272. reader.isImageDoc(forCPDFViewTag: tag, completionHandler: {isImageDoc in
  273. resolve(isImageDoc)
  274. })
  275. }
  276. }
  277. @objc(getPermissions: withResolver: withRejecter:)
  278. func getPermissions(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  279. DispatchQueue.main.async {
  280. let reader = self.readerView()
  281. reader.getPermissions(forCPDFViewTag: tag, completionHandler: {permissions in
  282. resolve(permissions)
  283. })
  284. }
  285. }
  286. @objc(getPageCount: withResolver: withRejecter:)
  287. func getPageCount(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  288. DispatchQueue.main.async {
  289. let reader = self.readerView()
  290. reader.getPageCount(forCPDFViewTag: tag, completionHandler: {pageCount in
  291. resolve(pageCount)
  292. })
  293. }
  294. }
  295. @objc(checkOwnerUnlocked: withResolver: withRejecter:)
  296. func checkOwnerUnlocked(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  297. DispatchQueue.main.async {
  298. let reader = self.readerView()
  299. reader.checkOwnerUnlocked(forCPDFViewTag: tag, completionHandler: {unlocked in
  300. resolve(unlocked)
  301. })
  302. }
  303. }
  304. @objc(checkOwnerPassword: withPassword: withResolver: withRejecter:)
  305. func checkOwnerPassword(forCPDFViewTag tag : Int, password : String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  306. DispatchQueue.main.async {
  307. let reader = self.readerView()
  308. reader.checkOwnerPassword(forCPDFViewTag: tag, password: password, completionHandler: {correct in
  309. resolve(correct)
  310. })
  311. }
  312. }
  313. @objc(getEncryptAlgo: withResolver: withRejecter:)
  314. func getEncryptAlgo(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
  315. DispatchQueue.main.async {
  316. let reader = self.readerView()
  317. reader.getEncryptAlgo(forCPDFViewTag: tag, completionHandler: {encryptAlgo in
  318. resolve(encryptAlgo)
  319. })
  320. }
  321. }
  322. }