Browse Source

ComPDFKit(flutter) - android v2.2.1正式版,新增字体导入示例

liuxiaolong 1 month ago
parent
commit
f225d1a147

+ 1 - 1
android/build.gradle

@@ -41,7 +41,7 @@ android {
         implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
 
         // dependencies compdfkit pdf sdk
-        api 'com.compdf:compdfkit-tools:2.2.1-SNAPSHOT'
+        api 'com.compdf:compdfkit-tools:2.2.1'
 
         testImplementation 'junit:junit:4.13.2'
         testImplementation 'org.mockito:mockito-core:5.0.0'

+ 1 - 1
android/src/main/java/com/compdfkit/flutter/compdfkit_flutter/platformview/CPDFViewCtrlFlutter.java

@@ -87,7 +87,7 @@ public class CPDFViewCtrlFlutter implements PlatformView {
                 if (fragmentActivity != null) {
                     fragmentActivity.getSupportFragmentManager()
                             .beginTransaction()
-                            .add(fragmentContainerView.getId(), documentFragment)
+                            .add(fragmentContainerView.getId(), documentFragment, "documentFragment")
                             .setReorderingAllowed(true)
                             .commit();
                 }

BIN
example/extraFonts/Merriweather-Black.ttf


BIN
example/extraFonts/Merriweather-BlackItalic.ttf


BIN
example/extraFonts/Merriweather-Bold.ttf


BIN
example/extraFonts/Merriweather-BoldItalic.ttf


BIN
example/extraFonts/Merriweather-Italic.ttf


BIN
example/extraFonts/Merriweather-Light.ttf


BIN
example/extraFonts/Merriweather-LightItalic.ttf


BIN
example/extraFonts/Merriweather-Regular.ttf


+ 1 - 24
example/lib/cpdf_reader_widget_controller_example.dart

@@ -121,6 +121,7 @@ class _CPDFReaderWidgetControllerExampleState
         //   String? savePath = await ComPDFKit.createUri('aaa.pdf', childDirectoryName: 'compdfkit');
         bool saveResult = await controller.document.saveAs(savePath);
         debugPrint('ComPDFKit:saveAs:Result:$saveResult');
+        debugPrint('ComPDFKit:saveAs:Path:$savePath');
         break;
       case 'setScale':
         controller.setScale(1.5);
@@ -176,27 +177,6 @@ class _CPDFReaderWidgetControllerExampleState
           document.open(path, "");
         }
         break;
-      case "importAnnotations":
-        // android assets:
-        // String? xfdfFile = "file:///android_asset/test.xfdf";
-
-        // android file path sample:
-        File xfdfFile = await extractAsset(context, 'pdfs/test.xfdf');
-
-        // android Uri:
-        //String xfdfFile = "content://xxx";
-
-        bool result =
-            await controller.document.importAnnotations(xfdfFile.path);
-        debugPrint('ComPDFKit:Document: importAnnotations:$result');
-        break;
-      case "exportAnnotations":
-        String xfdfPath = await controller.document.exportAnnotations();
-        debugPrint('ComPDFKit:Document: exportAnnotations:$xfdfPath');
-        break;
-      case "removeAllAnnotations":
-        await controller.document.removeAllAnnotations();
-        break;
       case "removeSignFileList":
         bool result = await ComPDFKit.removeSignFileList();
         debugPrint('ComPDFKit:removeSignFileList:$result');
@@ -260,9 +240,6 @@ var actions = [
   'setDisplayPageIndex',
   'isChanged',
   'documentInfo',
-  'importAnnotations',
-  'exportAnnotations',
-  'removeAllAnnotations',
   'removeSignFileList',
   'print'
 ];

+ 2 - 1
example/lib/cpdf_reader_widget_security_example.dart

@@ -111,6 +111,7 @@ class _CPDFReaderWidgetSecurityExampleState
         await controller.document.createWatermark(CPDFWatermark.image(
           imagePath: imageFile.path,
           opacity: 1,
+          scale: 0.5,
           rotation: 45,
           pages: [0, 1, 2, 3],
           horizontalAlignment: CPDFWatermarkHorizontalAlignment.center,
@@ -125,7 +126,7 @@ class _CPDFReaderWidgetSecurityExampleState
           await controller.document.createWatermark(CPDFWatermark.image(
             imagePath: pickerResult.files.first.path!,
             pages: [0, 1, 2, 3],
-            scale: 1,
+            scale: 0.5,
             horizontalAlignment: CPDFWatermarkHorizontalAlignment.center,
             verticalAlignment: CPDFWatermarkVerticalAlignment.center,
           ));

+ 5 - 9
example/lib/main.dart

@@ -10,6 +10,7 @@ import 'dart:io';
 import 'package:compdfkit_flutter/compdfkit.dart';
 import 'package:compdfkit_flutter_example/examples.dart';
 import 'package:compdfkit_flutter_example/theme/themes.dart';
+import 'package:compdfkit_flutter_example/utils/file_util.dart';
 import 'package:compdfkit_flutter_example/widgets/cpdf_app_bar.dart';
 
 import 'package:flutter/material.dart';
@@ -53,15 +54,8 @@ class _HomePageState extends State<HomePage> {
   }
 
   void _init() async {
-
-    // final tempDir = await ComPDFKit.getTemporaryDirectory();
-    // await extractAsset(context, 'extraFonts/arial.ttf');
-    // await extractAsset(context, 'extraFonts/Arial Bold.ttf');
-    // await extractAsset(context, 'extraFonts/Arial_Italic.ttf');
-    // await extractAsset(context, 'extraFonts/arialmt.ttf');
-    // await extractAsset(context, 'extraFonts/ArialUnicode.ttf');
-
-    // await ComPDFKit.setImportFontDir('${tempDir.path}/extraFonts/', addSysFont: true);
+    final fontDir = await extractAssetFolder(context, 'extraFonts/');
+    await ComPDFKit.setImportFontDir(fontDir, addSysFont: false);
 
     // online license auth
     // Please replace it with your ComPDFKit license
@@ -71,6 +65,8 @@ class _HomePageState extends State<HomePage> {
     ComPDFKit.init(Platform.isAndroid ? androidLicenseKey : iosLicenseKey);
   }
 
+
+
   @override
   Widget build(BuildContext context) {
     return Scaffold(

+ 22 - 0
example/lib/utils/file_util.dart

@@ -5,10 +5,12 @@
 // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
 // This notice may not be removed from this file.
 
+import 'dart:convert';
 import 'dart:io';
 
 import 'package:compdfkit_flutter/compdfkit.dart';
 import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
 
 Future<File> extractAsset(BuildContext context, String assetPath,
     {bool shouldOverwrite = false, String prefix = ''}) async {
@@ -25,3 +27,23 @@ Future<File> extractAsset(BuildContext context, String assetPath,
   }
   return file;
 }
+
+Future<String> extractAssetFolder(BuildContext context, String folder) async {
+  final tempDir = await ComPDFKit.getTemporaryDirectory();
+  final assetPaths = await getAssetPaths(folder);
+  for (var path in assetPaths) {
+    await extractAsset(context, path);
+  }
+  String dir = '${tempDir.path}/$folder';
+  debugPrint('ComPDFKit-fontDir: $dir');
+  return dir;
+}
+
+Future<List<String>> getAssetPaths(String folderPath) async {
+  final manifestJson = await rootBundle.loadString('AssetManifest.json');
+  final Map<String, dynamic> manifestMap = json.decode(manifestJson);
+  final assetPaths = manifestMap.keys
+      .where((key) => key.startsWith(folderPath))
+      .toList();
+  return assetPaths;
+}

+ 1 - 1
example/pubspec.yaml

@@ -60,7 +60,7 @@ flutter:
   assets:
     - pdfs/
     - images/
-#    - extraFonts/
+    - extraFonts/
   # To add assets to your application, add an assets section, like this:
   # assets:
   #   - images/a_dot_burr.jpeg