RCTCPDFView.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. func saveDocumentChange(_ cpdfView: RCTCPDFView)
  18. func onPageChanged(_ cpdfView: RCTCPDFView, pageIndex: Int)
  19. }
  20. class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
  21. weak var delegate: RCTCPDFViewDelegate?
  22. public var pdfViewController : CPDFViewController?
  23. private var navigationController : CNavigationController?
  24. init() {
  25. super.init(frame: CGRect(x: 0, y: 0, width: 500, height: 400))
  26. }
  27. required init?(coder: NSCoder) {
  28. fatalError("init(coder:) has not been implemented")
  29. }
  30. // MARK: - Private Methods
  31. private func createCPDFView() {
  32. var documentPath = document.path
  33. var success = false
  34. let homeDiectory = NSHomeDirectory()
  35. let bundlePath = Bundle.main.bundlePath
  36. if (documentPath.hasPrefix(homeDiectory) || documentPath.hasPrefix(bundlePath)) {
  37. let fileManager = FileManager.default
  38. let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
  39. let fileName = document.lastPathComponent
  40. let docsFilePath = samplesFilePath + "/" + fileName
  41. if !fileManager.fileExists(atPath: samplesFilePath) {
  42. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  43. }
  44. try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
  45. documentPath = docsFilePath
  46. } else {
  47. success = document.startAccessingSecurityScopedResource()
  48. }
  49. let jsonData = CPDFJSONDataParse(String: configuration)
  50. let configurations = jsonData.configuration ?? CPDFConfiguration()
  51. pdfViewController = CPDFViewController(filePath: documentPath, password: password, configuration: configurations)
  52. pdfViewController?.delegate = self
  53. navigationController = CNavigationController(rootViewController: pdfViewController!)
  54. navigationController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  55. navigationController?.view.frame = self.frame
  56. navigationController?.setViewControllers([pdfViewController!], animated: true)
  57. addSubview(navigationController?.view ?? UIView())
  58. self.delegate?.cpdfViewAttached(self)
  59. if success {
  60. document.stopAccessingSecurityScopedResource()
  61. }
  62. }
  63. // MARK: - Public Methods
  64. func saveDocument(completionHandler: @escaping (Bool) -> Void) {
  65. if (self.pdfViewController?.pdfListView?.isEditing() == true && self.pdfViewController?.pdfListView?.isEdited() == true) {
  66. self.pdfViewController?.pdfListView?.commitEditing()
  67. if self.pdfViewController?.pdfListView?.document.isModified() == true {
  68. let document = self.pdfViewController?.pdfListView?.document
  69. let success = document?.write(to: document?.documentURL ?? URL(fileURLWithPath: ""), isSaveFontSubset: true) ?? false
  70. completionHandler(success)
  71. } else {
  72. completionHandler(true)
  73. }
  74. } else {
  75. if self.pdfViewController?.pdfListView?.document.isModified() == true {
  76. let document = self.pdfViewController?.pdfListView?.document
  77. let success = document?.write(to: document?.documentURL ?? URL(fileURLWithPath: ""), isSaveFontSubset: true) ?? false
  78. completionHandler(success)
  79. } else {
  80. completionHandler(true)
  81. }
  82. }
  83. }
  84. func setMargins(left : Int, top : Int, right : Int, bottom : Int) {
  85. if let pdfListView = self.pdfViewController?.pdfListView {
  86. pdfListView.pageBreakMargins = .init(top: CGFloat(top), left: CGFloat(left), bottom: CGFloat(bottom), right: CGFloat(right))
  87. pdfListView.layoutDocumentView()
  88. }
  89. }
  90. func removeAllAnnotations(completionHandler: @escaping (Bool) -> Void) {
  91. if let pdfListView = self.pdfViewController?.pdfListView {
  92. let pageCount = pdfListView.document?.pageCount ?? 0
  93. for i in 0..<pageCount {
  94. let page = pdfListView.document?.page(at: i)
  95. page?.removeAllAnnotations()
  96. }
  97. self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
  98. self.pdfViewController?.pdfListView?.updateActiveAnnotations([])
  99. completionHandler(true)
  100. } else {
  101. completionHandler(false)
  102. }
  103. }
  104. func importAnnotations(xfdfFile : URL, completionHandler: @escaping (Bool) -> Void) {
  105. if let pdfListView = self.pdfViewController?.pdfListView {
  106. let documentFolder = NSHomeDirectory().appending("/Documents/Files")
  107. if !FileManager.default.fileExists(atPath: documentFolder) {
  108. try? FileManager.default.createDirectory(at: URL(fileURLWithPath: documentFolder), withIntermediateDirectories: true, attributes: nil)
  109. }
  110. let documentPath = documentFolder + "/\(xfdfFile.lastPathComponent)"
  111. try? FileManager.default.copyItem(atPath: xfdfFile.path, toPath: documentPath)
  112. if !FileManager.default.fileExists(atPath: documentPath) {
  113. print("fail")
  114. }
  115. let success = pdfListView.document?.importAnnotation(fromXFDFPath: documentPath) ?? false
  116. if success {
  117. self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
  118. }
  119. completionHandler(success)
  120. } else {
  121. completionHandler(false)
  122. }
  123. }
  124. func exportAnnotations(completionHandler: @escaping (String) -> Void) {
  125. if let pdfListView = self.pdfViewController?.pdfListView {
  126. let fileNameWithExtension = pdfListView.document?.documentURL.lastPathComponent ?? ""
  127. let fileName = (fileNameWithExtension as NSString).deletingPathExtension
  128. let documentFolder = NSHomeDirectory().appending("/Documents/\(fileName)_xfdf.xfdf")
  129. let succes = pdfListView.document?.exportAnnotation(toXFDFPath: documentFolder) ?? false
  130. if succes {
  131. completionHandler(documentFolder)
  132. } else {
  133. completionHandler("")
  134. }
  135. } else {
  136. completionHandler("")
  137. }
  138. }
  139. func setDisplayPageIndex(pageIndex : Int) {
  140. if let pdfListView = self.pdfViewController?.pdfListView {
  141. pdfListView.go(toPageIndex: pageIndex, animated: false)
  142. }
  143. }
  144. func getCurrentPageIndex(completionHandler: @escaping (Int) -> Void) {
  145. if let pdfListView = self.pdfViewController?.pdfListView {
  146. completionHandler(pdfListView.currentPageIndex)
  147. } else {
  148. completionHandler(0)
  149. }
  150. }
  151. func hasChange(completionHandler: @escaping (Bool) -> Void) {
  152. if let pdfListView = self.pdfViewController?.pdfListView {
  153. let success = pdfListView.document?.isModified() ?? false
  154. completionHandler(success)
  155. } else {
  156. completionHandler(false)
  157. }
  158. }
  159. // MARK: - CPDFViewBaseControllerDelete
  160. func PDFViewBaseController(_ baseController: CPDFViewBaseController, SaveState success: Bool) {
  161. self.delegate?.saveDocumentChange(self)
  162. }
  163. func PDFViewBaseController(_ baseController: CPDFViewBaseController, currentPageIndex index: Int) {
  164. self.delegate?.onPageChanged(self, pageIndex: index)
  165. }
  166. // MARK: - RCT Methods
  167. private var configuration: String = ""
  168. @objc func setConfiguration(_ newSection: String) {
  169. configuration = newSection
  170. if (document.path.count > 1) && (configuration.count > 1) {
  171. createCPDFView()
  172. }
  173. }
  174. private var document: URL = URL(fileURLWithPath: "")
  175. @objc func setDocument(_ newSection: URL) {
  176. document = newSection
  177. if (document.path.count > 1) && (configuration.count > 1) {
  178. createCPDFView()
  179. }
  180. }
  181. private var password: String = ""
  182. @objc func setPassword(_ newSection: String) {
  183. password = newSection
  184. }
  185. public var onChange: RCTBubblingEventBlock?
  186. @objc func setOnChange(_ newSection: @escaping RCTBubblingEventBlock) {
  187. onChange = newSection
  188. }
  189. }