cpdf_reader_widget_controller.dart 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 'package:flutter/services.dart';
  9. import '../configuration/cpdf_options.dart';
  10. /// PDF Reader Widget Controller
  11. ///
  12. /// Examples:
  13. /// ```dart
  14. /// Scaffold(
  15. /// resizeToAvoidBottomInset: false,
  16. /// appBar: AppBar(title: const Text('CPDFReaderWidget Example')),
  17. /// body: CPDFReaderWidget(
  18. /// document: widget.documentPath,
  19. /// configuration: CPDFConfiguration(),
  20. /// onCreated: (controller) {
  21. /// setState(() {
  22. /// _controller = controller;
  23. /// });
  24. /// },
  25. /// ));
  26. /// ```
  27. class CPDFReaderWidgetController {
  28. late MethodChannel _channel;
  29. // late CPDFDocument _document;
  30. CPDFReaderWidgetController(int id) {
  31. _channel = MethodChannel('com.compdfkit.flutter.ui.pdfviewer.$id');
  32. _channel.setMethodCallHandler((call) async {});
  33. // _document = CPDFDocument.withController(id);
  34. }
  35. // CPDFDocument get document => _document;
  36. /// Save document
  37. /// Return value: **true** if the save is successful,
  38. /// **false** if the save fails.
  39. Future<bool> save() async {
  40. return await _channel.invokeMethod('save');
  41. }
  42. /// Set the page scale
  43. /// Value Range: 1.0~5.0
  44. ///
  45. /// example:
  46. /// ```dart
  47. /// _controller.setScale(1.5);
  48. /// ```
  49. Future<void> setScale(double scale) async {
  50. await _channel.invokeMethod('set_scale', scale);
  51. }
  52. /// Get the page scale
  53. ///
  54. /// example:
  55. /// ```dart
  56. /// double scaleValue = await _controller.getScale();
  57. /// ```
  58. Future<double> getScale() async {
  59. return await _channel.invokeMethod('get_scale');
  60. }
  61. /// Whether allow to scale.
  62. /// Default : true
  63. ///
  64. /// example:
  65. /// ```dart
  66. /// _controller.setCanScale(canScale);
  67. /// ```
  68. Future<void> setCanScale(bool canScale) async {
  69. assert(Platform.isAndroid, 'This method is only supported on Android');
  70. await _channel.invokeMethod('set_can_scale', canScale);
  71. }
  72. /// Sets background color of reader.
  73. /// The color of each document space will be set to 75% of [color] transparency
  74. ///
  75. /// example:
  76. /// ```dart
  77. /// await _controller.setReadBackgroundColor(Colors.white);
  78. /// ```
  79. // Future<void> setReadBackgroundColor(Color color) async {
  80. // await _channel.invokeMethod('set_read_background_color', color.toHex());
  81. // }
  82. /// Get background color of reader.
  83. ///
  84. /// example:
  85. /// ```dart
  86. /// Color color = await _controller.getReadBackgroundColor();
  87. /// ```
  88. // Future<Color> getReadBackgroundColor() async {
  89. // String hexColor = await _channel.invokeMethod('get_read_background_color');
  90. // return HexColor.fromHex(hexColor);
  91. // }
  92. /// Sets whether to display highlight Form Field.
  93. /// [isFormFieldHighlight] : true to display highlight Form Field.
  94. ///
  95. /// example:
  96. /// ```dart
  97. /// _controller.setFormFieldHighlight(true);
  98. /// ```
  99. Future<void> setFormFieldHighlight(bool isFormFieldHighlight) async {
  100. await _channel.invokeMethod(
  101. 'set_form_field_highlight', isFormFieldHighlight);
  102. }
  103. /// Whether to display highlight Form Field.
  104. ///
  105. /// example:
  106. /// ```dart
  107. /// bool isFormFieldHighlight = await _controller.isFormFieldHighlight();
  108. /// ```
  109. Future<bool> isFormFieldHighlight() async {
  110. return await _channel.invokeMethod('is_form_field_highlight');
  111. }
  112. /// Sets whether to display highlight Link.
  113. ///
  114. /// [isLinkHighlight] : Whether to highlight Link.
  115. ///
  116. /// example:
  117. /// ```dart
  118. /// _controller.setLinkHighlight(true);
  119. /// ```
  120. Future<void> setLinkHighlight(bool isLinkHighlight) async {
  121. await _channel.invokeMethod('set_link_highlight', isLinkHighlight);
  122. }
  123. /// Whether to display highlight Link.
  124. ///
  125. /// example:
  126. /// ```dart
  127. /// bool isLinkHighlight = await _controller.isLinkHighlight();
  128. /// ```
  129. Future<bool> isLinkHighlight() async {
  130. return await _channel.invokeMethod('is_link_highlight');
  131. }
  132. /// Sets whether it is vertical scroll mode.
  133. ///
  134. /// [isVerticalMode] : Whether it is vertical scroll mode.
  135. ///
  136. /// example:
  137. /// ```dart
  138. /// _controller.setVerticalMode(true);
  139. /// ```
  140. Future<void> setVerticalMode(bool isVerticalMode) async {
  141. await _channel.invokeMethod('set_vertical_mode', isVerticalMode);
  142. }
  143. /// Whether it is vertical scroll mode.
  144. ///
  145. /// example:
  146. /// ```dart
  147. /// bool isVerticalMode = await _controller.isVerticalMode();
  148. /// ```
  149. Future<bool> isVerticalMode() async {
  150. return await _channel.invokeMethod('is_vertical_mode');
  151. }
  152. /// Set document page spacing
  153. ///
  154. /// [spacing] the spacing between pages
  155. ///
  156. /// example:
  157. /// ```dart
  158. /// _controller.setMargins(const CPDFEdgeInsets.symmetric(horizontal: 10, vertical: 10));
  159. /// ```
  160. Future<void> setMargins(CPDFEdgeInsets edgeInsets) async {
  161. await _channel.invokeMethod('set_margin' , edgeInsets.toJson());
  162. }
  163. /// Sets whether it is continuous scroll mode.
  164. ///
  165. /// [isContinueMode] Whether it is continuous scroll mode.
  166. ///
  167. /// example:
  168. /// ```dart
  169. /// _controller.setContinueMode(true);
  170. /// ```
  171. Future<void> setContinueMode(bool isContinueMode) async {
  172. await _channel.invokeMethod('set_continue_mode', isContinueMode);
  173. }
  174. /// Whether it is continuous scroll mode.
  175. ///
  176. /// example:
  177. /// ```dart
  178. /// bool isContinueMode = await _controller.isContinueMode();
  179. /// ```
  180. Future<bool> isContinueMode() async {
  181. return await _channel.invokeMethod('is_continue_mode');
  182. }
  183. /// Sets whether it is double page mode.
  184. ///
  185. /// [isDoublePageMode] Whether it is double page mode.
  186. ///
  187. /// example:
  188. /// ```dart
  189. /// _controller.setDoublePageMode(true);
  190. /// ```
  191. Future<void> setDoublePageMode(bool isDoublePageMode) async {
  192. await _channel.invokeMethod('set_double_page_mode', isDoublePageMode);
  193. }
  194. /// Whether it is double page mode.
  195. ///is_
  196. /// example:
  197. /// ```dart
  198. /// bool isDoublePageMode = await _controller.isDoublePageMode();
  199. /// ```
  200. Future<bool> isDoublePageMode() async {
  201. return await _channel.invokeMethod('is_double_page_mode');
  202. }
  203. /// Sets whether it is cover page mode.
  204. ///
  205. /// example:
  206. /// ```dart
  207. /// _controller.setCoverPageMode(true);
  208. /// ```
  209. Future<void> setCoverPageMode(bool coverPageMode) async {
  210. await _channel.invokeMethod('set_cover_page_mode', coverPageMode);
  211. }
  212. /// Whether it is cover page mode.
  213. ///
  214. /// example:
  215. /// ```dart
  216. /// bool isCoverPageMode = await _controller.isCoverPageMode();
  217. /// ```
  218. Future<bool> isCoverPageMode() async {
  219. return await _channel.invokeMethod('is_cover_page_mode');
  220. }
  221. /// Sets whether it is crop mode.
  222. ///
  223. /// [cropMode] Whether it is crop mode.
  224. ///
  225. /// example:
  226. /// ```dart
  227. /// _controller.setCropMode(true);
  228. /// ```
  229. Future<void> setCropMode(bool isCropMode) async {
  230. await _channel.invokeMethod('set_crop_mode', isCropMode);
  231. }
  232. /// Whether it is crop mode.
  233. ///
  234. /// example:
  235. /// ```dart
  236. /// bool isCropMode = await _controller.isCropMode();
  237. /// ```
  238. Future<bool> isCropMode() async {
  239. return await _channel.invokeMethod('is_crop_mode');
  240. }
  241. /// Jump to the index page.
  242. ///
  243. /// [pageIndex] The index of the page to jump.
  244. /// [animated] only for iOS, whether to use animation when jumping.
  245. ///
  246. /// example:
  247. /// ```dart
  248. /// _controller.setDisplayPageIndex(1, animated: true);
  249. /// ```
  250. Future<void> setDisplayPageIndex(int pageIndex,
  251. {bool animated = true}) async {
  252. await _channel.invokeMethod('set_display_page_index',
  253. {'pageIndex': pageIndex, 'animated': animated});
  254. }
  255. /// get current page index
  256. ///
  257. /// example:
  258. /// ```dart
  259. /// int currentPageIndex = await _controller.getCurrentPageIndex();
  260. /// ```
  261. Future<int> getCurrentPageIndex() async {
  262. return await _channel.invokeMethod('get_current_page_index');
  263. }
  264. /// In the single page mode, set whether all pages keep the same width and the original page keeps the same width as readerView
  265. ///
  266. /// [isSame] true: All pages keep the same width, the original state keeps the same width as readerView; false: Show in the actual width of page
  267. ///
  268. /// example:
  269. /// ```dart
  270. /// _controller.setPageSameWidth(true);
  271. /// ```
  272. Future<void> setPageSameWidth(bool isSame) async {
  273. assert(Platform.isAndroid, 'This method is only supported on Android');
  274. await _channel.invokeMethod('set_page_same_width', isSame);
  275. }
  276. /// Gets whether the specified [pageIndex] is displayed on the screen
  277. ///
  278. /// example:
  279. /// ```dart
  280. /// bool isPageInScreen = await _controller.isPageInScreen(1);
  281. /// ```
  282. Future<bool> isPageInScreen(int pageIndex) async {
  283. assert(Platform.isAndroid, 'This method is only supported on Android');
  284. return await _channel.invokeMethod('is_page_in_screen', pageIndex);
  285. }
  286. /// Sets whether to fix the position of the non-swipe direction when zooming in for reading.
  287. ///
  288. /// [isFixedScroll] true: fixed position; false: not fixed position; Set false by default
  289. ///
  290. Future<void> setFixedScroll(bool isFixedScroll) async {
  291. assert(Platform.isAndroid, 'This method is only supported on Android');
  292. await _channel.invokeMethod('set_fixed_scroll', isFixedScroll);
  293. }
  294. /// Check the document for modifications
  295. ///
  296. /// example:
  297. /// ```dart
  298. /// bool hasChange = await document.hasChange();
  299. /// ```
  300. Future<bool> hasChange() async {
  301. return await _channel.invokeMethod('has_change');
  302. }
  303. }