cpdf_document_open_pdf_example.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright © 2014-2025 PDF Technologies, Inc. All Rights Reserved.
  3. *
  4. * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. * This notice may not be removed from this file.
  8. *
  9. */
  10. import 'dart:io';
  11. import 'package:compdfkit_flutter/compdfkit.dart';
  12. import 'package:compdfkit_flutter/configuration/cpdf_configuration.dart';
  13. import 'package:compdfkit_flutter/configuration/cpdf_options.dart';
  14. import 'package:compdfkit_flutter/document/cpdf_document.dart';
  15. import 'package:compdfkit_flutter/widgets/cpdf_reader_widget.dart';
  16. import 'package:compdfkit_flutter_example/cpdf_reader_widget_security_example.dart';
  17. import 'package:compdfkit_flutter_example/examples.dart';
  18. import 'package:compdfkit_flutter_example/utils/file_util.dart';
  19. import 'package:flutter/material.dart';
  20. class CPDFDocumentOpenPDFExample extends StatefulWidget {
  21. const CPDFDocumentOpenPDFExample({super.key});
  22. @override
  23. State<CPDFDocumentOpenPDFExample> createState() => _CPDFDocumentExampleState();
  24. }
  25. class _CPDFDocumentExampleState extends State<CPDFDocumentOpenPDFExample> {
  26. TextEditingController _textEditingController = new TextEditingController();
  27. List<String> logs = List.empty(growable: true);
  28. late CPDFDocument document;
  29. @override
  30. void initState() {
  31. super.initState();
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return Scaffold(
  36. appBar: AppBar(
  37. title: const Text('Open Encrypt PDF Example'),
  38. ),
  39. body: Column(
  40. crossAxisAlignment: CrossAxisAlignment.start,
  41. children: [
  42. TextButton(
  43. onPressed: () async {
  44. clearLogs();
  45. File encryptPDF = await extractAsset(context,shouldOverwrite: true,
  46. 'pdfs/Password_compdfkit_Security_Sample_File.pdf');
  47. applyLog('filePath:${encryptPDF.path}\n');
  48. document = await CPDFDocument.createInstance();
  49. var error = await document.open(encryptPDF.path, '');
  50. applyLog('open result:${error.name}\n');
  51. String? password = '';
  52. if (error == CPDFDocumentError.errorPassword) {
  53. applyLog('show input password dialog\n');
  54. password = await _showInputPasswordDialog();
  55. if (password == null) {
  56. applyLog('input password is empty;\n');
  57. return;
  58. }
  59. applyLog('password:${password}\n');
  60. error = await document.open(encryptPDF.path, password);
  61. }
  62. applyLog('open result:$error\n');
  63. if (error == CPDFDocumentError.success) {
  64. applyLog('go to CPDFReaderWidgetSecurityExample\n');
  65. goTo(
  66. CPDFReaderWidgetSecurityExample(
  67. documentPath: encryptPDF.path, password: password),
  68. context);
  69. }
  70. },
  71. child: const Text('Open Document')),
  72. Expanded(
  73. child: Container(
  74. margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
  75. padding: const EdgeInsets.all(8),
  76. width: double.infinity,
  77. decoration: const BoxDecoration(
  78. color: Colors.black12,
  79. borderRadius: BorderRadius.all(Radius.circular(8))),
  80. child: Text(
  81. logs.join(),
  82. style: const TextStyle(fontSize: 12),
  83. ),
  84. ))
  85. ],
  86. ),
  87. );
  88. }
  89. Future<String?> _showInputPasswordDialog() async {
  90. _textEditingController.clear();
  91. return await showDialog(
  92. context: context,
  93. builder: (context) {
  94. return AlertDialog(
  95. title: const Text('Input Password'),
  96. content: TextField(
  97. controller: _textEditingController,
  98. textInputAction: TextInputAction.done,
  99. decoration: const InputDecoration(hintText: 'compdfkit'),
  100. ),
  101. actions: [
  102. TextButton(
  103. onPressed: () {
  104. Navigator.pop(context, null);
  105. },
  106. child: const Text('Cancel')),
  107. TextButton(
  108. onPressed: () {
  109. Navigator.pop(context, _textEditingController.text);
  110. },
  111. child: const Text('OK'))
  112. ],
  113. );
  114. });
  115. }
  116. void applyLog(String msg) {
  117. setState(() {
  118. logs.add(msg);
  119. });
  120. }
  121. void clearLogs() {
  122. setState(() {
  123. logs.clear();
  124. });
  125. }
  126. @override
  127. void dispose() {
  128. super.dispose();
  129. _textEditingController.dispose();
  130. }
  131. }