Run Project
- Open
lib/main.dart
and replace the entire content with the following code. And fill in the license provided to you in theComPDFKit.init
method.
There are 2 different ways to use ComPDFKit Flutter API:
- Present a document via a plugin.
- Show a ComPDFKit document view via a Widget.
Usage Plugin
Open lib/main.dart
,replace the entire file with the following:
dart
import 'dart:io';
import 'package:compdfkit_flutter/compdfkit.dart';
import 'package:compdfkit_flutter/configuration/cpdf_configuration.dart';
import 'package:compdfkit_flutter/util/cpdf_file_util.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 {
File licenseFile = await CPDFFileUtil.extractAsset(context, 'assets/license_key_flutter.xml');
ComPDFKit.initWithPath(licenseFile.path);
}
@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 {
var pdfFile = await CPDFFileUtil.extractAsset(_documentPath);
var configuration = CPDFConfiguration();
// Present a document via a plugin.
ComPDFKit.openDocument(pdfFile.path,
password: '', configuration: configuration);
}
}
Usage Widget
Open lib/main.dart
,replace the entire file with the following:
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:compdfkit_flutter/util/cpdf_file_util.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().then((value) {
setState(() {
_document = value;
});
});
}
void _init() async {
/// Please replace it with your ComPDFKit license
File licenseFile = await CPDFFileUtil.extractAsset(context, 'assets/license_key_flutter.xml');
ComPDFKit.initWithPath(licenseFile.path);
}
Future<String> _getDocumentPath() async {
var file = await CPDFFileUtil.extractAsset('pdfs/PDF_Document.pdf');
return file.path;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: const Text('CPDFReaderWidget Example'),
),
body: _document == null
? Container()
: CPDFReaderWidget(
document: _document!,
configuration: CPDFConfiguration(),
onCreated: (_create) => {})));
}
}
- Add the PDF documents you want to display in the project
create a
pdf
directorybashmkdir pdfs
Copy your example document into the newly created
pdfs
directory and name itPDF_Document.pdf
- Specify the
assets
directory inpubspec.yaml
diff
flutter:
+ assets:
+ - pdfs/
- Launch your Android, iOS emulator, or connect your device.
shell
flutter emulators --launch apple_ios_simulator
- Run the app with:
bash
flutter run