Browse Source

compdfkit(rn) - 新增获取文件路径接口

liuxiaolong 1 week ago
parent
commit
c23ecdf6ad

+ 5 - 0
android/src/main/java/com/compdfkitpdf/reactnative/modules/CPDFViewModule.java

@@ -413,6 +413,11 @@ public class CPDFViewModule extends ReactContextBaseJavaModule {
     uiBlock(nativeViewHierarchyManager -> mPDFViewInstance.reloadPages(tag));
   }
 
+  @ReactMethod
+  public void getDocumentPath(int tag, Promise promise){
+    uiBlock(nativeViewHierarchyManager -> promise.resolve(mPDFViewInstance.getDocumentPath(tag)));
+  }
+
 
   private void uiBlock(UIBlock uiBlock) {
     UIManagerModule uiManager = getReactApplicationContext().getNativeModule(UIManagerModule.class);

+ 10 - 0
android/src/main/java/com/compdfkitpdf/reactnative/viewmanager/CPDFViewManager.java

@@ -661,4 +661,14 @@ public class CPDFViewManager extends ViewGroupManager<CPDFView> {
     CPDFView cpdfView = mDocumentViews.get(tag);
     cpdfView.getCPDFReaderView().reloadPages();
   }
+
+  public String getDocumentPath(int tag) {
+    CPDFView pdfView = mDocumentViews.get(tag);
+    CPDFReaderView readerView = pdfView.getCPDFReaderView();
+    CPDFDocument document = readerView.getPDFDocument();
+    if (!TextUtils.isEmpty(document.getAbsolutePath())){
+      return document.getAbsolutePath();
+    }
+    return document.getUri().toString();
+  }
 }

+ 1 - 0
example/src/CPDFSecurityExample.tsx

@@ -77,6 +77,7 @@ const CPDFSecurityExampleScreen = () => {
                 break;
             case 'Document Info':
                 console.log('ComPDFKit-RN fileName:', await document?.getFileName());
+                console.log('ComPDFKit-RN documentPath:', await document?.getDocumentPath());
                 console.log('ComPDFKit-RN pageCount:', await document?.getPageCount());
                 console.log('ComPDFKit-RN isEncrypted:', await document?.isEncrypted());
                 console.log('ComPDFKit-RN isImageDoc:', await document?.isImageDoc());

+ 17 - 3
src/view/CPDFDocument.tsx

@@ -354,7 +354,7 @@ export class CPDFDocument {
      * Invokes the system's print service to print the current document.
      * @example
      * await pdfReaderRef.current?._pdfDocument.printDocument();
-     * @returns 
+     * @returns
      */
     printDocument = () : Promise<void> => {
         const tag = findNodeHandle(this._viewerRef);
@@ -395,12 +395,12 @@ export class CPDFDocument {
      * const fontSubset = true;
      * const result = await pdfReaderRef.current?._pdfDocument.saveAs(savePath, removeSecurity, fontSubset);
      * @param savePath Specifies the path where the document should be saved.<br>
-     * 
+     *
      *      On Android, both file paths and URIs are supported. For example:
      *      - File path: `/data/user/0/com.compdfkit.flutter.example/cache/temp/PDF_Document.pdf`
      *      - URI: `content://media/external/file/1000045118`
      * @param removeSecurity Whether to remove the document's password.
-     * @param fontSubset 
+     * @param fontSubset
      * @returns Whether to embed the font subset when saving the PDF.
      */
     saveAs = (savePath : string, removeSecurity : boolean, fontSubset : boolean) : Promise<string> => {
@@ -415,6 +415,20 @@ export class CPDFDocument {
         return Promise.reject('Unable to find the native view reference');
     }
 
+    /**
+     * 返回当前文档的路径。
+     * 在安卓平台,如果通过uri打开的文档,会返回uri
+     * @example
+     * const documentPath = await pdfReaderRef.current?._pdfDocument.getDocumentPath();
+     * @returns The path of the current document.
+     */
+    getDocumentPath = () : Promise<string> => {
+        const tag = findNodeHandle(this._viewerRef);
+        if (tag != null) {
+            return CPDFViewManager.getDocumentPath(tag);
+        }
+        return Promise.reject('Unable to find the native view reference');
+    }
 
 }
 // export default CPDFDocument;