Flutter PDF阅读插件compdfkit_flutter的使用
Flutter PDF阅读插件compdfkit_flutter的使用
概述
ComPDFKit Flutter PDF Library 是一个强大的PDF库,提供了快速查看、注释、编辑和签署PDF文件的全面功能。它功能丰富且经过实战测试,使PDF文件处理和操作更加简单快捷。
ComPDFKit for Flutter允许您快速将PDF功能添加到任何Flutter应用程序中,提升您的Android和iOS应用程序,确保无缝和高效的开发。它可在 pub.dev 和 GitHub 获取。
关键特性
- Viewer:提供标准页面显示模式、导航、文本搜索与选择、缩放和平移、文本重排等功能。
- Annotations:提供笔记、链接、自由文本、线条、矩形、圆形、高亮、下划线、波浪线、删除线、图章、墨水、声音等注释类型。
- Forms:提供按钮、复选框、单选按钮、文本框、组合框、列表框、签名等表单元素。
- Document Editor:提供拆分、提取、合并、删除、插入、裁剪、移动、旋转、替换和交换页面等功能。
- Content Editor:提供复制、调整大小、更改颜色、文本对齐、查找和替换等功能。
- Security:提供加密和解密PDF文件、水印等功能。
快速开始
环境要求
Android
- 最新稳定版本的Flutter
- 最新稳定版本的Android Studio
- Android NDK
- Android虚拟设备(AVD)
- 操作环境要求:
minSdkVersion
21或更高compileSdkVersion
34或更高targetSdkVersion
34或更高- 支持的ABI:x86, x86_64, armeabi-v7a, arm64-v8a
iOS
- 最新稳定版本的Flutter
- 最新稳定版本的Xcode
- 最新稳定版本的CocoaPods
- 操作环境要求:
- iOS 12.0或更高
- Xcode 12.0或更新版本
集成到新的Flutter应用
Android
-
创建一个名为
example
的Flutter项目:flutter create --org com.compdfkit.flutter example
-
进入项目目录:
cd example
-
修改
AndroidManifest.xml
,添加网络和存储权限:<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.compdfkit.flutter.example"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/> <application android:requestLegacyExternalStorage="true"> </application> </manifest>
-
修改
build.gradle
,设置最小SDK版本为21:android { defaultConfig { minSdkVersion 21 ... } }
-
修改
MainActivity.java
,继承FlutterFragmentActivity
:import io.flutter.embedding.android.FlutterFragmentActivity; public class MainActivity extends FlutterFragmentActivity { }
-
添加ComPDFKit依赖到
pubspec.yaml
:dependencies: flutter: sdk: flutter compdfkit_flutter: ^2.2.1
-
创建并添加PDF文件到
pdfs
目录,并在pubspec.yaml
中指定资源路径:flutter: assets: - pdfs/
-
获取所有依赖包:
flutter pub get
iOS
-
创建一个名为
example
的Flutter项目:flutter create --org com.compdfkit.flutter example
-
进入项目目录:
cd example
-
添加ComPDFKit依赖到
pubspec.yaml
:dependencies: flutter: sdk: flutter compdfkit_flutter: ^2.2.1
-
修改
Podfile
,更新平台为iOS 12并添加ComPDFKit Podspec:platform :ios, '12.0' target 'Runner' do use_frameworks! 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.1.podspec' pod 'ComPDFKit', podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit/2.2.1.podspec' end
-
安装Pods:
pod install
-
创建并添加PDF文件到
pdfs
目录,并在pubspec.yaml
中指定资源路径:flutter: assets: - pdfs/
-
获取所有依赖包:
flutter pub get
-
在
Info.plist
中配置隐私条款:<key>NSCameraUsageDescription</key> <string>Your consent is required before you could access the function.</string> <key>NSMicrophoneUsageDescription</key> <string>Your consent is required before you could access the function.</string> <key>NSPhotoLibraryAddUsageDescription</key> <string>Your consent is required before you could access the function.</string> <key>NSPhotoLibraryUsageDescription</key> <string>Your consent is required before you could access the function.</string> <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
应用许可证密钥
import 'package:compdfkit_flutter/compdfkit.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await ComPDFKit.initialize(
androidOnlineLicense: 'your compdfkit key',
iosOnlineLicense: 'your compdfkit key');
runApp(MyApp());
}
使用示例
使用Plugin打开PDF文档
import 'dart:io';
import 'package:compdfkit_flutter/compdfkit.dart';
import 'package:flutter/material.dart';
const String _documentPath = 'pdfs/PDF_Document.pdf';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
_init();
}
void _init() async {
await ComPDFKit.initialize(
androidOnlineLicense: 'your compdfkit key',
iosOnlineLicense: 'your compdfkit key');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: ElevatedButton(
onPressed: () async {
showDocument(context);
},
child: const Text('Open Document'),
),
),
),
),
);
}
void showDocument(BuildContext context) async {
final bytes = await DefaultAssetBundle.of(context).load(_documentPath);
final list = bytes.buffer.asUint8List();
final tempDir = await ComPDFKit.getTemporaryDirectory();
var pdfsDir = Directory('${tempDir.path}/pdfs');
pdfsDir.createSync(recursive: true);
final tempDocumentPath = '${tempDir.path}/$_documentPath';
final file = File(tempDocumentPath);
if (!file.existsSync()) {
file.createSync(recursive: true);
file.writeAsBytesSync(list);
}
var configuration = CPDFConfiguration();
ComPDFKit.openDocument(tempDocumentPath,
password: '', configuration: configuration);
}
}
使用Widget打开PDF文档
import 'dart:io';
import 'package:compdfkit_flutter/compdfkit.dart';
import 'package:compdfkit_flutter/widgets/cpdf_reader_widget.dart';
import 'package:flutter/material.dart';
const String _documentPath = 'pdfs/PDF_Document.pdf';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? _document;
@override
void initState() {
super.initState();
_init();
_getDocumentPath(context).then((value) {
setState(() {
_document = value;
});
});
}
void _init() async {
await ComPDFKit.initialize(
androidOnlineLicense: 'your compdfkit key',
iosOnlineLicense: 'your compdfkit key');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('CPDFReaderWidget Example')),
body: _document == null
? Container()
: CPDFReaderWidget(
document: _document!,
configuration: CPDFConfiguration(),
),
),
);
}
Future<String> _getDocumentPath(BuildContext context) async {
final bytes = await DefaultAssetBundle.of(context).load(_documentPath);
final list = bytes.buffer.asUint8List();
final tempDir = await ComPDFKit.getTemporaryDirectory();
var pdfsDir = Directory('${tempDir.path}/pdfs');
pdfsDir.createSync(recursive: true);
final tempDocumentPath = '${tempDir.path}/$_documentPath';
final file = File(tempDocumentPath);
if (!file.existsSync()) {
file.createSync(recursive: true);
file.writeAsBytesSync(list);
}
return tempDocumentPath;
}
}
UI定制
通过CPDFConfiguration
类可以定义多种配置选项,以满足产品需求。例如:
-
设置初始显示模式和可用模式:
var configuration = CPDFConfiguration(modeConfig: const ModeConfig( initialViewMode: CPreviewMode.viewer, availableViewModes: [ CPreviewMode.viewer, CPreviewMode.annotations ] ));
-
设置启用的注释类型和默认注释属性值:
var configuration = CPDFConfiguration( annotationsConfig: const CPDFAnnotationsConfig( availableTypes: [CPDFAnnotationType.highlight], initAttribute: CPDFAnnotationAttribute( highlight: CPDFAnnotAttr.highlight(color: Colors.blue, alpha: 255) ) ) );
-
设置显示模式和页面翻转方向:
var configuration = CPDFConfiguration( readerViewConfig: const ReaderViewConfig( displayMode: CPDFDisplayMode.doublePage, verticalMode: false ) );
示例应用
您可以查看我们的Flutter示例应用和API参考了解更多详细信息。
支持
如果您有任何问题或需要技术支持,请访问我们的指南页面,或联系我们的技术支援团队。
许可证
ComPDFKit PDF SDK支持灵活的许可选项,详情请联系我们的sales团队。
希望以上内容能帮助您更好地理解和使用ComPDFKit Flutter PDF Library!如果有任何问题,请随时联系我们的支持团队。
更多关于Flutter PDF阅读插件compdfkit_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html