RCTCPDFView.swift 29 KB

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