Browse Source

ComPDFKit(flutter) - 新增导入字体接口(Android)

liuxiaolong 2 months ago
parent
commit
2a74d662e6

+ 6 - 6
README.md

@@ -151,7 +151,7 @@ Alternatively you can update the `AndroidManifest.xml` file to use `FlutterFragm
  dependencies:
    flutter:
      sdk: flutter
-+  compdfkit_flutter: ^2.2.0
++  compdfkit_flutter: ^2.2.1
 ```
 
 8. Add the PDF documents you want to display in the project
@@ -197,7 +197,7 @@ cd example
  dependencies:
    flutter:
      sdk: flutter
-+  compdfkit_flutter: ^2.2.0
++  compdfkit_flutter: ^2.2.1
 ```
 
 4. Open your project's Podfile in a text editor:
@@ -220,8 +220,8 @@ open ios/Podfile
    use_modular_headers!`
 
    flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
-+  pod 'ComPDFKit_Tools', podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit_tools/2.2.0.podspec'
-+  pod 'ComPDFKit', podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit/2.2.0.podspec'
++  pod 'ComPDFKit_Tools', podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit_tools/2.2.1.podspec'
++  pod 'ComPDFKit', podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit/2.2.1.podspec'
 
  end
 ```
@@ -557,8 +557,8 @@ target 'PDFView_RN' do
     # Pods for testing
   end
 
-+  pod 'ComPDFKit', :git => 'https://github.com/ComPDFKit/compdfkit-pdf-sdk-ios-swift.git', :tag => '2.2.0'
-+  pod 'ComPDFKit_Tools', :git => 'https://github.com/ComPDFKit/compdfkit-pdf-sdk-ios-swift.git', :tag => '2.2.0'
++  pod 'ComPDFKit', :git => 'https://github.com/ComPDFKit/compdfkit-pdf-sdk-ios-swift.git', :tag => '2.2.1'
++  pod 'ComPDFKit_Tools', :git => 'https://github.com/ComPDFKit/compdfkit-pdf-sdk-ios-swift.git', :tag => '2.2.1'
 
   # Enables Flipper.
   #

+ 1 - 0
android/src/main/java/com/compdfkit/flutter/compdfkit_flutter/constants/CPDFConstants.java

@@ -149,6 +149,7 @@ public class CPDFConstants {
     public static final String CREATE_WATERMARK = "create_watermark";
     public static final String REMOVE_ALL_WATERMARKS = "remove_all_watermarks";
     public static final String GET_ENCRYPT_ALGORITHM = "get_encrypt_algorithm";
+    public static final String SET_IMPORT_FONT_DIRECTORY = "set_import_font_directory";
   }
 
 }

+ 9 - 1
android/src/main/java/com/compdfkit/flutter/compdfkit_flutter/plugin/ComPDFKitSDKPlugin.java

@@ -18,6 +18,7 @@ import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.Ch
 import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.REMOVE_SIGN_FILE_LIST;
 import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.SDK_BUILD_TAG;
 import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.SDK_VERSION_CODE;
+import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.SET_IMPORT_FONT_DIRECTORY;
 
 import android.app.Activity;
 import android.content.Context;
@@ -31,6 +32,7 @@ import androidx.annotation.NonNull;
 
 import androidx.annotation.Nullable;
 import com.compdfkit.core.document.CPDFSdk;
+import com.compdfkit.core.font.CPDFFont;
 import com.compdfkit.flutter.compdfkit_flutter.utils.FileUtils;
 import com.compdfkit.tools.common.pdf.CPDFConfigurationUtils;
 import com.compdfkit.tools.common.pdf.CPDFDocumentActivity;
@@ -97,7 +99,7 @@ public class ComPDFKitSDKPlugin extends BaseMethodChannelPlugin implements Plugi
                 context.startActivity(intent);
                 break;
             case GET_TEMP_DIRECTORY:
-                result.success(context.getCacheDir().getPath());
+                result.success(context.getCacheDir().getAbsolutePath());
                 break;
             case REMOVE_SIGN_FILE_LIST:
                 File dirFile = new File(context.getFilesDir(), CFileUtils.SIGNATURE_FOLDER);
@@ -127,6 +129,12 @@ public class ComPDFKitSDKPlugin extends BaseMethodChannelPlugin implements Plugi
                     result.error("CREATE_URI_FAIL", "create uri fail", "");
                 }
                 break;
+            case SET_IMPORT_FONT_DIRECTORY:
+                String importFontDir = call.argument("dir_path");
+                boolean addSysFont = call.argument("add_sys_font");
+                CPDFSdk.setImportFontDir(importFontDir, addSysFont);
+                result.success(true);
+                break;
             default:
                 break;
         }

+ 1 - 0
example/android/settings.gradle

@@ -14,6 +14,7 @@ pluginManagement {
         google()
         mavenCentral()
         gradlePluginPortal()
+        mavenLocal()
     }
 }
 

+ 16 - 16
example/lib/cpdf_reader_widget_controller_example.dart

@@ -75,14 +75,14 @@ class _CPDFReaderWidgetControllerExampleState
   }
 
   void _save() async {
-    bool saveResult = await _controller!.save();
+    bool saveResult = await _controller!.document.save();
     debugPrint('ComPDFKit-Flutter: saveResult:$saveResult');
   }
 
   List<Widget> _buildAppBarActions(BuildContext context) {
     return [
       PopupMenuButton<String>(
-        icon: Icon(Icons.settings),
+        icon: const Icon(Icons.settings),
         onSelected: (value) {
           handleClick(value, _controller!);
         },
@@ -131,12 +131,10 @@ class _CPDFReaderWidgetControllerExampleState
         String savePath = '${tempDir.path}/temp/${await controller.document.getFileName()}';
       // only android platform
       //   String? savePath = await ComPDFKit.createUri('aaa.pdf', childDirectoryName: 'compdfkit');
-        if (savePath != null) {
-          debugPrint('ComPDFKit:saveAs:$savePath');
-          bool saveResult = await controller.document.saveAs(savePath);
-          debugPrint('ComPDFKit:saveAs:Result:$saveResult');
-        }
-        break;
+        debugPrint('ComPDFKit:saveAs:$savePath');
+        bool saveResult = await controller.document.saveAs(savePath);
+        debugPrint('ComPDFKit:saveAs:Result:$saveResult');
+              break;
       case 'setScale':
         controller.setScale(1.5);
         double scaleValue = await controller.getScale();
@@ -188,7 +186,7 @@ class _CPDFReaderWidgetControllerExampleState
         break;
       case 'setDisplayPageIndex':
         int currentPageIndex = await controller.getCurrentPageIndex();
-        debugPrint('ComPDFKit:getCurrentPageIndex:${currentPageIndex}');
+        debugPrint('ComPDFKit:getCurrentPageIndex:$currentPageIndex');
         int nextPageIndex = currentPageIndex + 1;
         controller.setDisplayPageIndex(nextPageIndex, animated: true);
         break;
@@ -258,13 +256,15 @@ class _CPDFReaderWidgetControllerExampleState
         break;
       case "PreviewMode":
         CPDFViewMode mode = await controller.getPreviewMode();
-        CPDFViewMode? switchMode = await showModalBottomSheet(
-            context: context,
-            builder: (context) {
-              return CpdfReaderWidgetSwitchPreviewModePage(viewMode: mode);
-            });
-        if (switchMode != null) {
-          await controller.setPreviewMode(switchMode);
+        if(mounted){
+          CPDFViewMode? switchMode = await showModalBottomSheet(
+              context: context,
+              builder: (context) {
+                return CpdfReaderWidgetSwitchPreviewModePage(viewMode: mode);
+              });
+          if (switchMode != null) {
+            await controller.setPreviewMode(switchMode);
+          }
         }
         break;
       case 'DisplaySetting':

+ 1 - 1
example/lib/cpdf_reader_widget_example.dart

@@ -53,7 +53,7 @@ class _CPDFReaderWidgetExampleState extends State<CPDFReaderWidgetExample> {
   }
 
   void _save() async {
-    bool saveResult = await _controller.save();
+    bool saveResult = await _controller.document.save();
     debugPrint('ComPDFKit-Flutter: saveResult:$saveResult');
   }
 }

+ 0 - 5
example/lib/cpdf_reader_widget_security_example.dart

@@ -6,20 +6,15 @@
 // This notice may not be removed from this file.
 
 import 'dart:io';
-import 'dart:math';
 
-import 'package:compdfkit_flutter/compdfkit.dart';
 import 'package:compdfkit_flutter/configuration/cpdf_configuration.dart';
 import 'package:compdfkit_flutter/configuration/cpdf_options.dart';
 import 'package:compdfkit_flutter/document/cpdf_watermark.dart';
-import 'package:compdfkit_flutter/util/extension/cpdf_color_extension.dart';
 import 'package:compdfkit_flutter/widgets/cpdf_reader_widget.dart';
 import 'package:compdfkit_flutter/widgets/cpdf_reader_widget_controller.dart';
-import 'package:compdfkit_flutter_example/page/cpdf_reader_widget_switch_preview_mode_page.dart';
 import 'package:compdfkit_flutter_example/utils/file_util.dart';
 import 'package:file_picker/file_picker.dart';
 import 'package:flutter/material.dart';
-import 'package:flutter/widgets.dart';
 
 class CPDFReaderWidgetSecurityExample extends StatefulWidget {
   final String documentPath;

+ 1 - 0
example/lib/main.dart

@@ -53,6 +53,7 @@ class _HomePageState extends State<HomePage> {
   }
 
   void _init() async {
+    // await ComPDFKit.setImportFontDir('fonts directory',addSysFont: true);
     // online license auth
     // Please replace it with your ComPDFKit license
     // ComPDFKit.initialize(androidOnlineLicense: 'IVTAsbJCW0X45qIy5cTEuzxZzKpYIpJe6WPY7uCPIiI=',iosOnlineLicense: 'lkw3Gr0HuD5pV1/+DVRSxp7qBlvK+Izo3mOKyAEHXz4=');

+ 2 - 2
example/lib/page/cpdf_reader_widget_display_setting_page.dart

@@ -64,8 +64,8 @@ class _CpdfReaderWidgetDisplaySettingPageState
   Widget _scrollItem() {
     var textStyle =  Theme.of(context).textTheme.bodyMedium;
     return Column(children: [
-      ListTile(title: Text('Vertical Scrolling', style: textStyle), trailing: this._isVertical ? const Icon(Icons.check) : null,),
-      ListTile(title: Text('Horizontal Scrolling', style: textStyle,),trailing: !this._isVertical ? const Icon(Icons.check) : null,),
+      ListTile(title: Text('Vertical Scrolling', style: textStyle), trailing: _isVertical ? const Icon(Icons.check) : null,),
+      ListTile(title: Text('Horizontal Scrolling', style: textStyle,),trailing: !_isVertical ? const Icon(Icons.check) : null,),
     ],);
   }
 

+ 21 - 0
lib/compdfkit.dart

@@ -126,4 +126,25 @@ class ComPDFKit {
     });
     return uri;
   }
+
+  /// Import font directory and configure whether to include system fonts.
+  ///
+  /// - Parameters:
+  ///   - **dirPath**: The directory path where the font files are stored.
+  ///     The imported fonts will be available for selection in text annotations and content editing, resolving issues with missing text in certain languages.
+  ///   - **addSysFont**: Whether to include system fonts. Default is `true`, which will show system fonts in the font list.
+  ///
+  /// Note:
+  /// This method must be called before [ComPDFKit.init] or [ComPDFKit.initialize], otherwise the settings will have no effect.
+  ///
+  /// Example:
+  /// ```dart
+  /// ComPDFKit.setImportFontDir('path/to/your/font', addSysFont: true);
+  /// ComPDF
+  static Future<bool> setImportFontDir(String dirPath, {bool addSysFont = true}) async {
+    return await _methodChannel.invokeMethod('set_import_font_directory', {
+      'dir_path': dirPath,
+      'add_sys_font': addSysFont
+    });
+  }
 }

+ 4 - 3
lib/document/cpdf_document.dart

@@ -8,6 +8,7 @@
 import 'package:compdfkit_flutter/configuration/cpdf_options.dart';
 import 'package:compdfkit_flutter/document/cpdf_watermark.dart';
 import 'package:flutter/services.dart';
+import 'package:flutter/widgets.dart';
 
 /// A class to handle PDF documents without using [CPDFReaderWidget]
 ///
@@ -205,7 +206,7 @@ class CPDFDocument {
         'font_sub_set' : fontSubSet
       });
     } on PlatformException catch (e) {
-      print(e.details);
+      debugPrint(e.details);
       return false;
     } catch (e) {
       return false;
@@ -233,7 +234,7 @@ class CPDFDocument {
     try{
       return await _channel.invokeMethod('remove_password');
     } on PlatformException catch (e) {
-      print(e.message);
+      debugPrint(e.message);
       return false;
     }
   }
@@ -270,7 +271,7 @@ class CPDFDocument {
         'encrypt_algo' : encryptAlgo.name
       });
     } on PlatformException catch (e) {
-      print(e.message);
+      debugPrint(e.message);
       return false;
     }
   }

+ 46 - 4
lib/widgets/cpdf_reader_widget_controller.dart

@@ -404,30 +404,72 @@ class CPDFReaderWidgetController {
     return CPDFViewMode.values.where((e) => e.name == modeName).first;
   }
 
-  Future<void> showThumbnailView(bool editMode) async{
+  /// Displays the thumbnail view. When [editMode] is `true`, the page enters edit mode, allowing operations such as insert, delete, extract, etc.
+  ///
+  /// Example:
+  /// ```dart
+  /// await controller.showThumbnailView(false);
+  /// ```
+  Future<void> showThumbnailView(bool editMode) async {
     await _channel.invokeMethod('show_thumbnail_view', editMode);
   }
 
-  Future<void> showBotaView() async{
+  /// Displays the BOTA view, which includes the document outline, bookmarks, and annotation list.
+  ///
+  /// Example:
+  /// ```dart
+  /// await controller.showBotaView();
+  /// ```
+  Future<void> showBotaView() async {
     await _channel.invokeMethod('show_bota_view');
   }
 
-  Future<void> showAddWatermarkView() async{
+  /// Displays the "Add Watermark" view, where users can add watermarks to the document.
+  ///
+  /// Example:
+  /// ```dart
+  /// await controller.showAddWatermarkView();
+  /// ```
+  Future<void> showAddWatermarkView() async {
     await _channel.invokeMethod('show_add_watermark_view');
   }
 
+  /// Displays the document security settings view, allowing users to configure document security options.
+  ///
+  /// Example:
+  /// ```dart
+  /// await controller.showSecurityView();
+  /// ```
   Future<void> showSecurityView() async {
     await _channel.invokeMethod('show_security_view');
   }
 
-  Future<void> showDisplaySettingView() async{
+  /// Displays the display settings view, where users can configure options such as scroll direction, scroll mode, and themes.
+  ///
+  /// Example:
+  /// ```dart
+  /// await controller.showDisplaySettingView();
+  /// ```
+  Future<void> showDisplaySettingView() async {
     await _channel.invokeMethod('show_display_settings_view');
   }
 
+  /// Enters snip mode, allowing users to capture screenshots.
+  ///
+  /// Example:
+  /// ```dart
+  /// await controller.enterSnipMode();
+  /// ```
   Future<void> enterSnipMode() async {
     await _channel.invokeMethod('enter_snip_mode');
   }
 
+  /// Exits snip mode, stopping the screenshot capture.
+  ///
+  /// Example:
+  /// ```dart
+  /// await controller.exitSnipMode();
+  /// ```
   Future<void> exitSnipMode() async {
     await _channel.invokeMethod('exit_snip_mode');
   }