compdfkit_flutter 1.13.0-dev.2 compdfkit_flutter: ^1.13.0-dev.2 copied to clipboard
ComPDFKit for Flutter is a comprehensive SDK that allows you to quickly add PDF functionality to Android and iOS Flutter applications.
/// 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/cpdf_options.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
const String DOCUMENT_PATH = '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 {
// online license auth
// Please replace it with your ComPDFKit license
// ComPDFKit.initialize(
// androidOnlineLicense: 'android platform compdfkit online license',
// iosOnlineLicense: 'ios platform compdfkit online license');
// offline license auth
ComPDFKit.init(
'yObcn+VTuQP5XLYNIM28ZK8RXXmoHF8X9mlBM9JhXWL3ZXrjotdX36h1SusDWGSc2t0FUrWu49BYDKCAmQ+3GKbQ6am/67/6o77DaPGFWTsdVPYgAqivMVwSuulnaK9SIjMuPqIwmul2/DBdIDRNxgB/vL3T/7hAX8vF/w1JlURQKoUARS4gNoWCjQ8lJtzkblC8UabOEIGXm1psATU9HIewC86bW7uML03+Fo3z5Dnn5gdf0WkYEmisYnT7JbiYSHnPqWgnGYlzbEcp0IBs2QkCpm0PkwXGv1RjRUmU5CTr9VIjoAyvjx66JXD+J+rqEIMyA36+0x+0MBTGG8kIl2u0lH1oY17eVN2TRW5amzqCXJoH4JUoBrFuvx7jqOwQuX0wzykREfAWzG3IcRQVWXh9O4SrbLT/ISo0UvqIHuZitUoXodeVHdb2lgtikKumpzUOHDGJVMyK1TDG2GHZDb23xmqJkCj3yULxplRMJtddpII7oUIQL6yP5G33sE6UZLjSaf6CrC01Hg4GR2RXtL6FumMbrwwplWzGYfZyEUv2YR0d1AwdP5z0fYHI6xtrQij88FMMp49Xto4myQZxDTSoMwz+tr31jFCUjbEj/eBwy40H6P4fc2D9PZm2XGXHK5A32pegFtGaIjjc0BlqmESHcXnXdFwWjS4XTDWk3tscpt2fCfeAm/Hoy0V7jgUeNYV0Hx3c730HuQeZlVxoQb99e1PkXtLXZjcV9OgMHGbdG9VJxZnQSA7TSy4fHpebTSsbRmzsSDkeM/v3k1jSCw==');
}
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());
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
showDocument(context);
},
child: const Text('Open Document')),
ElevatedButton(
onPressed: () async {
pickDocument();
},
child: const Text('Pick Document'))
],
),
))),
);
}
void showDocument(BuildContext context) async {
final bytes = await DefaultAssetBundle.of(context).load(DOCUMENT_PATH);
final list = bytes.buffer.asUint8List();
final tempDir = await ComPDFKit.getTemporaryDirectory();
var pdfsDir = Directory('${tempDir.path}/pdfs');
pdfsDir.createSync(recursive: true);
final tempDocumentPath = '${tempDir.path}/$DOCUMENT_PATH';
final file = File(tempDocumentPath);
if (!file.existsSync()) {
file.create(recursive: true);
file.writeAsBytesSync(list);
}
var configuration = CPDFConfiguration(
toolbarConfig: const ToolbarConfig(androidAvailableActions: [
ToolbarAction.back,
ToolbarAction.thumbnail,
ToolbarAction.search,
ToolbarAction.bota,
ToolbarAction.menu,
]),
readerViewConfig: const ReaderViewConfig(pageSameWidth: true));
// How to disable functionality:
// setting the default display mode when opening
// configuration.modeConfig = const ModeConfig(initialViewMode: CPreviewMode.digitalSignatures);
// top toolbar configuration:
// android:
// configuration.toolbarConfig = const ToolbarConfig(androidAvailableActions: [
// ToolbarAction.thumbnail, ToolbarAction.bota,
// ToolbarAction.search, ToolbarAction.menu
// ],
// availableMenus: [
// ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
// ]);
// iOS:
// configuration.toolbarConfig = const ToolbarConfig(iosLeftBarAvailableActions: [
// ToolbarAction.back, ToolbarAction.thumbnail
// ],
// iosRightBarAvailableActions: [
// ToolbarAction.bota, ToolbarAction.search, ToolbarAction.menu
// ],
// availableMenus: [
// ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
// ]);
// readerview configuration
// configuration.readerViewConfig = const ReaderViewConfig(linkHighlight: true, formFieldHighlight: true);
ComPDFKit.openDocument(tempDocumentPath,
password: '', configuration: configuration);
}
}