cpdf_reader_widget_controller.dart 11 KB

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