本页内容
运行APP
- 打开
lib/main.dart
,将所有内容替换为以下代码。并在ComPDFKit.init
方法中填写提供给您的许可证,这个简单示例将从本地设备文件系统加载 PDF 文档。
有两种不同的方法可以使用 ComPDFKit Flutter API:
- 通过插件呈现文档。
- 通过
CPDFReaderWidget
组件显示 ComPDFKit 文档视图。
使用插件
打开lib/main.dart
,将整个文件替换为以下内容:
dart
import 'dart:io';
import 'package:compdfkit_flutter/compdfkit.dart';
import 'package:compdfkit_flutter/configuration/cpdf_configuration.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 {
/// 请将其替换为您的 ComPDFKit 许可证
ComPDFKit.initialize(androidOnlineLicense : 'your compdfkit key', iosOnlineLicense : 'your compdfkit key');
/// 如果您使用离线认证许可证,请使用 init() 方法
/// ComPDFKit.init('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',
style: TextStyle(color: Colors.white),
)),
))),
);
}
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.create(recursive: true);
file.writeAsBytesSync(list);
}
var configuration = CPDFConfiguration();
ComPDFKit.openDocument(tempDocumentPath,
password: '', configuration: configuration);
}
}
使用组件
打开lib/main.dart
,将整个文件替换为以下内容:
dart
import 'dart:io';
import 'package:compdfkit_flutter/compdfkit.dart';
import 'package:compdfkit_flutter/configuration/cpdf_configuration.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 {
/// 请将其替换为您的 ComPDFKit 许可证
ComPDFKit.initialize(androidOnlineLicense : 'your compdfkit key', iosOnlineLicense : 'your compdfkit key');
/// 如果您使用离线认证许可证,请使用 init() 方法
/// ComPDFKit.init('your compdfkit key')
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: const Text('Dark Theme Example'),
),
body: _document == null
? Container()
: CPDFReaderWidget(
document: _document!,
configuration: CPDFConfiguration(),
onCreated: (_create) => {})));
}
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.create(recursive: true);
file.writeAsBytesSync(list);
}
return tempDocumentPath;
}
}
- 将您要显示的 PDF 文档添加到项目中
创建一个
pdf
目录bashmkdir pdfs
将您的示例文档复制到新创建的
pdfs
目录中,并命名为PDF_Document.pdf
- 在
pubspec.yaml
文件中指定assets
目录
diff
flutter:
+ assets:
+ - pdfs/
- 启动您的 Android 或 iOS 模拟器,或连接您的设备。
shell
flutter emulators --launch apple_ios_simulator
- 运行应用程序:
bash
flutter run