RCTCPDFView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/ReaderViewer")
  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. }
  57. } else {
  58. if self.pdfViewController?.pdfListView?.document.isModified() == true {
  59. let document = self.pdfViewController?.pdfListView?.document
  60. let success = document?.write(to: document?.documentURL ?? URL(fileURLWithPath: ""), isSaveFontSubset: true) ?? false
  61. completionHandler(success)
  62. }
  63. }
  64. }
  65. // MARK: - RCT Methods
  66. private var configuration: String = ""
  67. @objc func setConfiguration(_ newSection: String) {
  68. configuration = newSection
  69. if (document.path.count > 1) && (configuration.count > 1) {
  70. createCPDFView()
  71. }
  72. }
  73. private var document: URL = URL(fileURLWithPath: "")
  74. @objc func setDocument(_ newSection: URL) {
  75. document = newSection
  76. if (document.path.count > 1) && (configuration.count > 1) {
  77. createCPDFView()
  78. }
  79. }
  80. private var password: String = ""
  81. @objc func setPassword(_ newSection: String) {
  82. password = newSection
  83. }
  84. }