|
@@ -17,6 +17,7 @@ import ComPDFKit
|
|
protocol RCTCPDFViewDelegate: AnyObject {
|
|
protocol RCTCPDFViewDelegate: AnyObject {
|
|
func cpdfViewAttached(_ cpdfView: RCTCPDFView)
|
|
func cpdfViewAttached(_ cpdfView: RCTCPDFView)
|
|
func saveDocumentChange(_ cpdfView: RCTCPDFView)
|
|
func saveDocumentChange(_ cpdfView: RCTCPDFView)
|
|
|
|
+ func onPageChanged(_ cpdfView: RCTCPDFView, pageIndex: Int)
|
|
}
|
|
}
|
|
|
|
|
|
class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
|
|
class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
|
|
@@ -38,21 +39,32 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
|
|
// MARK: - Private Methods
|
|
// MARK: - Private Methods
|
|
|
|
|
|
private func createCPDFView() {
|
|
private func createCPDFView() {
|
|
- let fileManager = FileManager.default
|
|
|
|
- let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
|
|
|
|
- let fileName = document.lastPathComponent
|
|
|
|
- let docsFilePath = samplesFilePath + "/" + fileName
|
|
|
|
|
|
+ var documentPath = document.path
|
|
|
|
+ var success = false
|
|
|
|
|
|
- if !fileManager.fileExists(atPath: samplesFilePath) {
|
|
|
|
- try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
|
|
+ let homeDiectory = NSHomeDirectory()
|
|
|
|
+
|
|
|
|
+ if (documentPath.hasPrefix(homeDiectory)) {
|
|
|
|
+ let fileManager = FileManager.default
|
|
|
|
+ let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
|
|
|
|
+ let fileName = document.lastPathComponent
|
|
|
|
+ let docsFilePath = samplesFilePath + "/" + fileName
|
|
|
|
+
|
|
|
|
+ if !fileManager.fileExists(atPath: samplesFilePath) {
|
|
|
|
+ try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
|
|
|
|
+
|
|
|
|
+ documentPath = docsFilePath
|
|
|
|
+ } else {
|
|
|
|
+ success = document.startAccessingSecurityScopedResource()
|
|
}
|
|
}
|
|
|
|
|
|
- try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
|
|
|
|
-
|
|
|
|
let jsonData = CPDFJSONDataParse(String: configuration)
|
|
let jsonData = CPDFJSONDataParse(String: configuration)
|
|
let configurations = jsonData.configuration ?? CPDFConfiguration()
|
|
let configurations = jsonData.configuration ?? CPDFConfiguration()
|
|
|
|
|
|
- pdfViewController = CPDFViewController(filePath: docsFilePath, password: password, configuration: configurations)
|
|
|
|
|
|
+ pdfViewController = CPDFViewController(filePath: documentPath, password: password, configuration: configurations)
|
|
pdfViewController?.delegate = self
|
|
pdfViewController?.delegate = self
|
|
navigationController = CNavigationController(rootViewController: pdfViewController!)
|
|
navigationController = CNavigationController(rootViewController: pdfViewController!)
|
|
navigationController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
navigationController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
@@ -63,6 +75,10 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
|
|
addSubview(navigationController?.view ?? UIView())
|
|
addSubview(navigationController?.view ?? UIView())
|
|
|
|
|
|
self.delegate?.cpdfViewAttached(self)
|
|
self.delegate?.cpdfViewAttached(self)
|
|
|
|
+
|
|
|
|
+ if success {
|
|
|
|
+ document.stopAccessingSecurityScopedResource()
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// MARK: - Public Methods
|
|
// MARK: - Public Methods
|
|
@@ -112,12 +128,26 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- func importAnnotations(xfdfFile : String, completionHandler: @escaping (Bool) -> Void) {
|
|
|
|
|
|
+ func importAnnotations(xfdfFile : URL, completionHandler: @escaping (Bool) -> Void) {
|
|
if let pdfListView = self.pdfViewController?.pdfListView {
|
|
if let pdfListView = self.pdfViewController?.pdfListView {
|
|
- let success = pdfListView.document?.importAnnotation(fromXFDFPath: xfdfFile) ?? false
|
|
|
|
|
|
+
|
|
|
|
+ let documentFolder = NSHomeDirectory().appending("/Documents/Files")
|
|
|
|
+ if !FileManager.default.fileExists(atPath: documentFolder) {
|
|
|
|
+ try? FileManager.default.createDirectory(at: URL(fileURLWithPath: documentFolder), withIntermediateDirectories: true, attributes: nil)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let documentPath = documentFolder + "/\(xfdfFile.lastPathComponent)"
|
|
|
|
+ try? FileManager.default.copyItem(atPath: xfdfFile.path, toPath: documentPath)
|
|
|
|
+
|
|
|
|
+ if !FileManager.default.fileExists(atPath: documentPath) {
|
|
|
|
+ print("fail")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let success = pdfListView.document?.importAnnotation(fromXFDFPath: documentPath) ?? false
|
|
if success {
|
|
if success {
|
|
self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
|
|
self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
|
|
}
|
|
}
|
|
|
|
+ completionHandler(success)
|
|
} else {
|
|
} else {
|
|
completionHandler(false)
|
|
completionHandler(false)
|
|
}
|
|
}
|
|
@@ -169,6 +199,10 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
|
|
self.delegate?.saveDocumentChange(self)
|
|
self.delegate?.saveDocumentChange(self)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ func PDFViewBaseController(_ baseController: CPDFViewBaseController, currentPageIndex index: Int) {
|
|
|
|
+ self.delegate?.onPageChanged(self, pageIndex: index)
|
|
|
|
+ }
|
|
|
|
+
|
|
// MARK: - RCT Methods
|
|
// MARK: - RCT Methods
|
|
|
|
|
|
private var configuration: String = ""
|
|
private var configuration: String = ""
|