فهرست منبع

ComPDFKit(flutter) - widget contrller 新增save方法

ComPDFKit-Youna 9 ماه پیش
والد
کامیت
750e9d88f0

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

@@ -25,7 +25,6 @@ import androidx.fragment.app.FragmentContainerView;
 import com.compdfkit.tools.common.pdf.CPDFConfigurationUtils;
 import com.compdfkit.tools.common.pdf.CPDFDocumentFragment;
 import com.compdfkit.tools.common.pdf.config.CPDFConfiguration;
-import com.compdfkit.tools.common.utils.CLog;
 
 import java.util.Map;
 

+ 4 - 3
example/ios/Podfile

@@ -31,9 +31,10 @@ target 'Runner' do
   use_frameworks!
   use_modular_headers!
 
-  pod "ComPDFKit", podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit/2.0.1.podspec'
-  pod "ComPDFKit_Tools", podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit_tools/2.0.1.podspec'
-
+#   pod "ComPDFKit", podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit/2.0.1.podspec'
+#   pod "ComPDFKit_Tools", podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit_tools/2.0.1.podspec'
+  pod 'ComPDFKit', :git => 'https://github.com/dinglingui/testLib.git', :tag => '2.0.1'
+  pod 'ComPDFKit_Tools', :git => 'https://github.com/dinglingui/testLib.git', :tag => '2.0.1'
   flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
   target 'RunnerTests' do
     inherit! :search_paths

+ 2 - 2
example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

@@ -51,7 +51,7 @@
       </Testables>
    </TestAction>
    <LaunchAction
-      buildConfiguration = "Release"
+      buildConfiguration = "Debug"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
       launchStyle = "0"
@@ -89,7 +89,7 @@
       </BuildableProductRunnable>
    </ProfileAction>
    <AnalyzeAction
-      buildConfiguration = "Release">
+      buildConfiguration = "Debug">
    </AnalyzeAction>
    <ArchiveAction
       buildConfiguration = "Release"

+ 56 - 0
example/lib/cpdf_reader_widget_dark_theme_example.dart

@@ -0,0 +1,56 @@
+/*
+ * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
+ *
+ * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+ * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+ * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+ * This notice may not be removed from this file.
+ *
+ */
+
+///  Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
+///
+///  THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+///  AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+///  UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+///  This notice may not be removed from this file.
+///
+
+import 'package:compdfkit_flutter/cpdf_configuration.dart';
+import 'package:compdfkit_flutter/cpdf_options.dart';
+import 'package:compdfkit_flutter/widgets/cpdf_reader_widget.dart';
+import 'package:compdfkit_flutter_example/theme/themes.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+
+class CPDFDarkThemeExample extends StatefulWidget {
+  final String documentPath;
+
+  const CPDFDarkThemeExample({super.key, required this.documentPath});
+
+  @override
+  State<CPDFDarkThemeExample> createState() => _CPDFDarkThemeExampleState();
+}
+
+class _CPDFDarkThemeExampleState extends State<CPDFDarkThemeExample> {
+  @override
+  Widget build(BuildContext context) {
+    return MaterialApp(
+        theme: darkTheme,
+        home: Scaffold(
+            resizeToAvoidBottomInset: false,
+            appBar: AppBar(
+              title: const Text('Dark Theme Example'),
+            ),
+            body: CPDFReaderWidget(
+                document: widget.documentPath,
+                configuration: CPDFConfiguration(
+                    readerViewConfig:
+                        const ReaderViewConfig(themes: CPDFThemes.dark),
+                    globalConfig: const CPDFGlobalConfig(
+                        themeMode: CPDFThemeMode.dark)),
+              onCreated: (controller) {
+
+              },)));
+  }
+}

+ 21 - 5
example/lib/cpdf_reader_widget_example.dart

@@ -6,30 +6,46 @@
 ///  This notice may not be removed from this file.
 ///
 
-
 import 'package:compdfkit_flutter/cpdf_configuration.dart';
+import 'package:compdfkit_flutter/cpdf_options.dart';
 import 'package:compdfkit_flutter/widgets/cpdf_reader_widget.dart';
+import 'package:compdfkit_flutter/widgets/cpdf_reader_widget_controller.dart';
+import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
 
-
 class CPDFReaderWidgetExample extends StatefulWidget {
   final String documentPath;
 
   const CPDFReaderWidgetExample({super.key, required this.documentPath});
 
   @override
-  State<CPDFReaderWidgetExample> createState() => _CPDFReaderWidgetExampleState();
+  State<CPDFReaderWidgetExample> createState() =>
+      _CPDFReaderWidgetExampleState();
 }
 
 class _CPDFReaderWidgetExampleState extends State<CPDFReaderWidgetExample> {
+  late CPDFReaderWidgetController _controller;
+
   @override
   Widget build(BuildContext context) {
     return Scaffold(
         resizeToAvoidBottomInset: false,
-        appBar: AppBar(title: const Text('CPDFReaderWidget Example'),),
+        appBar: AppBar(
+          title: const Text('CPDFReaderWidget Example'),
+          leading: IconButton(onPressed: () async {
+            bool saveResult = await _controller.save();
+            print('ComPDFKit-Flutter: saveResult:$saveResult');
+            Navigator.pop(context);
+          }, icon: const Icon(Icons.arrow_back)),
+        ),
         body: CPDFReaderWidget(
           document: widget.documentPath,
-          configuration: CPDFConfiguration()
+          configuration: CPDFConfiguration(),
+          onCreated: (controller) {
+            setState(() {
+              _controller = controller;
+            });
+          },
         ));
   }
 }

+ 80 - 0
example/lib/examples.dart

@@ -0,0 +1,80 @@
+/*
+ * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
+ *
+ * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+ * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+ * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+ * This notice may not be removed from this file.
+ *
+ */
+
+
+import 'dart:io';
+
+import 'package:compdfkit_flutter/compdfkit.dart';
+import 'package:compdfkit_flutter/cpdf_configuration.dart';
+import 'package:compdfkit_flutter_example/cpdf_reader_widget_dark_theme_example.dart';
+import 'package:compdfkit_flutter_example/utils/file_util.dart';
+import 'package:file_picker/file_picker.dart';
+import 'package:flutter/material.dart';
+import 'cpdf_reader_widget_example.dart';
+import 'widgets/cpdf_fun_item.dart';
+
+const String _documentPath = 'pdfs/PDF_Document.pdf';
+
+List<Widget> examples(BuildContext context) => [
+  Text(
+    'Modal View Examples',
+    style: Theme.of(context).textTheme.bodyMedium,
+  ),
+  FeatureItem(
+      title: 'Basic Example',
+      description: 'Open sample pdf document',
+      onTap: () => showDocument(context)),
+  FeatureItem(
+      title: 'Select External Files',
+      description: 'Select pdf document from system file manager',
+      onTap: () => pickDocument()),
+  FeatureItem(
+      title: 'Show CPDFReaderWidget',
+      description: 'Display PDF view in flutter widget',
+      onTap: () => showCPDFReaderWidget(context)),
+  if (Platform.isAndroid) ...[
+    FeatureItem(title: 'Dark Theme',
+        description: 'Opens a document in night mode with a custom dark theme',
+        onTap: () => showDarkThemeCPDFReaderWidget(context))
+  ]
+];
+
+
+void showDocument(context) async {
+  File document = await extractAsset(context, _documentPath);
+  ComPDFKit.openDocument(document.path,
+      password: '', configuration: CPDFConfiguration());
+}
+
+void pickDocument() async {
+  FilePickerResult? result = await FilePicker.platform.pickFiles(
+    type: FileType.custom,
+    allowedExtensions: ['pdf'],
+  );
+  if (result != null) {
+    ComPDFKit.openDocument(result.files.first.path!,
+        password: '', configuration: CPDFConfiguration());
+  }
+}
+
+void showCPDFReaderWidget(context) async {
+  File document = await extractAsset(context, _documentPath, shouldOverwrite: false);
+  goTo(CPDFReaderWidgetExample(documentPath: document.path), context);
+}
+
+void showDarkThemeCPDFReaderWidget(context) async {
+  File document = await extractAsset(context, _documentPath, shouldOverwrite: false);
+  goTo(CPDFDarkThemeExample(documentPath: document.path), context);
+}
+
+void goTo(Widget widget, BuildContext context) =>
+    Navigator.push(context, MaterialPageRoute(builder: (context) {
+      return widget;
+    }));

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 5 - 83
example/lib/main.dart


+ 9 - 2
example/lib/theme/themes.dart

@@ -7,6 +7,7 @@
 
 
 import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
 
 final ThemeData lightTheme = ThemeData(
     useMaterial3: true,
@@ -32,7 +33,10 @@ final ThemeData lightTheme = ThemeData(
             fontWeight: FontWeight.bold,
             color: Color(0xFF43474D)),
         backgroundColor: Color(0xFFFAFCFF),
-        foregroundColor: Color(0xFF43474D)),
+        foregroundColor: Color(0xFF43474D),
+        systemOverlayStyle: SystemUiOverlayStyle(
+            systemNavigationBarColor: Color(0xFFFFFFFF)
+        )),
     elevatedButtonTheme: ElevatedButtonThemeData(
         style: ElevatedButton.styleFrom(foregroundColor: Colors.blue)));
 
@@ -57,6 +61,9 @@ final ThemeData darkTheme = ThemeData(
         titleTextStyle: TextStyle(
             fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white),
         backgroundColor: Color(0xFF222429),
-        foregroundColor: Colors.white),
+        foregroundColor: Colors.white,
+    systemOverlayStyle: SystemUiOverlayStyle(
+      systemNavigationBarColor: Color(0xFF222429)
+    )),
     elevatedButtonTheme: ElevatedButtonThemeData(
         style: ElevatedButton.styleFrom(foregroundColor: Colors.blue)));

+ 42 - 0
example/lib/widgets/cpdf_app_bar.dart

@@ -0,0 +1,42 @@
+/*
+ * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
+ *
+ * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+ * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+ * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+ * This notice may not be removed from this file.
+ *
+ */
+import 'package:flutter/material.dart';
+import 'package:flutter_svg/svg.dart';
+
+import '../page/settings_page.dart';
+
+class CAppBar extends StatelessWidget implements PreferredSizeWidget {
+  const CAppBar({super.key});
+
+  @override
+  Widget build(BuildContext context) {
+    return AppBar(
+      title: const Text('ComPDFKit SDK for Flutter'),
+      actions: [
+        IconButton(
+            padding: const EdgeInsets.all(16),
+            onPressed: () {
+              Navigator.push(context, MaterialPageRoute(builder: (context) {
+                return const SettingsPage();
+              }));
+            },
+            icon: SvgPicture.asset(
+              'images/ic_home_setting.svg',
+              width: 24,
+              height: 24,
+              color: Theme.of(context).colorScheme.onPrimary,
+            ))
+      ],
+    );
+  }
+
+  @override
+  Size get preferredSize => const Size(double.infinity, 56);
+}

+ 27 - 3
ios/Classes/reader/CPDFViewCtrlFactory.swift

@@ -39,12 +39,14 @@ class CPDFViewCtrlFactory: NSObject, FlutterPlatformViewFactory {
     }
 }
 
-class CPDFViewCtrlFlutter: NSObject, FlutterPlatformView,CPDFViewBaseControllerDelete {
+class CPDFViewCtrlFlutter: NSObject, FlutterPlatformView, CPDFViewBaseControllerDelete {
     
     private var _pdfViewController : CPDFViewController
     
     private var _navigationController : CNavigationController
     
+    private var _methodChannel : FlutterMethodChannel
+    
 
     init(
         frame: CGRect,
@@ -52,7 +54,7 @@ class CPDFViewCtrlFlutter: NSObject, FlutterPlatformView,CPDFViewBaseControllerD
         arguments args: Any?,
         binaryMessenger messenger: FlutterBinaryMessenger?
     ) {
-        
+
         // 解析 文档路径、密码、配置信息
         let initInfo = args as? [String: Any]
         let jsonString = initInfo?["configuration"] ?? ""
@@ -69,13 +71,17 @@ class CPDFViewCtrlFlutter: NSObject, FlutterPlatformView,CPDFViewBaseControllerD
         _navigationController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
         _navigationController.view.frame = frame
         
+        _methodChannel = FlutterMethodChannel.init(name: "com.compdfkit.flutter.ui.pdfviewer.\(viewId)", binaryMessenger: messenger!)
+        
         super.init()
         
         // 设置代理,但是未生效
         _pdfViewController.delegate = self
         
         _navigationController.setViewControllers([_pdfViewController], animated: false)
-
+        
+        registeryMethodChannel(viewId: viewId, binaryMessenger: messenger!)
+        
     }
 
     func view() -> UIView {
@@ -85,6 +91,24 @@ class CPDFViewCtrlFlutter: NSObject, FlutterPlatformView,CPDFViewBaseControllerD
     public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
         baseControllerDelete.dismiss(animated: true)
     }
+    
+    private func registeryMethodChannel(viewId: Int64, binaryMessenger messenger: FlutterBinaryMessenger){
+
+        _methodChannel.setMethodCallHandler({
+            (call: FlutterMethodCall, result: FlutterResult) -> Void in
+            print("ComPDFKit-Flutter: iOS-MethodChannel: [method:\(call.method)]")
+              // Handle battery messages.
+            switch call.method {
+            case "save":
+                // save pdf
+                print("ComPDFKit-Flutter: save PDF")
+                result(true) // or return false
+            default:
+                result(FlutterMethodNotImplemented)
+            }
+        });
+        
+    }
 }
 
 

+ 4 - 3
lib/widgets/cpdf_reader_widget.dart

@@ -27,14 +27,15 @@ class CPDFReaderWidget extends StatefulWidget {
   /// init ComPDFKit SDK configuration
   final CPDFConfiguration configuration;
 
-  // final CPDFReaderWidgetCreatedCallback onCreated;
+  final CPDFReaderWidgetCreatedCallback onCreated;
 
   /// init callback
   const CPDFReaderWidget(
       {Key? key,
       required this.document,
       this.password = '',
-      required this.configuration})
+      required this.configuration,
+      required this.onCreated})
       : super(key: key);
 
   @override
@@ -92,6 +93,6 @@ class _CPDFReaderWidgetState extends State<CPDFReaderWidget> {
 
   Future<void> _onPlatformViewCreated(int id) async {
     debugPrint('ComPDFKit-Flutter: CPDFReaderWidget created');
-    // widget.onCreated(CPDFReaderWidgetController(id));
+    widget.onCreated(CPDFReaderWidgetController(id));
   }
 }

+ 4 - 0
lib/widgets/cpdf_reader_widget_controller.dart

@@ -19,4 +19,8 @@ class CPDFReaderWidgetController {
     });
   }
 
+  Future<bool> save() async {
+    return await _channel.invokeMethod('save');
+  }
+
 }