RCTCPDFViewManager.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // RCTCPDFViewManager.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
  14. @objc(RCTCPDFReaderView)
  15. class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
  16. @objc override static func requiresMainQueueSetup() -> Bool {
  17. return true
  18. }
  19. var cpdfViews: Dictionary<Int, RCTCPDFView> = [:]
  20. @objc override func view() -> UIView! {
  21. let rtcCPDFView = RCTCPDFView()
  22. rtcCPDFView.delegate = self
  23. return rtcCPDFView
  24. }
  25. func saveDocument(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
  26. let rtcCPDFView = cpdfViews[tag]
  27. rtcCPDFView?.saveDocument(completionHandler: { success in
  28. completionHandler(success)
  29. if (success) {
  30. if let onChange = rtcCPDFView?.onChange {
  31. onChange(["saveDocument": "saveDocument"])
  32. }
  33. }
  34. })
  35. }
  36. func setMargins(forCPDFViewTag tag: Int, left : Int, top : Int, right : Int, bottom : Int) {
  37. let rtcCPDFView = cpdfViews[tag]
  38. rtcCPDFView?.setMargins(left: left, top: top, right: right, bottom: bottom)
  39. }
  40. func removeAllAnnotations(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
  41. let rtcCPDFView = cpdfViews[tag]
  42. rtcCPDFView?.removeAllAnnotations(completionHandler: { success in
  43. completionHandler(success)
  44. })
  45. }
  46. func importAnnotations(forCPDFViewTag tag: Int, xfdfFile : URL, completionHandler: @escaping (Bool) -> Void) {
  47. let rtcCPDFView = cpdfViews[tag]
  48. rtcCPDFView?.importAnnotations(xfdfFile: xfdfFile, completionHandler: { success in
  49. completionHandler(success)
  50. })
  51. }
  52. func exportAnnotations(forCPDFViewTag tag: Int, completionHandler: @escaping (String) -> Void) {
  53. let rtcCPDFView = cpdfViews[tag]
  54. rtcCPDFView?.exportAnnotations(completionHandler: { success in
  55. completionHandler(success)
  56. })
  57. }
  58. func setDisplayPageIndex(forCPDFViewTag tag : Int, pageIndex : Int) {
  59. let rtcCPDFView = cpdfViews[tag]
  60. rtcCPDFView?.setDisplayPageIndex(pageIndex: pageIndex)
  61. }
  62. func getCurrentPageIndex(forCPDFViewTag tag: Int, completionHandler: @escaping (Int) -> Void) {
  63. let rtcCPDFView = cpdfViews[tag]
  64. rtcCPDFView?.getCurrentPageIndex(completionHandler: { pageIndex in
  65. completionHandler(pageIndex)
  66. })
  67. }
  68. func hasChange(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
  69. let rtcCPDFView = cpdfViews[tag]
  70. rtcCPDFView?.hasChange(completionHandler: { success in
  71. completionHandler(success)
  72. })
  73. }
  74. // MARK: - RCTCPDFViewDelegate
  75. func cpdfViewAttached(_ cpdfView: RCTCPDFView) {
  76. cpdfViews[cpdfView.reactTag.intValue] = cpdfView
  77. }
  78. func saveDocumentChange(_ cpdfView: RCTCPDFView) {
  79. if let onChange = cpdfView.onChange {
  80. onChange(["saveDocument": "saveDocument"])
  81. }
  82. }
  83. func onPageChanged(_ cpdfView: RCTCPDFView, pageIndex: Int) {
  84. if let onChange = cpdfView.onChange {
  85. onChange(["onPageChanged": pageIndex])
  86. }
  87. }
  88. }