cpdf_document_save_as_example.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/widgets/cpdf_reader_widget_controller.dart';
  17. import 'package:compdfkit_flutter_example/cpdf_reader_widget_example.dart';
  18. import 'package:compdfkit_flutter_example/cpdf_reader_widget_security_example.dart';
  19. import 'package:compdfkit_flutter_example/examples.dart';
  20. import 'package:compdfkit_flutter_example/utils/file_util.dart';
  21. import 'package:flutter/material.dart';
  22. class CPDFSaveAsExample extends StatefulWidget {
  23. final String document;
  24. const CPDFSaveAsExample({super.key, required this.document});
  25. @override
  26. State<CPDFSaveAsExample> createState() => _CPDFSaveAsExampleState();
  27. }
  28. class _CPDFSaveAsExampleState extends State<CPDFSaveAsExample> {
  29. late CPDFReaderWidgetController controller;
  30. @override
  31. void initState() {
  32. super.initState();
  33. }
  34. void initPDFPath() async {}
  35. @override
  36. Widget build(BuildContext context) {
  37. return Scaffold(
  38. appBar: AppBar(
  39. title: const Text('Save As issues'),
  40. actions: [
  41. IconButton(
  42. onPressed: () async {
  43. // await controller.document.save();
  44. final tempDir = await ComPDFKit.getTemporaryDirectory();
  45. String savePath = '${tempDir.path}/test_2.pdf';
  46. bool saveResult = await controller.document.saveAs(savePath);
  47. if (saveResult) {
  48. var jump = await _showSaveResultDialog(savePath);
  49. if (jump != null) {
  50. goTo(CPDFReaderWidgetExample(documentPath: jump), context);
  51. }
  52. }
  53. },
  54. icon: const Icon(Icons.download)),
  55. IconButton(
  56. onPressed: () async {
  57. final tempDir = await ComPDFKit.getTemporaryDirectory();
  58. String savePath = '${tempDir.path}/test_2.pdf';
  59. final file = File(savePath);
  60. if(await file.exists()){
  61. await file.delete();
  62. }
  63. // File xfdfFile = await extractAsset(context, 'pdfs/test.xfdf');
  64. //
  65. // // android Uri:
  66. // //String xfdfFile = "content://xxx";
  67. //
  68. // bool result = await controller.document
  69. // .importAnnotations(xfdfFile.path);
  70. // debugPrint('ComPDFKit:Document: importAnnotations:$result');
  71. },
  72. icon: Icon(Icons.settings_ethernet_rounded)),
  73. ],
  74. ),
  75. body: CPDFReaderWidget(
  76. document: widget.document,
  77. configuration: CPDFConfiguration(),
  78. onCreated: (constroller) {
  79. setState(() {
  80. this.controller = constroller;
  81. });
  82. }));
  83. }
  84. Future<String?> _showSaveResultDialog(String path) async {
  85. return await showDialog(
  86. context: context,
  87. builder: (context) {
  88. return AlertDialog(
  89. title: const Text('Save Result'),
  90. content: Text('Save Path:${path}'),
  91. actions: [
  92. TextButton(
  93. onPressed: () {
  94. Navigator.pop(context, null);
  95. },
  96. child: const Text('Cancel')),
  97. TextButton(
  98. onPressed: () {
  99. Navigator.pop(context, path);
  100. },
  101. child: const Text('Jump'))
  102. ],
  103. );
  104. });
  105. }
  106. }