cpdf_reader_widget_controller_example.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/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:flutter/material.dart';
  14. class CPDFReaderWidgetControllerExample extends StatefulWidget {
  15. final String documentPath;
  16. const CPDFReaderWidgetControllerExample({super.key, required this.documentPath});
  17. @override
  18. State<CPDFReaderWidgetControllerExample> createState() =>
  19. _CPDFReaderWidgetControllerExampleState();
  20. }
  21. class _CPDFReaderWidgetControllerExampleState
  22. extends State<CPDFReaderWidgetControllerExample> {
  23. CPDFReaderWidgetController? _controller;
  24. bool canScale = true;
  25. bool pageSameWidth = true;
  26. bool isFixedScroll = false;
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. resizeToAvoidBottomInset: false,
  31. appBar: AppBar(
  32. title: const Text('CPDFReaderWidget Example'),
  33. leading: IconButton(
  34. onPressed: () {
  35. _save();
  36. Navigator.pop(context);
  37. },
  38. icon: const Icon(Icons.arrow_back)),
  39. actions: null == _controller
  40. ? []
  41. : [
  42. PopupMenuButton(onSelected: (value) {
  43. handleClick(value, _controller!);
  44. }, itemBuilder: (context) {
  45. return actions
  46. .map((e) => PopupMenuItem(value: e, child: Text(e)))
  47. .toList();
  48. })
  49. ],
  50. ),
  51. body: CPDFReaderWidget(
  52. document: widget.documentPath,
  53. configuration: CPDFConfiguration(toolbarConfig: const CPDFToolbarConfig(
  54. iosLeftBarAvailableActions: [CPDFToolbarAction.thumbnail]
  55. )),
  56. onCreated: (controller) {
  57. setState(() {
  58. _controller = controller;
  59. });
  60. },
  61. ));
  62. }
  63. void _save() async {
  64. bool saveResult = await _controller!.save();
  65. debugPrint('ComPDFKit-Flutter: saveResult:$saveResult');
  66. }
  67. void handleClick(String value, CPDFReaderWidgetController controller) async {
  68. switch (value) {
  69. case 'setScale':
  70. controller.setScale(1.5);
  71. break;
  72. case 'getScale':
  73. double scaleValue = await controller.getScale();
  74. debugPrint('ComPDFKit:CPDFReaderWidget-getScale():$scaleValue');
  75. break;
  76. case 'setCanScale':
  77. canScale = !canScale;
  78. await controller.setCanScale(canScale);
  79. break;
  80. case 'setFormHighlight':
  81. bool isFormFieldHighlight = await controller.isFormFieldHighlight();
  82. debugPrint('ComPDFKit:isFormFieldHighlight:$isFormFieldHighlight');
  83. controller.setFormFieldHighlight(!isFormFieldHighlight);
  84. break;
  85. case 'setLinkHighlight':
  86. bool isLinkHighlight = await controller.isLinkHighlight();
  87. debugPrint('ComPDFKit:isLinkHighlight:$isLinkHighlight');
  88. controller.setLinkHighlight(!isLinkHighlight);
  89. break;
  90. case 'setVerticalMode':
  91. bool isVerticalMode = await controller.isVerticalMode();
  92. debugPrint('ComPDFKit:isVerticalMode:$isVerticalMode');
  93. controller.setVerticalMode(!isVerticalMode);
  94. break;
  95. case 'setMargin':
  96. final Random random = Random();
  97. int value = random.nextInt(50);
  98. debugPrint('ComPDFKit:setMargin:$value');
  99. controller.setMargins(const CPDFEdgeInsets.symmetric(horizontal: 100, vertical: 10));
  100. break;
  101. case 'setContinueMode':
  102. bool isContinueMode = await controller.isContinueMode();
  103. debugPrint('ComPDFKit:isContinueMode:$isContinueMode');
  104. controller.setContinueMode(!isContinueMode);
  105. break;
  106. case 'setDoublePageMode':
  107. bool isDoublePageMode = await controller.isDoublePageMode();
  108. debugPrint('ComPDFKit:isDoublePageMode:$isDoublePageMode');
  109. controller.setDoublePageMode(!isDoublePageMode);
  110. break;
  111. case 'setCropMode':
  112. bool isCropMode = await controller.isCropMode();
  113. debugPrint('ComPDFKit:isCropMode:$isCropMode');
  114. controller.setCropMode(!isCropMode);
  115. break;
  116. case 'setDisplayPageIndex':
  117. int nextPageIndex = await controller.getCurrentPageIndex() + 1;
  118. controller.setDisplayPageIndex(nextPageIndex, animated: false);
  119. break;
  120. case 'getCurrentPageIndex':
  121. debugPrint('ComPDFKit:getCurrentPageIndex:${await controller.getCurrentPageIndex()}');
  122. break;
  123. case 'setCoverPageMode':
  124. bool isCoverPageMode = await controller.isCoverPageMode();
  125. debugPrint('ComPDFKit:isCoverPageMode:$isCoverPageMode');
  126. controller.setCoverPageMode(!isCoverPageMode);
  127. break;
  128. case 'pageSameWidth':
  129. pageSameWidth = !pageSameWidth;
  130. await controller.setPageSameWidth(pageSameWidth);
  131. break;
  132. case 'isPageInScreen':
  133. bool isPageInScreen = await controller.isPageInScreen(0);
  134. debugPrint('ComPDFKit:first page is in screen:$isPageInScreen');
  135. break;
  136. case 'setFixedScroll':
  137. isFixedScroll = !isFixedScroll;
  138. await controller.setFixedScroll(isFixedScroll);
  139. break;
  140. case 'isChanged':
  141. bool hasChange = await controller.hasChange();
  142. debugPrint('ComPDFKit:hasChange:$hasChange');
  143. break;
  144. }
  145. }
  146. }
  147. var actions = [
  148. 'setScale',
  149. 'getScale',
  150. if(Platform.isAndroid) ...[
  151. 'setCanScale',
  152. 'pageSameWidth',
  153. 'isPageInScreen',
  154. 'setFixedScroll'
  155. ],
  156. 'setFormHighlight',
  157. 'setLinkHighlight',
  158. 'setVerticalMode',
  159. 'setMargin',
  160. 'setContinueMode',
  161. 'setDoublePageMode',
  162. 'setCropMode',
  163. 'setDisplayPageIndex',
  164. 'getCurrentPageIndex',
  165. 'setCoverPageMode',
  166. 'isChanged'
  167. ];
  168. Color randomColor() {
  169. final Random random = Random();
  170. return Color.fromARGB(
  171. 255, // Alpha value (fully opaque)
  172. random.nextInt(256), // Red value
  173. random.nextInt(256), // Green value
  174. random.nextInt(256), // Blue value
  175. );
  176. }