cpdf_document_save_as_example.dart 3.9 KB

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