RCTCPDFView.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. let document = self.pdfViewController?.pdfListView?.document
  51. let success = document?.write(to: document?.documentURL ?? URL(fileURLWithPath: ""), isSaveFontSubset: true) ?? false
  52. completionHandler(success)
  53. }
  54. // MARK: - RCT Methods
  55. private var configuration: String = ""
  56. @objc func setConfiguration(_ newSection: String) {
  57. configuration = newSection
  58. if (document.path.count > 1) && (configuration.count > 1) {
  59. createCPDFView()
  60. }
  61. }
  62. private var document: URL = URL(fileURLWithPath: "")
  63. @objc func setDocument(_ newSection: URL) {
  64. document = newSection
  65. if (document.path.count > 1) && (configuration.count > 1) {
  66. createCPDFView()
  67. }
  68. }
  69. private var password: String = ""
  70. @objc func setPassword(_ newSection: String) {
  71. password = newSection
  72. }
  73. }