cpdf_reader_widget_controller.dart 10 KB

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