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/configuration/cpdf_options.dart';
  12. import 'package:compdfkit_flutter/document/cpdf_document.dart';
  13. import 'package:compdfkit_flutter_example/cpdf_reader_widget_security_example.dart';
  14. import 'package:compdfkit_flutter_example/examples.dart';
  15. import 'package:compdfkit_flutter_example/utils/file_util.dart';
  16. import 'package:flutter/material.dart';
  17. class CPDFDocumentOpenPDFExample extends StatefulWidget {
  18. const CPDFDocumentOpenPDFExample({super.key});
  19. @override
  20. State<CPDFDocumentOpenPDFExample> createState() => _CPDFDocumentExampleState();
  21. }
  22. class _CPDFDocumentExampleState extends State<CPDFDocumentOpenPDFExample> {
  23. final TextEditingController _textEditingController = TextEditingController();
  24. List<String> logs = List.empty(growable: true);
  25. late CPDFDocument document;
  26. @override
  27. void initState() {
  28. super.initState();
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. return Scaffold(
  33. appBar: AppBar(
  34. title: const Text('Open Encrypt PDF Example'),
  35. ),
  36. body: Column(
  37. crossAxisAlignment: CrossAxisAlignment.start,
  38. children: [
  39. TextButton(
  40. onPressed: () async {
  41. clearLogs();
  42. File encryptPDF = await extractAsset(context,shouldOverwrite: true,
  43. 'pdfs/Password_compdfkit_Security_Sample_File.pdf');
  44. applyLog('filePath:${encryptPDF.path}\n');
  45. document = await CPDFDocument.createInstance();
  46. var error = await document.open(encryptPDF.path, '');
  47. applyLog('open result:${error.name}\n');
  48. String? password = '';
  49. if (error == CPDFDocumentError.errorPassword) {
  50. applyLog('show input password dialog\n');
  51. password = await _showInputPasswordDialog();
  52. if (password == null) {
  53. applyLog('input password is empty;\n');
  54. return;
  55. }
  56. applyLog('password:$password\n');
  57. error = await document.open(encryptPDF.path, password);
  58. }
  59. applyLog('open result:$error\n');
  60. if (error == CPDFDocumentError.success) {
  61. applyLog('go to CPDFReaderWidgetSecurityExample\n');
  62. if(context.mounted){
  63. goTo(
  64. CPDFReaderWidgetSecurityExample(
  65. documentPath: encryptPDF.path, password: password),
  66. context);
  67. }
  68. }
  69. },
  70. child: const Text('Open Document')),
  71. const Padding(padding: EdgeInsets.only(left: 12), child: Text('Password: compdfkit')),
  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', hintStyle: TextStyle(color: Colors.black12)),
  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. }