123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /*
- * Copyright © 2014-2025 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/configuration/cpdf_configuration.dart';
- import 'package:compdfkit_flutter/configuration/cpdf_options.dart';
- import 'package:compdfkit_flutter/document/cpdf_document.dart';
- import 'package:compdfkit_flutter/widgets/cpdf_reader_widget.dart';
- import 'package:compdfkit_flutter_example/cpdf_reader_widget_security_example.dart';
- import 'package:compdfkit_flutter_example/examples.dart';
- import 'package:compdfkit_flutter_example/utils/file_util.dart';
- import 'package:flutter/material.dart';
- class CPDFDocumentOpenPDFExample extends StatefulWidget {
- const CPDFDocumentOpenPDFExample({super.key});
- @override
- State<CPDFDocumentOpenPDFExample> createState() => _CPDFDocumentExampleState();
- }
- class _CPDFDocumentExampleState extends State<CPDFDocumentOpenPDFExample> {
- TextEditingController _textEditingController = new TextEditingController();
- List<String> logs = List.empty(growable: true);
- late CPDFDocument document;
- @override
- void initState() {
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: const Text('Open Encrypt PDF Example'),
- ),
- body: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- TextButton(
- onPressed: () async {
- clearLogs();
- File encryptPDF = await extractAsset(context,shouldOverwrite: true,
- 'pdfs/Password_compdfkit_Security_Sample_File.pdf');
- applyLog('filePath:${encryptPDF.path}\n');
- document = await CPDFDocument.createInstance();
- var error = await document.open(encryptPDF.path, '');
- applyLog('open result:${error.name}\n');
- String? password = '';
- if (error == CPDFDocumentError.errorPassword) {
- applyLog('show input password dialog\n');
- password = await _showInputPasswordDialog();
- if (password == null) {
- applyLog('input password is empty;\n');
- return;
- }
- applyLog('password:${password}\n');
- error = await document.open(encryptPDF.path, password);
- }
- applyLog('open result:$error\n');
- if (error == CPDFDocumentError.success) {
- applyLog('go to CPDFReaderWidgetSecurityExample\n');
- goTo(
- CPDFReaderWidgetSecurityExample(
- documentPath: encryptPDF.path, password: password),
- context);
- }
- },
- child: const Text('Open Document')),
- Expanded(
- child: Container(
- margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
- padding: const EdgeInsets.all(8),
- width: double.infinity,
- decoration: const BoxDecoration(
- color: Colors.black12,
- borderRadius: BorderRadius.all(Radius.circular(8))),
- child: Text(
- logs.join(),
- style: const TextStyle(fontSize: 12),
- ),
- ))
- ],
- ),
- );
- }
- Future<String?> _showInputPasswordDialog() async {
- _textEditingController.clear();
- return await showDialog(
- context: context,
- builder: (context) {
- return AlertDialog(
- title: const Text('Input Password'),
- content: TextField(
- controller: _textEditingController,
- textInputAction: TextInputAction.done,
- decoration: const InputDecoration(hintText: 'compdfkit'),
- ),
- actions: [
- TextButton(
- onPressed: () {
- Navigator.pop(context, null);
- },
- child: const Text('Cancel')),
- TextButton(
- onPressed: () {
- Navigator.pop(context, _textEditingController.text);
- },
- child: const Text('OK'))
- ],
- );
- });
- }
- void applyLog(String msg) {
- setState(() {
- logs.add(msg);
- });
- }
- void clearLogs() {
- setState(() {
- logs.clear();
- });
- }
- @override
- void dispose() {
- super.dispose();
- _textEditingController.dispose();
- }
- }
|