RCTCPDFView.swift 30 KB

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