cpdf_reader_widget_controller_example.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright © 2014-2024 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/compdfkit.dart';
  10. import 'package:compdfkit_flutter/configuration/cpdf_configuration.dart';
  11. import 'package:compdfkit_flutter/configuration/cpdf_options.dart';
  12. import 'package:compdfkit_flutter/util/extension/cpdf_color_extension.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/utils/file_util.dart';
  16. import 'package:file_picker/file_picker.dart';
  17. import 'package:flutter/material.dart';
  18. class CPDFReaderWidgetControllerExample extends StatefulWidget {
  19. final String documentPath;
  20. const CPDFReaderWidgetControllerExample({super.key, required this.documentPath});
  21. @override
  22. State<CPDFReaderWidgetControllerExample> createState() =>
  23. _CPDFReaderWidgetControllerExampleState();
  24. }
  25. class _CPDFReaderWidgetControllerExampleState
  26. extends State<CPDFReaderWidgetControllerExample> {
  27. CPDFReaderWidgetController? _controller;
  28. bool canScale = true;
  29. bool pageSameWidth = true;
  30. bool isFixedScroll = false;
  31. @override
  32. Widget build(BuildContext context) {
  33. return Scaffold(
  34. resizeToAvoidBottomInset: false,
  35. appBar: AppBar(
  36. title: const Text('CPDFReaderWidget Example'),
  37. leading: IconButton(
  38. onPressed: () {
  39. _save();
  40. Navigator.pop(context);
  41. },
  42. icon: const Icon(Icons.arrow_back)),
  43. actions: null == _controller
  44. ? []
  45. : [
  46. PopupMenuButton(onSelected: (value) {
  47. handleClick(value, _controller!);
  48. }, itemBuilder: (context) {
  49. return actions
  50. .map((e) => PopupMenuItem(value: e, child: Text(e)))
  51. .toList();
  52. })
  53. ],
  54. ),
  55. body: CPDFReaderWidget(
  56. document: widget.documentPath,
  57. configuration: CPDFConfiguration(toolbarConfig: const CPDFToolbarConfig(
  58. iosLeftBarAvailableActions: [CPDFToolbarAction.thumbnail]
  59. )),
  60. onCreated: (controller) {
  61. setState(() {
  62. _controller = controller;
  63. });
  64. },
  65. onPageChanged: (pageIndex){
  66. debugPrint('pageIndex:${pageIndex}');
  67. },
  68. onSaveCallback: (){
  69. debugPrint('CPDFDocument: save success');
  70. },
  71. ));
  72. }
  73. void _save() async {
  74. bool saveResult = await _controller!.save();
  75. debugPrint('ComPDFKit-Flutter: saveResult:$saveResult');
  76. }
  77. void handleClick(String value, CPDFReaderWidgetController controller) async {
  78. switch (value) {
  79. case 'save':
  80. await controller.save();
  81. break;
  82. case 'setScale':
  83. controller.setScale(1.5);
  84. break;
  85. case 'getScale':
  86. double scaleValue = await controller.getScale();
  87. debugPrint('ComPDFKit:CPDFReaderWidget-getScale():$scaleValue');
  88. break;
  89. case 'setCanScale':
  90. canScale = !canScale;
  91. await controller.setCanScale(canScale);
  92. break;
  93. case 'setFormHighlight':
  94. bool isFormFieldHighlight = await controller.isFormFieldHighlight();
  95. debugPrint('ComPDFKit:isFormFieldHighlight:$isFormFieldHighlight');
  96. controller.setFormFieldHighlight(!isFormFieldHighlight);
  97. break;
  98. case 'setLinkHighlight':
  99. bool isLinkHighlight = await controller.isLinkHighlight();
  100. debugPrint('ComPDFKit:isLinkHighlight:$isLinkHighlight');
  101. controller.setLinkHighlight(!isLinkHighlight);
  102. break;
  103. case 'setVerticalMode':
  104. bool isVerticalMode = await controller.isVerticalMode();
  105. debugPrint('ComPDFKit:isVerticalMode:$isVerticalMode');
  106. controller.setVerticalMode(!isVerticalMode);
  107. break;
  108. case 'setMargin':
  109. final Random random = Random();
  110. int value = random.nextInt(50);
  111. debugPrint('ComPDFKit:setMargin:$value');
  112. controller.setMargins(const CPDFEdgeInsets.only(left: 10, top:10, right: 10, bottom: 10));
  113. break;
  114. case "setPageSpacing":
  115. await controller.setPageSpacing(20);
  116. break;
  117. case 'setContinueMode':
  118. bool isContinueMode = await controller.isContinueMode();
  119. debugPrint('ComPDFKit:isContinueMode:$isContinueMode');
  120. controller.setContinueMode(!isContinueMode);
  121. break;
  122. case 'setDoublePageMode':
  123. bool isDoublePageMode = await controller.isDoublePageMode();
  124. debugPrint('ComPDFKit:isDoublePageMode:$isDoublePageMode');
  125. controller.setDoublePageMode(!isDoublePageMode);
  126. break;
  127. case 'setCropMode':
  128. bool isCropMode = await controller.isCropMode();
  129. debugPrint('ComPDFKit:isCropMode:$isCropMode');
  130. controller.setCropMode(!isCropMode);
  131. break;
  132. case 'setDisplayPageIndex':
  133. int nextPageIndex = await controller.getCurrentPageIndex() + 1;
  134. controller.setDisplayPageIndex(nextPageIndex, animated: false);
  135. break;
  136. case 'getCurrentPageIndex':
  137. debugPrint('ComPDFKit:getCurrentPageIndex:${await controller.getCurrentPageIndex()}');
  138. break;
  139. case 'setCoverPageMode':
  140. bool isCoverPageMode = await controller.isCoverPageMode();
  141. debugPrint('ComPDFKit:isCoverPageMode:$isCoverPageMode');
  142. controller.setCoverPageMode(!isCoverPageMode);
  143. break;
  144. case 'pageSameWidth':
  145. pageSameWidth = !pageSameWidth;
  146. await controller.setPageSameWidth(pageSameWidth);
  147. break;
  148. case 'isPageInScreen':
  149. bool isPageInScreen = await controller.isPageInScreen(0);
  150. debugPrint('ComPDFKit:first page is in screen:$isPageInScreen');
  151. break;
  152. case 'setFixedScroll':
  153. isFixedScroll = !isFixedScroll;
  154. await controller.setFixedScroll(isFixedScroll);
  155. break;
  156. case 'setReadBackgroundColor':
  157. var currentReadBackgroundColor = await controller.getReadBackgroundColor();
  158. debugPrint('readBackgroundColor:${currentReadBackgroundColor.toHex()}');
  159. await controller.setReadBackgroundColor(theme: CPDFThemes.light);
  160. break;
  161. case 'isChanged':
  162. bool hasChange = await controller.hasChange();
  163. debugPrint('ComPDFKit:hasChange:$hasChange');
  164. break;
  165. case "documentInfo":
  166. var document = controller.document;
  167. debugPrint('ComPDFKit:Document: fileName:${await document.getFileName()}');
  168. debugPrint('ComPDFKit:Document: checkOwnerUnlocked:${await document.checkOwnerUnlocked()}');
  169. debugPrint('ComPDFKit:Document: hasChange:${await document.hasChange()}');
  170. debugPrint('ComPDFKit:Document: isEncrypted:${await document.isEncrypted()}');
  171. debugPrint('ComPDFKit:Document: isImageDoc:${await document.isImageDoc()}');
  172. debugPrint('ComPDFKit:Document: getPermissions:${await document.getPermissions()}');
  173. debugPrint('ComPDFKit:Document: getPageCount:${await document.getPageCount()}');
  174. break;
  175. case "openDocument":
  176. FilePickerResult? result = await FilePicker.platform.pickFiles(
  177. type: FileType.custom,
  178. allowedExtensions: ['pdf'],
  179. );
  180. if (result != null) {
  181. var document = controller.document;
  182. document.open(result.files.first.path!, "");
  183. }
  184. break;
  185. case "importAnnotations":
  186. File xfdfFile = await extractAsset(context, 'pdfs/test.xfdf');
  187. bool result = await controller.document.importAnnotations(xfdfFile.path);
  188. debugPrint('ComPDFKit:Document: importAnnotations:$result');
  189. break;
  190. case "exportAnnotations":
  191. String xfdfPath = await controller.document.exportAnnotations();
  192. debugPrint('ComPDFKit:Document: exportAnnotations:$xfdfPath');
  193. break;
  194. case "removeAllAnnotations":
  195. await controller.document.removeAllAnnotations();
  196. break;
  197. case "removeSignFileList":
  198. bool result = await ComPDFKit.removeSignFileList();
  199. debugPrint('ComPDFKit:removeSignFileList:${result}');
  200. break;
  201. }
  202. }
  203. }
  204. var actions = [
  205. 'save',
  206. 'setScale',
  207. 'getScale',
  208. if(Platform.isAndroid) ...[
  209. 'setCanScale',
  210. 'pageSameWidth',
  211. 'isPageInScreen',
  212. 'setFixedScroll',
  213. 'setReadBackgroundColor',
  214. 'setPageSpacing'
  215. ],
  216. 'setFormHighlight',
  217. 'setLinkHighlight',
  218. 'setVerticalMode',
  219. 'setMargin',
  220. 'setContinueMode',
  221. 'setDoublePageMode',
  222. 'setCropMode',
  223. 'setDisplayPageIndex',
  224. 'getCurrentPageIndex',
  225. 'setCoverPageMode',
  226. 'isChanged',
  227. 'documentInfo',
  228. 'openDocument',
  229. 'importAnnotations',
  230. 'exportAnnotations',
  231. 'removeAllAnnotations',
  232. 'removeSignFileList'
  233. ];
  234. Color randomColor() {
  235. final Random random = Random();
  236. return Color.fromARGB(
  237. 255, // Alpha value (fully opaque)
  238. random.nextInt(256), // Red value
  239. random.nextInt(256), // Green value
  240. random.nextInt(256), // Blue value
  241. );
  242. }