RCTCPDFView.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. //
  2. // RCTCPDFView.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_Tools
  14. import ComPDFKit
  15. protocol RCTCPDFViewDelegate: AnyObject {
  16. func cpdfViewAttached(_ cpdfView: RCTCPDFView)
  17. func saveDocumentChange(_ cpdfView: RCTCPDFView)
  18. func onPageChanged(_ cpdfView: RCTCPDFView, pageIndex: Int)
  19. }
  20. class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
  21. weak var delegate: RCTCPDFViewDelegate?
  22. public var pdfViewController : CPDFViewController?
  23. private var navigationController : CNavigationController?
  24. init() {
  25. super.init(frame: CGRect(x: 0, y: 0, width: 500, height: 400))
  26. }
  27. required init?(coder: NSCoder) {
  28. fatalError("init(coder:) has not been implemented")
  29. }
  30. // MARK: - Private Methods
  31. private func createCPDFView() {
  32. var documentPath = document.path
  33. var success = false
  34. let homeDiectory = NSHomeDirectory()
  35. let bundlePath = Bundle.main.bundlePath
  36. if (documentPath.hasPrefix(homeDiectory) || documentPath.hasPrefix(bundlePath)) {
  37. let documentHome = homeDiectory.appending("/Documents")
  38. if documentPath.hasPrefix(homeDiectory) && documentPath.hasPrefix(documentHome) {
  39. } else {
  40. let fileManager = FileManager.default
  41. let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
  42. let fileName = document.lastPathComponent
  43. let docsFilePath = samplesFilePath + "/" + fileName
  44. if !fileManager.fileExists(atPath: samplesFilePath) {
  45. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  46. }
  47. try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
  48. documentPath = docsFilePath
  49. }
  50. } else {
  51. success = document.startAccessingSecurityScopedResource()
  52. }
  53. let jsonData = CPDFJSONDataParse(String: configuration)
  54. let configurations = jsonData.configuration ?? CPDFConfiguration()
  55. pdfViewController = CPDFViewController(filePath: documentPath, password: password, configuration: configurations)
  56. pdfViewController?.delegate = self
  57. navigationController = CNavigationController(rootViewController: pdfViewController!)
  58. navigationController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  59. navigationController?.view.frame = self.frame
  60. navigationController?.setViewControllers([pdfViewController!], animated: true)
  61. addSubview(navigationController?.view ?? UIView())
  62. self.delegate?.cpdfViewAttached(self)
  63. if success {
  64. document.stopAccessingSecurityScopedResource()
  65. }
  66. }
  67. func insertPDFDocument(_ document: CPDFDocument, Pages pages: [Int], Position index: UInt) -> Bool {
  68. if let pdfListView = self.pdfViewController?.pdfListView {
  69. var _index: UInt = index
  70. if index < 0 || index > pdfListView.document.pageCount {
  71. if Int(index) == -1 {
  72. _index = pdfListView.document.pageCount
  73. } else {
  74. return false
  75. }
  76. }
  77. var indexSet = IndexSet()
  78. for page in pages {
  79. indexSet.insert(IndexSet.Element(page))
  80. }
  81. let success = pdfListView.document.importPages(indexSet, from: document, at: _index)
  82. pdfListView.layoutDocumentView()
  83. return success
  84. } else {
  85. return false
  86. }
  87. }
  88. func extractPDFDocument(_ savePath: URL, Pages pages: [Int]) -> Bool {
  89. if let pdfListView = self.pdfViewController?.pdfListView {
  90. var indexSet = IndexSet()
  91. for page in pages {
  92. indexSet.insert(page)
  93. }
  94. let document = CPDFDocument()
  95. document?.importPages(indexSet, from: pdfListView.document, at: 0)
  96. let success = document?.write(to: savePath, isSaveFontSubset: true) ?? false
  97. return success
  98. } else {
  99. return false
  100. }
  101. }
  102. func getValue<T>(from info: [String: Any]?, key: String, defaultValue: T) -> T {
  103. guard let value = info?[key] as? T else {
  104. return defaultValue
  105. }
  106. return value
  107. }
  108. // MARK: - Page Public Methods
  109. func getPage(_ index: UInt) -> CPDFPage {
  110. if let pdfListView = self.pdfViewController?.pdfListView {
  111. let page = pdfListView.document.page(at: index) ?? CPDFPage()
  112. return page
  113. } else {
  114. return CPDFPage()
  115. }
  116. }
  117. // MARK: - Document Public Methods
  118. func saveDocument(completionHandler: @escaping (Bool) -> Void) {
  119. if (self.pdfViewController?.pdfListView?.isEditing() == true && self.pdfViewController?.pdfListView?.isEdited() == true) {
  120. self.pdfViewController?.pdfListView?.commitEditing()
  121. if self.pdfViewController?.pdfListView?.document.isModified() == true {
  122. let document = self.pdfViewController?.pdfListView?.document
  123. let success = document?.write(to: document?.documentURL ?? URL(fileURLWithPath: ""), isSaveFontSubset: true) ?? false
  124. completionHandler(success)
  125. } else {
  126. completionHandler(true)
  127. }
  128. } else {
  129. if self.pdfViewController?.pdfListView?.document.isModified() == true {
  130. let document = self.pdfViewController?.pdfListView?.document
  131. let success = document?.write(to: document?.documentURL ?? URL(fileURLWithPath: ""), isSaveFontSubset: true) ?? false
  132. completionHandler(success)
  133. } else {
  134. completionHandler(true)
  135. }
  136. }
  137. }
  138. func setMargins(left : Int, top : Int, right : Int, bottom : Int) {
  139. if let pdfListView = self.pdfViewController?.pdfListView {
  140. pdfListView.pageBreakMargins = .init(top: CGFloat(top), left: CGFloat(left), bottom: CGFloat(bottom), right: CGFloat(right))
  141. pdfListView.layoutDocumentView()
  142. }
  143. }
  144. func removeAllAnnotations(completionHandler: @escaping (Bool) -> Void) {
  145. if let pdfListView = self.pdfViewController?.pdfListView {
  146. let pageCount = pdfListView.document?.pageCount ?? 0
  147. for i in 0..<pageCount {
  148. let page = pdfListView.document?.page(at: i)
  149. page?.removeAllAnnotations()
  150. }
  151. self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
  152. self.pdfViewController?.pdfListView?.updateActiveAnnotations([])
  153. completionHandler(true)
  154. } else {
  155. completionHandler(false)
  156. }
  157. }
  158. func importAnnotations(xfdfFile : URL, completionHandler: @escaping (Bool) -> Void) {
  159. if let pdfListView = self.pdfViewController?.pdfListView {
  160. let documentFolder = NSHomeDirectory().appending("/Documents/Files")
  161. if !FileManager.default.fileExists(atPath: documentFolder) {
  162. try? FileManager.default.createDirectory(at: URL(fileURLWithPath: documentFolder), withIntermediateDirectories: true, attributes: nil)
  163. }
  164. let documentPath = documentFolder + "/\(xfdfFile.lastPathComponent)"
  165. try? FileManager.default.copyItem(atPath: xfdfFile.path, toPath: documentPath)
  166. if !FileManager.default.fileExists(atPath: documentPath) {
  167. print("fail")
  168. }
  169. let success = pdfListView.document?.importAnnotation(fromXFDFPath: documentPath) ?? false
  170. if success {
  171. self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
  172. }
  173. completionHandler(success)
  174. } else {
  175. completionHandler(false)
  176. }
  177. }
  178. func exportAnnotations(completionHandler: @escaping (String) -> Void) {
  179. if let pdfListView = self.pdfViewController?.pdfListView {
  180. let fileNameWithExtension = pdfListView.document?.documentURL.lastPathComponent ?? ""
  181. let fileName = (fileNameWithExtension as NSString).deletingPathExtension
  182. let documentFolder = NSHomeDirectory().appending("/Documents/\(fileName)_xfdf.xfdf")
  183. let succes = pdfListView.document?.exportAnnotation(toXFDFPath: documentFolder) ?? false
  184. if succes {
  185. completionHandler(documentFolder)
  186. } else {
  187. completionHandler("")
  188. }
  189. } else {
  190. completionHandler("")
  191. }
  192. }
  193. func setDisplayPageIndex(pageIndex : Int) {
  194. if let pdfListView = self.pdfViewController?.pdfListView {
  195. pdfListView.go(toPageIndex: pageIndex, animated: false)
  196. }
  197. }
  198. func getCurrentPageIndex(completionHandler: @escaping (Int) -> Void) {
  199. if let pdfListView = self.pdfViewController?.pdfListView {
  200. completionHandler(pdfListView.currentPageIndex)
  201. } else {
  202. completionHandler(0)
  203. }
  204. }
  205. func hasChange(completionHandler: @escaping (Bool) -> Void) {
  206. if let pdfListView = self.pdfViewController?.pdfListView {
  207. let success = pdfListView.document?.isModified() ?? false
  208. completionHandler(success)
  209. } else {
  210. completionHandler(false)
  211. }
  212. }
  213. func setScale(scale : NSNumber){
  214. if let pdfListView = self.pdfViewController?.pdfListView {
  215. pdfListView.setScaleFactor(CGFloat(truncating: scale), animated: true)
  216. }
  217. }
  218. func getScale(completionHandler: @escaping (NSNumber) -> Void) {
  219. if let pdfListView = self.pdfViewController?.pdfListView {
  220. completionHandler(NSNumber(value: pdfListView.scaleFactor))
  221. } else {
  222. completionHandler(1.0)
  223. }
  224. }
  225. func setReadBackgroundColor(displayMode : NSString) {
  226. if let pdfListView = self.pdfViewController?.pdfListView {
  227. switch displayMode {
  228. case "light":
  229. pdfListView.displayMode = .normal
  230. case "dark":
  231. pdfListView.displayMode = .night
  232. case "sepia":
  233. pdfListView.displayMode = .soft
  234. case "reseda":
  235. pdfListView.displayMode = .green
  236. default:
  237. pdfListView.displayMode = .normal
  238. }
  239. pdfListView.layoutDocumentView()
  240. }
  241. }
  242. func getReadbackgroundColor(completionHandler: @escaping (NSString) -> Void) {
  243. if let pdfListView = self.pdfViewController?.pdfListView {
  244. let dispalyMode = pdfListView.displayMode
  245. switch dispalyMode {
  246. case .normal:
  247. completionHandler("#FFFFFF")
  248. case .night:
  249. completionHandler("#000000")
  250. case .soft:
  251. completionHandler("#FFFFFF")
  252. case .green:
  253. completionHandler("#FFEFBE")
  254. case .custom:
  255. completionHandler("#CDE6D0")
  256. @unknown default:
  257. completionHandler("#FFFFFF")
  258. }
  259. } else {
  260. completionHandler("#FFFFFF")
  261. }
  262. }
  263. func setFormFieldHighlight(formFieldHighlight : Bool){
  264. if let pdfListView = self.pdfViewController?.pdfListView {
  265. CPDFKitConfig.sharedInstance().setEnableFormFieldHighlight(formFieldHighlight)
  266. pdfListView.layoutDocumentView()
  267. }
  268. }
  269. func isFormFieldHighlight(completionHandler: @escaping (Bool) -> Void){
  270. completionHandler(CPDFKitConfig.sharedInstance().enableFormFieldHighlight())
  271. }
  272. func setLinkHighlight(linkHighlight : Bool) {
  273. if let pdfListView = self.pdfViewController?.pdfListView {
  274. CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(linkHighlight)
  275. pdfListView.layoutDocumentView()
  276. }
  277. }
  278. func isLinkHighlight(completionHandler: @escaping (Bool) -> Void){
  279. completionHandler(CPDFKitConfig.sharedInstance().enableLinkFieldHighlight())
  280. }
  281. func setVerticalMode(isVerticalMode : Bool) {
  282. if let pdfListView = self.pdfViewController?.pdfListView {
  283. pdfListView.displayDirection = isVerticalMode ? .vertical : .horizontal
  284. pdfListView.layoutDocumentView()
  285. }
  286. }
  287. func isVerticalMode(completionHandler: @escaping (Bool) -> Void){
  288. if let pdfListView = self.pdfViewController?.pdfListView {
  289. completionHandler(pdfListView.displayDirection == .vertical)
  290. } else {
  291. completionHandler(true)
  292. }
  293. }
  294. func setContinueMode(isContinueMode : Bool) {
  295. if let pdfListView = self.pdfViewController?.pdfListView {
  296. pdfListView.displaysPageBreaks = isContinueMode
  297. pdfListView.layoutDocumentView()
  298. }
  299. }
  300. func isContinueMode(completionHandler: @escaping (Bool) -> Void){
  301. if let pdfListView = self.pdfViewController?.pdfListView {
  302. completionHandler(pdfListView.displaysPageBreaks)
  303. }else {
  304. completionHandler(true)
  305. }
  306. }
  307. func setDoublePageMode(isDoublePageMode : Bool) {
  308. if let pdfListView = self.pdfViewController?.pdfListView {
  309. pdfListView.displayTwoUp = isDoublePageMode
  310. pdfListView.displaysAsBook = false
  311. pdfListView.layoutDocumentView()
  312. }
  313. }
  314. func isDoublePageMode(completionHandler: @escaping (Bool) -> Void){
  315. if let pdfListView = self.pdfViewController?.pdfListView {
  316. completionHandler(pdfListView.displayTwoUp)
  317. }else {
  318. completionHandler(true)
  319. }
  320. }
  321. func setCoverPageMode(isCoverPageMode : Bool) {
  322. if let pdfListView = self.pdfViewController?.pdfListView {
  323. pdfListView.displayTwoUp = isCoverPageMode
  324. pdfListView.displaysAsBook = isCoverPageMode
  325. pdfListView.layoutDocumentView()
  326. }
  327. }
  328. func isCoverPageMode(completionHandler: @escaping (Bool) -> Void){
  329. if let pdfListView = self.pdfViewController?.pdfListView {
  330. completionHandler(pdfListView.displaysAsBook)
  331. }else {
  332. completionHandler(true)
  333. }
  334. }
  335. func setCropMode(isCropMode : Bool) {
  336. if let pdfListView = self.pdfViewController?.pdfListView {
  337. pdfListView.displayCrop = isCropMode
  338. pdfListView.layoutDocumentView()
  339. }
  340. }
  341. func isCropMode(completionHandler: @escaping (Bool) -> Void){
  342. if let pdfListView = self.pdfViewController?.pdfListView {
  343. completionHandler(pdfListView.displayCrop)
  344. }else {
  345. completionHandler(true)
  346. }
  347. }
  348. func setPreviewMode(viewMode : String) {
  349. switch viewMode {
  350. case "viewer":
  351. self.pdfViewController?.enterViewerMode()
  352. case "annotations":
  353. self.pdfViewController?.enterAnnotationMode()
  354. case "contentEditor":
  355. self.pdfViewController?.enterEditMode()
  356. case "forms":
  357. self.pdfViewController?.enterFormMode()
  358. case "signatures":
  359. self.pdfViewController?.enterSignatureMode()
  360. default:
  361. self.pdfViewController?.enterViewerMode()
  362. }
  363. }
  364. func getPreviewMode(completionHandler: @escaping (String) -> Void) {
  365. let state = self.pdfViewController?.functionTypeState ?? .viewer
  366. switch state {
  367. case .viewer:
  368. completionHandler("viewer")
  369. case .edit:
  370. completionHandler("contentEditor")
  371. case .annotation:
  372. completionHandler("annotations")
  373. case .form:
  374. completionHandler("forms")
  375. case .signature:
  376. completionHandler("signatures")
  377. default:
  378. completionHandler("viewer")
  379. }
  380. }
  381. func showThumbnailView(isEditMode : Bool) {
  382. if isEditMode {
  383. self.pdfViewController?.enterPDFPageEdit()
  384. } else {
  385. self.pdfViewController?.enterThumbnail()
  386. }
  387. }
  388. func showBotaView() {
  389. self.pdfViewController?.buttonItemClicked_Bota(UIButton(frame: .zero))
  390. }
  391. func showAddWatermarkView(saveAsNewFile : Bool) {
  392. self.pdfViewController?.enterPDFWatermark(isSaveAs: saveAsNewFile)
  393. }
  394. func showSecurityView() {
  395. self.pdfViewController?.enterPDFSecurity()
  396. }
  397. func showDisplaySettingView() {
  398. self.pdfViewController?.enterPDFSetting()
  399. }
  400. func enterSnipMode() {
  401. self.pdfViewController?.enterPDFSnipImageMode()
  402. }
  403. func exitSnipMode() {
  404. self.pdfViewController?.exitPDFSnipImageMode()
  405. }
  406. func open(document : URL, password : String, completionHandler: @escaping (Bool) -> Void) {
  407. if let pdfListView = self.pdfViewController?.pdfListView {
  408. let newDocument = CPDFDocument(url: document)
  409. if(newDocument?.isLocked == true){
  410. newDocument?.unlock(withPassword: password)
  411. }
  412. pdfListView.document = newDocument
  413. pdfListView.setNeedsDisplay()
  414. completionHandler(true)
  415. } else {
  416. completionHandler(false)
  417. }
  418. }
  419. func getFileName(completionHandler: @escaping (String) -> Void){
  420. if let pdfListView = self.pdfViewController?.pdfListView {
  421. completionHandler(pdfListView.document.documentURL.lastPathComponent)
  422. }else {
  423. completionHandler("")
  424. }
  425. }
  426. func isEncrypted(completionHandler: @escaping (Bool) -> Void){
  427. if let pdfListView = self.pdfViewController?.pdfListView {
  428. completionHandler(pdfListView.document.isEncrypted)
  429. }else {
  430. completionHandler(false)
  431. }
  432. }
  433. func isImageDoc(completionHandler: @escaping (Bool) -> Void){
  434. if let pdfListView = self.pdfViewController?.pdfListView {
  435. completionHandler(pdfListView.document.isImageDocument())
  436. }else {
  437. completionHandler(false)
  438. }
  439. }
  440. func getPermissions(completionHandler: @escaping (NSNumber) -> Void) {
  441. if let pdfListView = self.pdfViewController?.pdfListView {
  442. let permissions = pdfListView.document?.permissionsStatus ?? .none
  443. switch permissions {
  444. case .none:
  445. completionHandler(0)
  446. case .user:
  447. completionHandler(1)
  448. case .owner:
  449. completionHandler(2)
  450. default:
  451. completionHandler(0)
  452. }
  453. }else {
  454. completionHandler(0)
  455. }
  456. }
  457. func getPageCount(completionHandler: @escaping (NSNumber) -> Void) {
  458. if let pdfListView = self.pdfViewController?.pdfListView {
  459. completionHandler(pdfListView.document.pageCount as NSNumber)
  460. }else {
  461. completionHandler(0)
  462. }
  463. }
  464. func checkOwnerUnlocked(completionHandler: @escaping (Bool) -> Void) {
  465. if let pdfListView = self.pdfViewController?.pdfListView {
  466. completionHandler(pdfListView.document.isCheckOwnerUnlocked())
  467. }else {
  468. completionHandler(false)
  469. }
  470. }
  471. func checkOwnerPassword(password : String, completionHandler: @escaping (Bool) -> Void) {
  472. if let pdfListView = self.pdfViewController?.pdfListView {
  473. completionHandler(pdfListView.document.checkOwnerPassword(password))
  474. }else {
  475. completionHandler(false)
  476. }
  477. }
  478. func removePassword(completionHandler: @escaping (Bool) -> Void) {
  479. if let pdfListView = self.pdfViewController?.pdfListView {
  480. let newDocument = pdfListView.document
  481. let url = newDocument?.documentURL
  482. let success = newDocument?.writeDecrypt(to: url, isSaveFontSubset: true) ?? false
  483. completionHandler(success)
  484. } else {
  485. completionHandler(false)
  486. }
  487. }
  488. func setPassword(info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
  489. if let pdfListView = self.pdfViewController?.pdfListView {
  490. let _info = info as? [String: Any]
  491. let userPassword : String = self.getValue(from: _info, key: "user_password", defaultValue: "")
  492. let ownerPassword : String = self.getValue(from: _info, key: "owner_password", defaultValue: "")
  493. let allowsPrinting : Bool = self.getValue(from: _info, key: "allows_printing", defaultValue: true)
  494. let allowsCopying : Bool = self.getValue(from: _info, key: "allows_copying", defaultValue: true)
  495. let encryptAlgo : String = self.getValue(from: _info, key: "encrypt_algo", defaultValue: "rc4")
  496. var level: Int = 0
  497. // Encryption mode, the type passed in is:rc4, aes128, aes256, noEncryptAlgo
  498. switch encryptAlgo {
  499. case "rc4":
  500. level = 0
  501. case "aes128":
  502. level = 1
  503. case "aes256":
  504. level = 2
  505. case "noEncryptAlgo":
  506. level = 3
  507. default:
  508. level = 3
  509. }
  510. var options:[CPDFDocumentWriteOption: Any] = [:]
  511. options[CPDFDocumentWriteOption.userPasswordOption] = userPassword
  512. options[CPDFDocumentWriteOption.ownerPasswordOption] = ownerPassword
  513. options[CPDFDocumentWriteOption.allowsPrintingOption] = allowsPrinting
  514. options[CPDFDocumentWriteOption.allowsCopyingOption] = allowsCopying
  515. options[CPDFDocumentWriteOption.encryptionLevelOption] = NSNumber(value: level)
  516. let newDocument = pdfListView.document
  517. let url = newDocument?.documentURL
  518. let success = newDocument?.write(to: url, withOptions: options, isSaveFontSubset: true) ?? false
  519. completionHandler(success)
  520. } else {
  521. completionHandler(false)
  522. }
  523. }
  524. func getEncryptAlgo(completionHandler: @escaping (String) -> Void) {
  525. if let pdfListView = self.pdfViewController?.pdfListView {
  526. let encryptAlgo = pdfListView.document.encryptionLevel
  527. switch encryptAlgo {
  528. case .RC4:
  529. completionHandler("rc4")
  530. case .AES128:
  531. completionHandler("aes128")
  532. case .AES256:
  533. completionHandler("aes256")
  534. case .noEncryptAlgo:
  535. completionHandler("noEncryptAlgo")
  536. default:
  537. completionHandler("noEncryptAlgo")
  538. }
  539. }else {
  540. completionHandler("noEncryptAlgo")
  541. }
  542. }
  543. func printDocument() {
  544. self.pdfViewController?.enterPrintState();
  545. }
  546. func importWidgets(xfdfFile : URL, completionHandler: @escaping (Bool) -> Void) {
  547. if let pdfListView = self.pdfViewController?.pdfListView {
  548. let documentFolder = NSHomeDirectory().appending("/Documents/Files")
  549. if !FileManager.default.fileExists(atPath: documentFolder) {
  550. try? FileManager.default.createDirectory(at: URL(fileURLWithPath: documentFolder), withIntermediateDirectories: true, attributes: nil)
  551. }
  552. let documentPath = documentFolder + "/\(xfdfFile.lastPathComponent)"
  553. try? FileManager.default.copyItem(atPath: xfdfFile.path, toPath: documentPath)
  554. if !FileManager.default.fileExists(atPath: documentPath) {
  555. print("fail")
  556. }
  557. let success = pdfListView.document?.importForm(fromXFDFPath: documentPath) ?? false
  558. if success {
  559. self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
  560. }
  561. completionHandler(success)
  562. } else {
  563. completionHandler(false)
  564. }
  565. }
  566. func getDocumentPath(completionHandler: @escaping (String) -> Void) {
  567. if let pdfListView = self.pdfViewController?.pdfListView {
  568. let documentPath = pdfListView.document.documentURL.path
  569. completionHandler(documentPath)
  570. }
  571. }
  572. func exportWidgets(completionHandler: @escaping (String) -> Void) {
  573. if let pdfListView = self.pdfViewController?.pdfListView {
  574. let fileNameWithExtension = pdfListView.document?.documentURL.lastPathComponent ?? ""
  575. let fileName = (fileNameWithExtension as NSString).deletingPathExtension
  576. let documentFolder = NSHomeDirectory().appending("/Documents/\(fileName)_xfdf.xfdf")
  577. let succes = pdfListView.document?.exportForm(toXFDFPath: documentFolder) ?? false
  578. if succes {
  579. completionHandler(documentFolder)
  580. } else {
  581. completionHandler("")
  582. }
  583. } else {
  584. completionHandler("")
  585. }
  586. }
  587. func flattenAllPages(savePath: URL, fontSubset: Bool, completionHandler: @escaping (Bool) -> Void) {
  588. if let pdfListView = self.pdfViewController?.pdfListView {
  589. let success = pdfListView.document.writeFlatten(to: savePath, isSaveFontSubset: fontSubset)
  590. completionHandler(success)
  591. } else {
  592. completionHandler(false)
  593. }
  594. }
  595. func saveAs(savePath: URL, removeSecurity: Bool, fontSubset: Bool, completionHandler: @escaping (Bool) -> Void) {
  596. if let pdfListView = self.pdfViewController?.pdfListView {
  597. var success: Bool = false
  598. if removeSecurity {
  599. success = pdfListView.document.writeDecrypt(to: savePath, isSaveFontSubset: fontSubset)
  600. } else {
  601. success = pdfListView.document.write(to: savePath, isSaveFontSubset: fontSubset)
  602. }
  603. completionHandler(success)
  604. } else {
  605. completionHandler(false)
  606. }
  607. }
  608. func importDocument(_ filePath:URL, _ info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
  609. if let pdfListView = self.pdfViewController?.pdfListView {
  610. let _info = info as? [String: Any]
  611. let password: String = self.getValue(from: _info, key: "password", defaultValue: "")
  612. let pages: [Int] = self.getValue(from: _info, key: "pages", defaultValue: [])
  613. let insert_position: Int = self.getValue(from: _info, key: "insert_position", defaultValue: 0)
  614. let _document = CPDFDocument(url: filePath)
  615. if _document?.isLocked == true {
  616. _document?.unlock(withPassword: password)
  617. }
  618. let success = self.insertPDFDocument(_document!, Pages: pages, Position: UInt(insert_position))
  619. completionHandler(success)
  620. } else {
  621. completionHandler(false)
  622. }
  623. }
  624. func splitDocumentPages(savePath: URL, pages: [Int], completionHandler: @escaping (Bool) -> Void) {
  625. if let pdfListView = self.pdfViewController?.pdfListView {
  626. let success = self.extractPDFDocument(savePath, Pages: pages)
  627. completionHandler(success)
  628. } else {
  629. completionHandler(false)
  630. }
  631. }
  632. // MARK: - CPDFViewBaseControllerDelete
  633. func PDFViewBaseController(_ baseController: CPDFViewBaseController, SaveState success: Bool) {
  634. self.delegate?.saveDocumentChange(self)
  635. }
  636. func PDFViewBaseController(_ baseController: CPDFViewBaseController, currentPageIndex index: Int) {
  637. self.delegate?.onPageChanged(self, pageIndex: index)
  638. }
  639. // MARK: - RCT Methods
  640. private var configuration: String = ""
  641. @objc func setConfiguration(_ newSection: String) {
  642. configuration = newSection
  643. if (document.path.count > 1) && (configuration.count > 1) {
  644. createCPDFView()
  645. }
  646. }
  647. private var document: URL = URL(fileURLWithPath: "")
  648. @objc func setDocument(_ newSection: URL) {
  649. document = newSection
  650. if (document.path.count > 1) && (configuration.count > 1) {
  651. createCPDFView()
  652. }
  653. }
  654. private var password: String = ""
  655. @objc func setPassword(_ newSection: String) {
  656. password = newSection
  657. }
  658. public var onChange: RCTBubblingEventBlock?
  659. @objc func setOnChange(_ newSection: @escaping RCTBubblingEventBlock) {
  660. onChange = newSection
  661. }
  662. }