cpdf_reader_widget_controller.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // Copyright © 2014-2025 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. ///
  53. /// example:
  54. /// ```dart
  55. /// bool result = await _controller.save();
  56. /// ```
  57. /// Return value: **true** if the save is successful,
  58. /// **false** if the save fails.
  59. @Deprecated("use CPDFDocument().save()")
  60. Future<bool> save() async {
  61. return await document.save();
  62. }
  63. /// Set the page scale
  64. /// Value Range: 1.0~5.0
  65. ///
  66. /// example:
  67. /// ```dart
  68. /// await _controller.setScale(1.5);
  69. /// ```
  70. Future<void> setScale(double scale) async {
  71. await _channel.invokeMethod('set_scale', scale);
  72. }
  73. /// Get the page scale
  74. ///
  75. /// example:
  76. /// ```dart
  77. /// double scaleValue = await _controller.getScale();
  78. /// ```
  79. Future<double> getScale() async {
  80. return await _channel.invokeMethod('get_scale');
  81. }
  82. /// Whether allow to scale.
  83. /// Default : true
  84. ///
  85. /// example:
  86. /// ```dart
  87. /// await _controller.setCanScale(canScale);
  88. /// ```
  89. Future<void> setCanScale(bool canScale) async {
  90. assert(Platform.isAndroid, 'This method is only supported on Android');
  91. await _channel.invokeMethod('set_can_scale', canScale);
  92. }
  93. /// Sets background color of reader.
  94. /// The color of each document space will be set to 75% of [color] transparency
  95. ///
  96. /// example:
  97. /// ```dart
  98. /// await _controller.setReadBackgroundColor(theme: CPDFThemes.light);
  99. /// ```
  100. Future<void> setReadBackgroundColor(CPDFThemes theme) async {
  101. await _channel.invokeMethod('set_read_background_color', {
  102. 'displayMode': theme.name,
  103. 'color' : theme.color
  104. });
  105. }
  106. /// Get background color of reader.
  107. ///
  108. /// example:
  109. /// ```dart
  110. /// Color color = await _controller.getReadBackgroundColor();
  111. /// ```
  112. Future<Color> getReadBackgroundColor() async {
  113. String hexColor = await _channel.invokeMethod('get_read_background_color');
  114. return HexColor.fromHex(hexColor);
  115. }
  116. /// Sets whether to display highlight Form Field.
  117. /// [isFormFieldHighlight] : true to display highlight Form Field.
  118. ///
  119. /// example:
  120. /// ```dart
  121. /// await _controller.setFormFieldHighlight(true);
  122. /// ```
  123. Future<void> setFormFieldHighlight(bool isFormFieldHighlight) async {
  124. await _channel.invokeMethod(
  125. 'set_form_field_highlight', isFormFieldHighlight);
  126. }
  127. /// Whether to display highlight Form Field.
  128. ///
  129. /// example:
  130. /// ```dart
  131. /// bool isFormFieldHighlight = await _controller.isFormFieldHighlight();
  132. /// ```
  133. Future<bool> isFormFieldHighlight() async {
  134. return await _channel.invokeMethod('is_form_field_highlight');
  135. }
  136. /// Sets whether to display highlight Link.
  137. ///
  138. /// [isLinkHighlight] : Whether to highlight Link.
  139. ///
  140. /// example:
  141. /// ```dart
  142. /// await _controller.setLinkHighlight(true);
  143. /// ```
  144. Future<void> setLinkHighlight(bool isLinkHighlight) async {
  145. await _channel.invokeMethod('set_link_highlight', isLinkHighlight);
  146. }
  147. /// Whether to display highlight Link.
  148. ///
  149. /// example:
  150. /// ```dart
  151. /// bool isLinkHighlight = await _controller.isLinkHighlight();
  152. /// ```
  153. Future<bool> isLinkHighlight() async {
  154. return await _channel.invokeMethod('is_link_highlight');
  155. }
  156. /// Sets whether it is vertical scroll mode.
  157. ///
  158. /// [isVerticalMode] : Whether it is vertical scroll mode.
  159. ///
  160. /// example:
  161. /// ```dart
  162. /// await _controller.setVerticalMode(true);
  163. /// ```
  164. Future<void> setVerticalMode(bool isVerticalMode) async {
  165. await _channel.invokeMethod('set_vertical_mode', isVerticalMode);
  166. }
  167. /// Whether it is vertical scroll mode.
  168. ///
  169. /// example:
  170. /// ```dart
  171. /// bool isVerticalMode = await _controller.isVerticalMode();
  172. /// ```
  173. Future<bool> isVerticalMode() async {
  174. return await _channel.invokeMethod('is_vertical_mode');
  175. }
  176. /// Set document page spacing
  177. ///
  178. /// [spacing] the spacing between pages
  179. ///
  180. /// example:
  181. /// ```dart
  182. /// await _controller.setMargins(const CPDFEdgeInsets.symmetric(horizontal: 10, vertical: 10));
  183. /// ```
  184. Future<void> setMargins(CPDFEdgeInsets edgeInsets) async {
  185. await _channel.invokeMethod('set_margin' , edgeInsets.toJson());
  186. }
  187. /// Sets the spacing between pages. This method is supported only on the [Android] platform.
  188. ///
  189. /// - For the [iOS] platform, use the [setMargins] method instead.
  190. /// The spacing between pages is equal to the value of [CPDFEdgeInsets.top].
  191. ///
  192. /// Parameters:
  193. /// [spacing] The space between pages, in pixels.
  194. ///
  195. /// example:
  196. /// ```dart
  197. /// await _controller.setPageSpacing(10);
  198. /// ```
  199. Future<void> setPageSpacing(int spacing) async {
  200. await _channel.invokeMethod('set_page_spacing', spacing);
  201. }
  202. /// Sets whether it is continuous scroll mode.
  203. ///
  204. /// [isContinueMode] Whether it is continuous scroll mode.
  205. ///
  206. /// example:
  207. /// ```dart
  208. /// await _controller.setContinueMode(true);
  209. /// ```
  210. Future<void> setContinueMode(bool isContinueMode) async {
  211. await _channel.invokeMethod('set_continue_mode', isContinueMode);
  212. }
  213. /// Whether it is continuous scroll mode.
  214. ///
  215. /// example:
  216. /// ```dart
  217. /// bool isContinueMode = await _controller.isContinueMode();
  218. /// ```
  219. Future<bool> isContinueMode() async {
  220. return await _channel.invokeMethod('is_continue_mode');
  221. }
  222. /// Sets whether it is double page mode.
  223. ///
  224. /// [isDoublePageMode] Whether it is double page mode.
  225. ///
  226. /// example:
  227. /// ```dart
  228. /// await _controller.setDoublePageMode(true);
  229. /// ```
  230. Future<void> setDoublePageMode(bool isDoublePageMode) async {
  231. await _channel.invokeMethod('set_double_page_mode', isDoublePageMode);
  232. }
  233. /// Whether it is double page mode.
  234. ///is_
  235. /// example:
  236. /// ```dart
  237. /// bool isDoublePageMode = await _controller.isDoublePageMode();
  238. /// ```
  239. Future<bool> isDoublePageMode() async {
  240. return await _channel.invokeMethod('is_double_page_mode');
  241. }
  242. /// Sets whether it is cover page mode.
  243. ///
  244. /// example:
  245. /// ```dart
  246. /// await _controller.setCoverPageMode(true);
  247. /// ```
  248. Future<void> setCoverPageMode(bool coverPageMode) async {
  249. await _channel.invokeMethod('set_cover_page_mode', coverPageMode);
  250. }
  251. /// Whether it is cover page mode.
  252. ///
  253. /// example:
  254. /// ```dart
  255. /// bool isCoverPageMode = await _controller.isCoverPageMode();
  256. /// ```
  257. Future<bool> isCoverPageMode() async {
  258. return await _channel.invokeMethod('is_cover_page_mode');
  259. }
  260. /// Sets whether it is crop mode.
  261. ///
  262. /// [cropMode] Whether it is crop mode.
  263. ///
  264. /// example:
  265. /// ```dart
  266. /// _controller.setCropMode(true);
  267. /// ```
  268. Future<void> setCropMode(bool isCropMode) async {
  269. await _channel.invokeMethod('set_crop_mode', isCropMode);
  270. }
  271. /// Whether it is crop mode.
  272. ///
  273. /// example:
  274. /// ```dart
  275. /// bool isCropMode = await _controller.isCropMode();
  276. /// ```
  277. Future<bool> isCropMode() async {
  278. return await _channel.invokeMethod('is_crop_mode');
  279. }
  280. /// Jump to the index page.
  281. ///
  282. /// [pageIndex] The index of the page to jump.
  283. /// [animated] only for iOS, whether to use animation when jumping.
  284. ///
  285. /// example:
  286. /// ```dart
  287. /// _controller.setDisplayPageIndex(1, animated: true);
  288. /// ```
  289. Future<void> setDisplayPageIndex(int pageIndex,
  290. {bool animated = true}) async {
  291. await _channel.invokeMethod('set_display_page_index',
  292. {'pageIndex': pageIndex, 'animated': animated});
  293. }
  294. /// get current page index
  295. ///
  296. /// example:
  297. /// ```dart
  298. /// int currentPageIndex = await _controller.getCurrentPageIndex();
  299. /// ```
  300. Future<int> getCurrentPageIndex() async {
  301. return await _channel.invokeMethod('get_current_page_index');
  302. }
  303. /// In the single page mode, set whether all pages keep the same width and the original page keeps the same width as readerView
  304. ///
  305. /// [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
  306. ///
  307. /// example:
  308. /// ```dart
  309. /// _controller.setPageSameWidth(true);
  310. /// ```
  311. Future<void> setPageSameWidth(bool isSame) async {
  312. assert(Platform.isAndroid, 'This method is only supported on Android');
  313. await _channel.invokeMethod('set_page_same_width', isSame);
  314. }
  315. /// Gets whether the specified [pageIndex] is displayed on the screen
  316. ///
  317. /// example:
  318. /// ```dart
  319. /// bool isPageInScreen = await _controller.isPageInScreen(1);
  320. /// ```
  321. Future<bool> isPageInScreen(int pageIndex) async {
  322. assert(Platform.isAndroid, 'This method is only supported on Android');
  323. return await _channel.invokeMethod('is_page_in_screen', pageIndex);
  324. }
  325. /// Sets whether to fix the position of the non-swipe direction when zooming in for reading.
  326. ///
  327. /// example:
  328. /// ```dart
  329. /// await _controller.setFixedScroll(true);
  330. /// ```
  331. ///
  332. /// [isFixedScroll] true: fixed position; false: not fixed position; Set false by default
  333. ///
  334. Future<void> setFixedScroll(bool isFixedScroll) async {
  335. assert(Platform.isAndroid, 'This method is only supported on Android');
  336. await _channel.invokeMethod('set_fixed_scroll', isFixedScroll);
  337. }
  338. /// Check the document for modifications
  339. ///
  340. /// example:
  341. /// ```dart
  342. /// bool hasChange = await document.hasChange();
  343. /// ```
  344. @Deprecated("use CPDFDocument().hasChange()")
  345. Future<bool> hasChange() async {
  346. return await _document.hasChange();
  347. }
  348. /// Switch the mode displayed by the current CPDFReaderWidget.
  349. /// Please see [CPDFViewMode] for available modes.
  350. ///
  351. /// example:
  352. /// ```dart
  353. /// await setPreviewMode(CPDFViewMode.viewer);
  354. /// ```
  355. Future<void> setPreviewMode(CPDFViewMode viewMode) async {
  356. await _channel.invokeMethod('set_preview_mode', viewMode.name);
  357. }
  358. /// Get the currently displayed mode
  359. ///
  360. /// example:
  361. /// ```dart
  362. /// CPDFViewMode mode = await controller.getPreviewMode();
  363. /// ```
  364. Future<CPDFViewMode> getPreviewMode() async {
  365. String modeName = await _channel.invokeMethod('get_preview_mode');
  366. return CPDFViewMode.values.where((e) => e.name == modeName).first;
  367. }
  368. /// Displays the thumbnail view. When [editMode] is `true`, the page enters edit mode, allowing operations such as insert, delete, extract, etc.
  369. ///
  370. /// Example:
  371. /// ```dart
  372. /// await controller.showThumbnailView(false);
  373. /// ```
  374. Future<void> showThumbnailView(bool editMode) async {
  375. await _channel.invokeMethod('show_thumbnail_view', editMode);
  376. }
  377. /// Displays the BOTA view, which includes the document outline, bookmarks, and annotation list.
  378. ///
  379. /// Example:
  380. /// ```dart
  381. /// await controller.showBotaView();
  382. /// ```
  383. Future<void> showBotaView() async {
  384. await _channel.invokeMethod('show_bota_view');
  385. }
  386. /// Displays the "Add Watermark" view, where users can add watermarks to the document.
  387. ///
  388. /// Example:
  389. /// ```dart
  390. /// await controller.showAddWatermarkView();
  391. /// ```
  392. Future<void> showAddWatermarkView({bool saveAsNewFile = true}) async {
  393. await _channel.invokeMethod('show_add_watermark_view', saveAsNewFile);
  394. }
  395. /// Displays the document security settings view, allowing users to configure document security options.
  396. ///
  397. /// Example:
  398. /// ```dart
  399. /// await controller.showSecurityView();
  400. /// ```
  401. Future<void> showSecurityView() async {
  402. await _channel.invokeMethod('show_security_view');
  403. }
  404. /// Displays the display settings view, where users can configure options such as scroll direction, scroll mode, and themes.
  405. ///
  406. /// Example:
  407. /// ```dart
  408. /// await controller.showDisplaySettingView();
  409. /// ```
  410. Future<void> showDisplaySettingView() async {
  411. await _channel.invokeMethod('show_display_settings_view');
  412. }
  413. /// Enters snip mode, allowing users to capture screenshots.
  414. ///
  415. /// Example:
  416. /// ```dart
  417. /// await controller.enterSnipMode();
  418. /// ```
  419. Future<void> enterSnipMode() async {
  420. await _channel.invokeMethod('enter_snip_mode');
  421. }
  422. /// Exits snip mode, stopping the screenshot capture.
  423. ///
  424. /// Example:
  425. /// ```dart
  426. /// await controller.exitSnipMode();
  427. /// ```
  428. Future<void> exitSnipMode() async {
  429. await _channel.invokeMethod('exit_snip_mode');
  430. }
  431. }