cpdf_reader_widget_controller.dart 10.0 KB

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