cpdf_reader_widget_annotations_example.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright © 2014-2025 PDF Technologies, Inc. All Rights Reserved.
  2. //
  3. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  4. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  5. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  6. // This notice may not be removed from this file.
  7. import 'dart:io';
  8. import 'dart:math';
  9. import 'package:compdfkit_flutter/configuration/cpdf_configuration.dart';
  10. import 'package:compdfkit_flutter/configuration/cpdf_options.dart';
  11. import 'package:compdfkit_flutter/widgets/cpdf_reader_widget.dart';
  12. import 'package:compdfkit_flutter/widgets/cpdf_reader_widget_controller.dart';
  13. import 'package:compdfkit_flutter_example/utils/file_util.dart';
  14. import 'package:file_picker/file_picker.dart';
  15. import 'package:flutter/material.dart';
  16. class CPDFReaderWidgetAnnotationsExample extends StatefulWidget {
  17. final String documentPath;
  18. const CPDFReaderWidgetAnnotationsExample(
  19. {super.key, required this.documentPath});
  20. @override
  21. State<CPDFReaderWidgetAnnotationsExample> createState() =>
  22. _CPDFReaderWidgetAnnotationsExampleState();
  23. }
  24. class _CPDFReaderWidgetAnnotationsExampleState
  25. extends State<CPDFReaderWidgetAnnotationsExample> {
  26. CPDFReaderWidgetController? _controller;
  27. bool pageSameWidth = true;
  28. bool isFixedScroll = false;
  29. @override
  30. Widget build(BuildContext context) {
  31. return Scaffold(
  32. resizeToAvoidBottomInset: false,
  33. appBar: AppBar(
  34. title: const Text('CPDFReaderWidget Example'),
  35. leading: IconButton(
  36. onPressed: () {
  37. _save();
  38. Navigator.pop(context);
  39. },
  40. icon: const Icon(Icons.arrow_back)),
  41. actions: null == _controller ? null : _buildAppBarActions(context),
  42. ),
  43. body: CPDFReaderWidget(
  44. document: widget.documentPath,
  45. configuration: CPDFConfiguration(
  46. toolbarConfig: const CPDFToolbarConfig(
  47. iosLeftBarAvailableActions: [CPDFToolbarAction.thumbnail])),
  48. onCreated: (controller) {
  49. setState(() {
  50. _controller = controller;
  51. });
  52. },
  53. ));
  54. }
  55. void _save() async {
  56. bool saveResult = await _controller!.document.save();
  57. debugPrint('ComPDFKit-Flutter: saveResult:$saveResult');
  58. }
  59. List<Widget> _buildAppBarActions(BuildContext context) {
  60. return [
  61. PopupMenuButton<String>(
  62. onSelected: (value) {
  63. handleClick(value, _controller!);
  64. },
  65. itemBuilder: (context) {
  66. return actions.map((action) {
  67. return PopupMenuItem(
  68. value: action,
  69. child: Text(action),
  70. );
  71. }).toList();
  72. },
  73. ),
  74. ];
  75. }
  76. void handleClick(String value, CPDFReaderWidgetController controller) async {
  77. switch (value) {
  78. case 'Save':
  79. bool saveResult = await controller.document.save();
  80. debugPrint('ComPDFKit: save():$saveResult');
  81. break;
  82. case "Import Annotations 1":
  83. FilePickerResult? result = await FilePicker.platform.pickFiles(type: FileType.any);
  84. if(result != null) {
  85. debugPrint('mimeType: ${result.files.first.extension}');
  86. if(result.files.first.extension != 'xfdf') {
  87. debugPrint('Please select a xfdf file.');
  88. return;
  89. }
  90. bool importResult = await controller.document.importAnnotations(result.files.first.path!);
  91. debugPrint('ComPDFKit:Document: importAnnotations:$importResult');
  92. }
  93. break;
  94. case "Import Annotations 2":
  95. // android assets:
  96. // String? xfdfFile = "file:///android_asset/test.xfdf";
  97. // android file path sample:
  98. File xfdfFile = await extractAsset(context, 'pdfs/test.xfdf');
  99. // android Uri:
  100. //String xfdfFile = "content://xxx";
  101. bool result =
  102. await controller.document.importAnnotations(xfdfFile.path);
  103. debugPrint('ComPDFKit:Document: importAnnotations:$result');
  104. break;
  105. case "Export Annotations":
  106. String xfdfPath = await controller.document.exportAnnotations();
  107. debugPrint('ComPDFKit:Document: exportAnnotations:$xfdfPath');
  108. break;
  109. case "Remove All Annotations":
  110. await controller.document.removeAllAnnotations();
  111. break;
  112. }
  113. }
  114. }
  115. var actions = [
  116. 'Save',
  117. 'Import Annotations 1',
  118. 'Import Annotations 2',
  119. 'Export Annotations',
  120. 'Remove All Annotations',
  121. ];
  122. Color randomColor() {
  123. final Random random = Random();
  124. return Color.fromARGB(
  125. 255, // Alpha value (fully opaque)
  126. random.nextInt(256), // Red value
  127. random.nextInt(256), // Green value
  128. random.nextInt(256), // Blue value
  129. );
  130. }