Browse Source

comdfpdfkit(rn) - pickfile 事件和测试问题处理

yangliuhua 2 months ago
parent
commit
0f8ae1b574

+ 4 - 0
example/ios/CompdfkitPdfExample.xcodeproj/project.pbxproj

@@ -14,6 +14,7 @@
 		13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
 		7699B88040F8A987B510C191 /* libPods-CompdfkitPdfExample-CompdfkitPdfExampleTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 19F6CBCC0A4E27FBF8BF4A61 /* libPods-CompdfkitPdfExample-CompdfkitPdfExampleTests.a */; };
 		81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; };
+		C9A15F3B2D0BDD210047A6FB /* test.xfdf in Resources */ = {isa = PBXBuildFile; fileRef = C9A15F3A2D0BDD200047A6FB /* test.xfdf */; };
 		C9FCF5E32C475A9100CEFDBB /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = C9FCF5E12C475A9100CEFDBB /* Localizable.strings */; };
 		ECF47E9D2BDF6D3300E7456A /* PDF_Document.pdf in Resources */ = {isa = PBXBuildFile; fileRef = ECF47E9C2BDF6D3300E7456A /* PDF_Document.pdf */; };
 /* End PBXBuildFile section */
@@ -46,6 +47,7 @@
 		5DCACB8F33CDC322A6C60F78 /* libPods-CompdfkitPdfExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-CompdfkitPdfExample.a"; sourceTree = BUILT_PRODUCTS_DIR; };
 		81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = CompdfkitPdfExample/LaunchScreen.storyboard; sourceTree = "<group>"; };
 		89C6BE57DB24E9ADA2F236DE /* Pods-CompdfkitPdfExample-CompdfkitPdfExampleTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CompdfkitPdfExample-CompdfkitPdfExampleTests.release.xcconfig"; path = "Target Support Files/Pods-CompdfkitPdfExample-CompdfkitPdfExampleTests/Pods-CompdfkitPdfExample-CompdfkitPdfExampleTests.release.xcconfig"; sourceTree = "<group>"; };
+		C9A15F3A2D0BDD200047A6FB /* test.xfdf */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = test.xfdf; sourceTree = "<group>"; };
 		C9FCF5E22C475A9100CEFDBB /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
 		C9FCF5E42C475AB600CEFDBB /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
 		ECF47E9C2BDF6D3300E7456A /* PDF_Document.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = PDF_Document.pdf; sourceTree = "<group>"; };
@@ -124,6 +126,7 @@
 		83CBB9F61A601CBA00E9B192 = {
 			isa = PBXGroup;
 			children = (
+				C9A15F3A2D0BDD200047A6FB /* test.xfdf */,
 				ECF47E9C2BDF6D3300E7456A /* PDF_Document.pdf */,
 				13B07FAE1A68108700A75B9A /* CompdfkitPdfExample */,
 				832341AE1AAA6A7D00B99B32 /* Libraries */,
@@ -253,6 +256,7 @@
 			files = (
 				81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */,
 				C9FCF5E32C475A9100CEFDBB /* Localizable.strings in Resources */,
+				C9A15F3B2D0BDD210047A6FB /* test.xfdf in Resources */,
 				13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
 				ECF47E9D2BDF6D3300E7456A /* PDF_Document.pdf in Resources */,
 			);

+ 27 - 26
example/src/CPDFReaderViewConctrolExample.tsx

@@ -14,6 +14,7 @@ import { useNavigation, useRoute, RouteProp } from '@react-navigation/native';
 import { HeaderBackButton } from '@react-navigation/elements';
 import { MenuProvider, Menu, MenuTrigger, MenuOptions, MenuOption } from 'react-native-popup-menu';
 import { SafeAreaView } from 'react-native-safe-area-context';
+import DocumentPicker from 'react-native-document-picker';
 
 type RootStackParamList = {
     CPDFReaderViewExample: { document?: string };
@@ -99,38 +100,38 @@ const CPDFReaderViewConctrolExampleScreen = () => {
 
                     // Android
                     // import xfdf file from android assets directory
-                        const testXfdf = Platform.OS === 'android'
-                            ? 'file:///android_asset/test.xfdf'
-                            : 'test.xfdf'
+                        // const testXfdf = Platform.OS === 'android'
+                        //     ? 'file:///android_asset/test.xfdf'
+                        //     : 'test.xfdf'
                     // import xfdf file from file path
                         // const testXfdf = '/data/user/0/com.compdfkit.reactnative.example/xxx/xxx.xfdf';
 
-                        const importResult = await pdfReaderRef.current?.importAnnotations(testXfdf);
-                        console.log('ComPDFKitRN importAnnotations:', importResult);
+                        // const importResult = await pdfReaderRef.current?.importAnnotations(testXfdf);
+                        // console.log('ComPDFKitRN importAnnotations:', importResult);
 
 
                     // Select an xfdf file from the public directory and import it into the current document
-                        // const pickerResult = DocumentPicker.pick({
-                        //     type: [DocumentPicker.types.allFiles],
-                        //     copyTo: 'cachesDirectory'
-                        // });
-                        // pickerResult.then(async (res) => {
-                        //     const file = res[0];
-                        //     const fileName = file?.name;
-                        //     if(!fileName?.endsWith('xfdf')){
-                        //         console.log('ComPDFKitRN please select xfdf format file');
-                        //         return;
-                        //     }
-
-                        //     console.log('fileUri:' , file?.uri);
-                        //     console.log('fileCopyUri:' , file?.fileCopyUri);
-                        //     console.log('fileType:' , file?.type);
-                        //     const path = file!!.fileCopyUri!!
-
-                        //     const importResult = await pdfReaderRef.current?.importAnnotations(path);
-                        //     console.log('ComPDFKitRN importAnnotations:', importResult);
-
-                        // })
+                        const pickerResult = DocumentPicker.pick({
+                            type: [DocumentPicker.types.allFiles],
+                            copyTo: 'cachesDirectory'
+                        });
+                        pickerResult.then(async (res) => {
+                            const file = res[0];
+                            const fileName = file?.name;
+                            // if(!fileName?.endsWith('xfdf')){
+                            //     console.log('ComPDFKitRN please select xfdf format file');
+                            //     return;
+                            // }
+
+                            console.log('fileUri:' , file?.uri);
+                            console.log('fileCopyUri:' , file?.fileCopyUri);
+                            console.log('fileType:' , file?.type);
+                            const path = file!!.fileCopyUri!!
+
+                            const importResult = await pdfReaderRef.current?.importAnnotations(path);
+                            console.log('ComPDFKitRN importAnnotations:', importResult);
+
+                        })
                 } catch (err) {
                 }
                 break;

+ 6 - 0
ios/ComPDFKitRN.mm

@@ -30,6 +30,12 @@ RCT_EXTERN_METHOD(openDocument:(NSURL *)document
                   password:(NSString *)password
                   configurationJson:(NSString *)configurationJson)
 
+RCT_EXTERN_METHOD(removeSignFileList:(RCTPromiseResolveBlock)resolve
+                  withRejecter:(RCTPromiseRejectBlock)reject)
+
+RCT_EXTERN_METHOD(pickFile:(RCTPromiseResolveBlock)resolve
+                  withRejecter:(RCTPromiseRejectBlock)reject)
+
 + (BOOL)requiresMainQueueSetup
 {
   return NO;

+ 58 - 16
ios/ComPDFKitRN.swift

@@ -16,9 +16,11 @@ import ComPDFKit_Tools
  *
  */
 @objc(ComPDFKit)
-class ComPDFKit: NSObject, CPDFViewBaseControllerDelete{
+class ComPDFKit: NSObject, CPDFViewBaseControllerDelete, UIDocumentPickerDelegate{
     
     private var pdfViewController: CPDFViewController?
+    
+    private var _resolve: RCTPromiseResolveBlock?
 
     /**
       * Get the version number of the ComPDFKit SDK.<br/>
@@ -132,30 +134,66 @@ class ComPDFKit: NSObject, CPDFViewBaseControllerDelete{
     @objc(openDocument: password: configurationJson:)
     func openDocument(document : URL, password: String, configurationJson : String) -> Void {
         DispatchQueue.main.async {
-            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)
-            }
+            var documentPath = document.path
+            var success = false
+            
+            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
 
-            try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
+                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()
+            }
+            
             let rootNav = ComPDFKit.presentedViewController()
 
             let jsonDataParse = CPDFJSONDataParse(String: configurationJson)
             guard let configuration = jsonDataParse.configuration else { return }
 
-            self.pdfViewController = CPDFViewController(filePath: docsFilePath, password: password, configuration: configuration)
+            self.pdfViewController = CPDFViewController(filePath: documentPath, password: password, configuration: configuration)
             self.pdfViewController?.delegate = self
             let nav = CNavigationController(rootViewController: self.pdfViewController!)
             nav.modalPresentationStyle = .fullScreen
             rootNav?.present(nav, animated: true)
+            
+            if success {
+                document.stopAccessingSecurityScopedResource()
+            }
+        }
+    }
+    
+    
+    @objc(removeSignFileList:withRejecter:)
+    func removeSignFileList(resolve: @escaping RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
+        DispatchQueue.main.async {
+            self.pdfViewController?.removeAllElectronicSignatures()
+            resolve(true)
+        }
+    }
+    
+    @objc(pickFile:withRejecter:)
+    func pickFile(resolve: @escaping RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
+        DispatchQueue.main.async {
+            let documentTypes = ["com.adobe.pdf"]
+            let documentPickerViewController = UIDocumentPickerViewController(documentTypes: documentTypes, in: .open)
+            documentPickerViewController.delegate = self
+            UIApplication.presentedViewController()?.present(documentPickerViewController, animated: true, completion: nil)
+            self._resolve = resolve
         }
     }
 
+    //MARK: - ViewController Method
 
     /**
      * CPDFViewBaseControllerDelete delegate to dismiss ViewController.<br/>
@@ -209,11 +247,15 @@ class ComPDFKit: NSObject, CPDFViewBaseControllerDelete{
 
       return currentViewController
     }
-
-    @objc(removeSignFileList:withRejecter:)
-    func removeSignFileList(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
-        self.pdfViewController?.removeAllElectronicSignatures()
-        resolve(true)
+    
+    //MARK: - UIDocumentPickerDelegate
+    
+    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
+        let fileUrlAuthozied = urls.first?.startAccessingSecurityScopedResource() ?? false
+        if fileUrlAuthozied {
+            let filePath = urls.first?.path ?? ""
+            self._resolve?(filePath)
+        }
     }
 
 }

+ 45 - 11
ios/RCTCPDFView.swift

@@ -17,6 +17,7 @@ import ComPDFKit
 protocol RCTCPDFViewDelegate: AnyObject {
     func cpdfViewAttached(_ cpdfView: RCTCPDFView)
     func saveDocumentChange(_ cpdfView: RCTCPDFView)
+    func onPageChanged(_ cpdfView: RCTCPDFView, pageIndex: Int)
 }
 
 class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
@@ -38,21 +39,32 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
     // MARK: - Private Methods
     
     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 configurations = jsonData.configuration ?? CPDFConfiguration()
 
-        pdfViewController = CPDFViewController(filePath: docsFilePath, password: password, configuration: configurations)
+        pdfViewController = CPDFViewController(filePath: documentPath, password: password, configuration: configurations)
         pdfViewController?.delegate = self
         navigationController = CNavigationController(rootViewController: pdfViewController!)
         navigationController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
@@ -63,6 +75,10 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
         addSubview(navigationController?.view ?? UIView())
         
         self.delegate?.cpdfViewAttached(self)
+        
+        if success {
+            document.stopAccessingSecurityScopedResource()
+        }
     }
     
     // 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 {
-            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 {
                 self.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
             }
+            completionHandler(success)
         } else {
             completionHandler(false)
         }
@@ -169,6 +199,10 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
         self.delegate?.saveDocumentChange(self)
     }
     
+    func PDFViewBaseController(_ baseController: CPDFViewBaseController, currentPageIndex index: Int) {
+        self.delegate?.onPageChanged(self, pageIndex: index)
+    }
+    
     // MARK: - RCT Methods
     
     private var configuration: String = ""

+ 7 - 17
ios/RCTCPDFViewManager.swift

@@ -33,22 +33,6 @@ class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
         rtcCPDFView?.saveDocument(completionHandler: { success in
             completionHandler(success)
         })
-        
-        // TODO: 监听滚动的页码变动,返回当前页码
-        // WritableMap params = Arguments.createMap();
-        // params.putInt("onPageChanged", pageIndex);
-        // themedReactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
-        // getId(),
-        // "topChange",
-        // event);
-        
-        // TODO: 监听文档保存,回调保存的通知
-        // WritableMap event = Arguments.createMap();
-        // event.putString("saveDocument", "saveDocument");
-        // themedReactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
-        // getId(),
-        // "topChange",
-        // event);
     }
     
     func setMargins(forCPDFViewTag tag: Int, left : Int, top : Int, right : Int, bottom : Int) {
@@ -63,7 +47,7 @@ class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
         })
     }
     
-    func importAnnotations(forCPDFViewTag tag: Int, xfdfFile : String, completionHandler: @escaping (Bool) -> Void) {
+    func importAnnotations(forCPDFViewTag tag: Int, xfdfFile : URL, completionHandler: @escaping (Bool) -> Void) {
         let rtcCPDFView = cpdfViews[tag]
         rtcCPDFView?.importAnnotations(xfdfFile: xfdfFile, completionHandler: { success in
             completionHandler(success)
@@ -110,4 +94,10 @@ class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
         }
     }
     
+    func onPageChanged(_ cpdfView: RCTCPDFView, pageIndex: Int) {
+        if let onChange = cpdfView.onChange {
+            onChange(["onPageChanged": pageIndex])
+        }
+    }
+    
 }

+ 2 - 5
ios/RCTDocumentManager.m

@@ -15,17 +15,14 @@ RCT_EXTERN_METHOD(save:(NSInteger)tag
                   withRejecter:(RCTPromiseRejectBlock)reject)
 
 RCT_EXTERN_METHOD(setMargins:(NSInteger)tag
-                  withLeft:(NSInteger)left
-                  withTop:(NSInteger)top
-                  withRight:(NSInteger)right
-                  withBottom:(NSInteger)bottom)
+                  withEdges:[Int]edges)
 
 RCT_EXTERN_METHOD(removeAllAnnotations:(NSInteger)tag
                   withResolver:(RCTPromiseResolveBlock)resolve
                   withRejecter:(RCTPromiseRejectBlock)reject)
 
 RCT_EXTERN_METHOD(importAnnotations:(NSInteger)tag
-                  withXfdfFile:(NSString *)xfdfFile
+                  withXfdfFile:(NSURL *)xfdfFile
                   withResolver:(RCTPromiseResolveBlock)resolve
                   withRejecter:(RCTPromiseRejectBlock)reject)
 

+ 44 - 28
ios/RCTDocumentManager.swift

@@ -40,63 +40,79 @@ class RCTDocumentManager: NSObject, RCTBridgeModule {
         }
     }
     
-    @objc(setMargins: withLeft: withTop: withRight: withBottom:)
-    func setMargins(tag : Int, left : Int, top : Int, right : Int, bottom : Int) -> Void {
-        let reader = self.readerView()
-        reader.setMargins(forCPDFViewTag: tag, left: left, top: top, right: right, bottom: bottom)
+    @objc(setMargins: withEdges:)
+    func setMargins(tag : Int, edges: [Int]) -> Void {
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            if edges.count == 4 {
+                reader.setMargins(forCPDFViewTag: tag, left: edges[0], top: edges[1], right: edges[2], bottom: edges[3])
+            }
+        }
     }
     
     @objc(removeAllAnnotations: withResolver: withRejecter:)
     func removeAllAnnotations(tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
-        let reader = self.readerView()
-        reader.removeAllAnnotations(forCPDFViewTag: tag) { success in
-            if success {
-                resolve(success)
-            } else {
-                reject("remove_all_annotation_failed", "Failed to remove all annotation", nil);
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.removeAllAnnotations(forCPDFViewTag: tag) { success in
+                if success {
+                    resolve(success)
+                } else {
+                    reject("remove_all_annotation_failed", "Failed to remove all annotation", nil);
+                }
             }
         }
     }
     
     @objc(importAnnotations: withXfdfFile: withResolver: withRejecter:)
-    func importAnnotations(tag : Int, xfdfFile : String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
-        let reader = self.readerView()
-        reader.importAnnotations(forCPDFViewTag: tag, xfdfFile: xfdfFile) { success in
-            if success {
-                resolve(success)
-            } else {
-                reject("import_annotation_failed", "Failed to import annotation", nil);
+    func importAnnotations(tag : Int, xfdfFile : URL, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.importAnnotations(forCPDFViewTag: tag, xfdfFile: xfdfFile) { success in
+                if success {
+                    resolve(success)
+                } else {
+                    reject("import_annotation_failed", "Failed to import annotation", nil);
+                }
             }
         }
     }
     
     @objc(exportAnnotations: withResolver: withRejecter:)
     func exportAnnotations(tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
-        let reader = self.readerView()
-        reader.exportAnnotations(forCPDFViewTag: tag) { xfdfFilePath in
-            resolve(xfdfFilePath)
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.exportAnnotations(forCPDFViewTag: tag) { xfdfFilePath in
+                resolve(xfdfFilePath)
+            } 
         }
     }
     
     @objc(setDisplayPageIndex: withPageIndex:)
     func setDisplayPageIndex(tag : Int, pageIndex : Int) -> Void {
-        let reader = self.readerView()
-        reader.setDisplayPageIndex(forCPDFViewTag: tag, pageIndex: pageIndex)
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.setDisplayPageIndex(forCPDFViewTag: tag, pageIndex: pageIndex)
+        }
     }
     
     @objc(getCurrentPageIndex: withResolver: withRejecter:)
     func getCurrentPageIndex(tag : Int,resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
-        let reader = self.readerView()
-        reader.getCurrentPageIndex(forCPDFViewTag: tag) { pageIndex in
-            resolve(pageIndex)
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.getCurrentPageIndex(forCPDFViewTag: tag) { pageIndex in
+                resolve(pageIndex)
+            }
         }
     }
     
     @objc(hasChange: withResolver: withRejecter:)
     func hasChange(tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
-        let reader = self.readerView()
-        reader.hasChange(forCPDFViewTag: tag) { success in
-            resolve(success)
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.hasChange(forCPDFViewTag: tag) { success in
+                resolve(success)
+            }
         }
     }