RCTCPDFView.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // RCTCPDFView.swift
  3. // react-native-compdfkit-pdf
  4. //
  5. // Copyright © 2014-2024 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. }
  18. class RCTCPDFView: UIView {
  19. weak var delegate: RCTCPDFViewDelegate?
  20. public var pdfViewController : CPDFViewController?
  21. private var navigationController : CNavigationController?
  22. init() {
  23. super.init(frame: CGRect(x: 0, y: 0, width: 500, height: 400))
  24. }
  25. required init?(coder: NSCoder) {
  26. fatalError("init(coder:) has not been implemented")
  27. }
  28. // MARK: - Private Methods
  29. private func createCPDFView() {
  30. let fileManager = FileManager.default
  31. let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
  32. let fileName = document.lastPathComponent
  33. let docsFilePath = samplesFilePath + "/" + fileName
  34. if !fileManager.fileExists(atPath: samplesFilePath) {
  35. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  36. }
  37. try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
  38. let jsonData = CPDFJSONDataParse(String: configuration)
  39. let configurations = jsonData.configuration ?? CPDFConfiguration()
  40. pdfViewController = CPDFViewController(filePath: docsFilePath, password: password, configuration: configurations)
  41. navigationController = CNavigationController(rootViewController: pdfViewController!)
  42. navigationController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  43. navigationController?.view.frame = self.frame
  44. navigationController?.setViewControllers([pdfViewController!], animated: true)
  45. addSubview(navigationController?.view ?? UIView())
  46. self.delegate?.cpdfViewAttached(self)
  47. }
  48. // MARK: - Public Methods
  49. func saveDocument(completionHandler: @escaping (Bool) -> Void) {
  50. if (self.pdfViewController?.pdfListView?.isEditing() == true && self.pdfViewController?.pdfListView?.isEdited() == true) {
  51. self.pdfViewController?.pdfListView?.commitEditing()
  52. if self.pdfViewController?.pdfListView?.document.isModified() == true {
  53. let document = self.pdfViewController?.pdfListView?.document
  54. let success = document?.write(to: document?.documentURL ?? URL(fileURLWithPath: ""), isSaveFontSubset: true) ?? false
  55. completionHandler(success)
  56. } else {
  57. completionHandler(true)
  58. }
  59. } else {
  60. if self.pdfViewController?.pdfListView?.document.isModified() == true {
  61. let document = self.pdfViewController?.pdfListView?.document
  62. let success = document?.write(to: document?.documentURL ?? URL(fileURLWithPath: ""), isSaveFontSubset: true) ?? false
  63. completionHandler(success)
  64. } else {
  65. completionHandler(true)
  66. }
  67. }
  68. }
  69. // MARK: - RCT Methods
  70. private var configuration: String = ""
  71. @objc func setConfiguration(_ newSection: String) {
  72. configuration = newSection
  73. if (document.path.count > 1) && (configuration.count > 1) {
  74. createCPDFView()
  75. }
  76. }
  77. private var document: URL = URL(fileURLWithPath: "")
  78. @objc func setDocument(_ newSection: URL) {
  79. document = newSection
  80. if (document.path.count > 1) && (configuration.count > 1) {
  81. createCPDFView()
  82. }
  83. }
  84. private var password: String = ""
  85. @objc func setPassword(_ newSection: String) {
  86. password = newSection
  87. }
  88. }