RCTCPDFView.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. class RCTCPDFView: UIView {
  15. private var pdfViewController : CPDFViewController?
  16. private var navigationController : CNavigationController?
  17. init() {
  18. super.init(frame: CGRect(x: 0, y: 0, width: 500, height: 400))
  19. }
  20. required init?(coder: NSCoder) {
  21. fatalError("init(coder:) has not been implemented")
  22. }
  23. private func createCPDFView() {
  24. let jsonData = CPDFJSONDataParse(String: configuration)
  25. let configurations = jsonData.configuration ?? CPDFConfiguration()
  26. pdfViewController = CPDFViewController(filePath: document.path, password: password, configuration: configurations)
  27. navigationController = CNavigationController(rootViewController: pdfViewController!)
  28. navigationController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  29. navigationController?.view.frame = self.frame
  30. navigationController?.setViewControllers([pdfViewController!], animated: true)
  31. addSubview(navigationController?.view ?? UIView())
  32. }
  33. private var configuration: String = ""
  34. @objc func setConfiguration(_ newSection: String) {
  35. configuration = newSection
  36. if (document.path.count > 1) && (configuration.count > 1) {
  37. createCPDFView()
  38. }
  39. }
  40. private var document: URL = URL(fileURLWithPath: "")
  41. @objc func setDocument(_ newSection: URL) {
  42. document = newSection
  43. if (document.path.count > 1) && (configuration.count > 1) {
  44. createCPDFView()
  45. }
  46. }
  47. private var password: String = ""
  48. @objc func setPassword(_ newSection: String) {
  49. password = newSection
  50. }
  51. }