cpdf_configuration.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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:convert';
  8. import 'package:compdfkit_flutter/configuration/attributes/cpdf_annot_attr.dart';
  9. import 'cpdf_options.dart';
  10. /// modeConfig: Configuration of parameters that can be adjusted when opening a PDF interface.
  11. /// For example, setting the default display mode when opening, such as entering viewer or annotations mode.
  12. ///
  13. /// toolbarConfig: Configuration of top toolbar functionality and menu feature lists.
  14. /// This allows you to customize the buttons and menu options on the top toolbar for various operations.
  15. ///
  16. /// readerViewConfig: Configuration related to the PDF view,
  17. /// including functions like highlighting hyperlinks and form field highlighting.
  18. ///
  19. /// ```dart
  20. /// ComPDFKit.openDocument(
  21. /// tempDocumentPath,
  22. /// password: '',
  23. /// configuration: CPDFConfiguration());
  24. ///
  25. /// ```
  26. class CPDFConfiguration {
  27. CPDFModeConfig modeConfig;
  28. CPDFToolbarConfig toolbarConfig;
  29. CPDFReaderViewConfig readerViewConfig;
  30. CPDFAnnotationsConfig annotationsConfig;
  31. CPDFContentEditorConfig contentEditorConfig;
  32. CPDFFormsConfig formsConfig;
  33. CPDFGlobalConfig globalConfig;
  34. CPDFConfiguration(
  35. {this.modeConfig =
  36. const CPDFModeConfig(initialViewMode: CPDFViewMode.viewer),
  37. this.toolbarConfig = const CPDFToolbarConfig(),
  38. this.readerViewConfig = const CPDFReaderViewConfig(),
  39. this.annotationsConfig = const CPDFAnnotationsConfig(),
  40. this.contentEditorConfig = const CPDFContentEditorConfig(),
  41. this.formsConfig = const CPDFFormsConfig(),
  42. this.globalConfig = const CPDFGlobalConfig()});
  43. String toJson() => jsonEncode({
  44. 'modeConfig': modeConfig.toJson(),
  45. 'toolbarConfig': toolbarConfig.toJson(),
  46. 'readerViewConfig': readerViewConfig.toJson(),
  47. 'annotationsConfig': annotationsConfig.toJson(),
  48. 'contentEditorConfig': contentEditorConfig.toJson(),
  49. 'formsConfig': formsConfig.toJson(),
  50. 'global': globalConfig.toJson()
  51. });
  52. }
  53. // Set the default display mode and list of supported modes when rendering PDF documents.
  54. // The default display mode is the **viewer** mode.
  55. // In this mode, you can preview the document and fill in the form,
  56. // but you cannot edit comments and other content.
  57. class CPDFModeConfig {
  58. /// Default mode to display when opening the PDF View, default is [CPDFViewMode.viewer]
  59. final CPDFViewMode initialViewMode;
  60. /// Configure supported modes
  61. final List<CPDFViewMode> availableViewModes;
  62. /// Setting this to true will hide the top and bottom toolbars,
  63. /// displaying only the central PDF document view.
  64. /// Conversely, setting this to false will display all toolbars.
  65. final bool readerOnly;
  66. const CPDFModeConfig(
  67. {this.initialViewMode = CPDFViewMode.viewer,
  68. this.readerOnly = false,
  69. this.availableViewModes = CPDFViewMode.values});
  70. Map<String, dynamic> toJson() => {
  71. 'initialViewMode': initialViewMode.name,
  72. 'readerOnly': readerOnly,
  73. 'availableViewModes': availableViewModes.map((e) => e.name).toList()
  74. };
  75. }
  76. /// Configuration for top toolbar functionality.
  77. class CPDFToolbarConfig {
  78. /// Top toolbar actions for Android platform
  79. ///
  80. /// Default: thumbnail, search, bota, menu.
  81. ///
  82. /// [CPDFToolbarAction.BACK] button will be shown only on the far left
  83. final List<CPDFToolbarAction> androidAvailableActions;
  84. /// Left toolbar actions for iOS platform
  85. ///
  86. /// Default: back, thumbnail
  87. final List<CPDFToolbarAction> iosLeftBarAvailableActions;
  88. /// Right toolbar actions for iOS platform
  89. ///
  90. /// Default: search, bota, menu
  91. final List<CPDFToolbarAction> iosRightBarAvailableActions;
  92. /// Configure the menu options opened in the top toolbar [CPDFToolbarAction.menu]
  93. final List<CPDFToolbarMenuAction> availableMenus;
  94. const CPDFToolbarConfig(
  95. {this.androidAvailableActions = const [
  96. CPDFToolbarAction.thumbnail,
  97. CPDFToolbarAction.search,
  98. CPDFToolbarAction.bota,
  99. CPDFToolbarAction.menu,
  100. ],
  101. this.iosLeftBarAvailableActions = const [
  102. CPDFToolbarAction.back,
  103. CPDFToolbarAction.thumbnail,
  104. ],
  105. this.iosRightBarAvailableActions = const [
  106. CPDFToolbarAction.search,
  107. CPDFToolbarAction.bota,
  108. CPDFToolbarAction.menu
  109. ],
  110. this.availableMenus = const [
  111. CPDFToolbarMenuAction.viewSettings,
  112. CPDFToolbarMenuAction.documentEditor,
  113. CPDFToolbarMenuAction.security,
  114. CPDFToolbarMenuAction.watermark,
  115. CPDFToolbarMenuAction.flattened,
  116. CPDFToolbarMenuAction.documentInfo,
  117. CPDFToolbarMenuAction.save,
  118. CPDFToolbarMenuAction.share,
  119. CPDFToolbarMenuAction.openDocument,
  120. CPDFToolbarMenuAction.snip
  121. ]});
  122. Map<String, dynamic> toJson() => {
  123. 'androidAvailableActions':
  124. androidAvailableActions.map((e) => e.name).toList(),
  125. 'iosLeftBarAvailableActions':
  126. iosLeftBarAvailableActions.map((e) => e.name).toList(),
  127. 'iosRightBarAvailableActions':
  128. iosRightBarAvailableActions.map((e) => e.name).toList(),
  129. 'availableMenus': availableMenus.map((e) => e.name).toList()
  130. };
  131. }
  132. /// pdf readerView configuration
  133. class CPDFReaderViewConfig {
  134. // Highlight hyperlink annotations in pdf
  135. final bool linkHighlight;
  136. // Highlight hyperlink form field
  137. final bool formFieldHighlight;
  138. /// Display mode of the PDF document, single page, double page, or book mode.
  139. /// Default: [CPDFDisplayMode.singlePage]
  140. final CPDFDisplayMode displayMode;
  141. /// Whether PDF page flipping is continuous scrolling.
  142. final bool continueMode;
  143. /// Whether scrolling is in vertical direction.
  144. ///
  145. /// `true`: Vertical scrolling.<br/>
  146. /// `false`: Horizontal scrolling. <br/>
  147. /// Default: true
  148. final bool verticalMode;
  149. /// Cropping mode.
  150. ///
  151. /// Whether to crop blank areas of PDF pages.<br/>
  152. /// Default: false
  153. final bool cropMode;
  154. /// Theme color.
  155. ///
  156. /// Default: [CPDFThemes.light]
  157. final CPDFThemes themes;
  158. /// Whether to display the sidebar quick scroll bar.
  159. final bool enableSliderBar;
  160. /// Whether to display the bottom page indicator.
  161. final bool enablePageIndicator;
  162. /// Spacing between each page of the PDF, default 10px.
  163. final int pageSpacing;
  164. /// Page scale value, default 1.0.
  165. final double pageScale;
  166. /// only android platform
  167. final bool pageSameWidth;
  168. const CPDFReaderViewConfig(
  169. {this.linkHighlight = true,
  170. this.formFieldHighlight = true,
  171. this.displayMode = CPDFDisplayMode.singlePage,
  172. this.continueMode = true,
  173. this.verticalMode = true,
  174. this.cropMode = false,
  175. this.themes = CPDFThemes.light,
  176. this.enableSliderBar = true,
  177. this.enablePageIndicator = true,
  178. this.pageSpacing = 10,
  179. this.pageScale = 1.0,
  180. this.pageSameWidth = true});
  181. Map<String, dynamic> toJson() => {
  182. 'linkHighlight': linkHighlight,
  183. 'formFieldHighlight': formFieldHighlight,
  184. 'displayMode': displayMode.name,
  185. 'continueMode': continueMode,
  186. 'verticalMode': verticalMode,
  187. 'cropMode': cropMode,
  188. 'themes': themes.name,
  189. 'enableSliderBar': enableSliderBar,
  190. 'enablePageIndicator': enablePageIndicator,
  191. 'pageSpacing': pageSpacing,
  192. 'pageScale': pageScale,
  193. 'pageSameWidth': pageSameWidth
  194. };
  195. }
  196. class CPDFAnnotationsConfig {
  197. /// In the **V2.1.0** version, a new comment reply function is added,
  198. /// and the name of the comment author can be set here.
  199. final String annotationAuthor;
  200. /// [CPDFViewMode.annotations] mode,
  201. /// list of annotation functions shown at the bottom of the view.
  202. final List<CPDFAnnotationType> availableTypes;
  203. /// [CPDFViewMode.ANNOTATIONS] mode,
  204. /// annotation tools shown at the bottom of the view.
  205. final List<CPDFConfigTool> availableTools;
  206. /// When adding an annotation, the annotation’s default attributes.
  207. final CPDFAnnotAttribute initAttribute;
  208. const CPDFAnnotationsConfig(
  209. {this.availableTypes = CPDFAnnotationType.values,
  210. this.availableTools = CPDFConfigTool.values,
  211. this.initAttribute = const CPDFAnnotAttribute(),
  212. this.annotationAuthor = ""});
  213. Map<String, dynamic> toJson() => {
  214. 'availableTypes': availableTypes.map((e) => e.name).toList(),
  215. 'availableTools': availableTools.map((e) => e.name).toList(),
  216. 'initAttribute': initAttribute.toJson(),
  217. 'annotationAuthor': annotationAuthor
  218. };
  219. }
  220. class CPDFAnnotAttribute {
  221. /// Note annotation attribute configuration.
  222. final CPDFTextAttr noteAttr;
  223. final CPDFHighlightAttr highlightAttr;
  224. final CPDFUnderlineAttr underlineAttr;
  225. final CPDFSquigglyAttr squigglyAttr;
  226. final CPDFStrikeoutAttr strikeoutAttr;
  227. final CPDFInkAttr inkAttr;
  228. final CPDFSquareAttr squareAttr;
  229. final CPDFCircleAttr circleAttr;
  230. final CPDFLineAttr lineAttr;
  231. final CPDFArrowAttr arrowAttr;
  232. final CPDFFreetextAttr freeTextAttr;
  233. const CPDFAnnotAttribute({
  234. this.noteAttr = const CPDFTextAttr(),
  235. this.highlightAttr = const CPDFHighlightAttr(),
  236. this.underlineAttr = const CPDFUnderlineAttr(),
  237. this.squigglyAttr = const CPDFSquigglyAttr(),
  238. this.strikeoutAttr = const CPDFStrikeoutAttr(),
  239. this.inkAttr = const CPDFInkAttr(),
  240. this.squareAttr = const CPDFSquareAttr(),
  241. this.circleAttr = const CPDFCircleAttr(),
  242. this.lineAttr = const CPDFLineAttr(),
  243. this.arrowAttr = const CPDFArrowAttr(),
  244. this.freeTextAttr = const CPDFFreetextAttr(),
  245. });
  246. Map<String, dynamic> toJson() => {
  247. 'note': noteAttr.toJson(),
  248. 'highlight': highlightAttr.toJson(),
  249. 'underline': underlineAttr.toJson(),
  250. 'squiggly': squigglyAttr.toJson(),
  251. 'strikeout': strikeoutAttr.toJson(),
  252. 'ink': inkAttr.toJson(),
  253. 'square': squareAttr.toJson(),
  254. 'circle': circleAttr.toJson(),
  255. 'line': lineAttr.toJson(),
  256. 'arrow': arrowAttr.toJson(),
  257. 'freeText': freeTextAttr.toJson()
  258. };
  259. }
  260. class CPDFContentEditorConfig {
  261. /// Content editing mode, the editing mode displayed at the bottom of the view
  262. /// Default order: editorText, editorImage
  263. final List<CPDFContentEditorType> availableTypes;
  264. /// Available tools, including: Setting, Undo, Redo.
  265. final List<CPDFConfigTool> availableTools;
  266. final CPDFContentEditorAttribute initAttribute;
  267. const CPDFContentEditorConfig(
  268. {this.availableTypes = CPDFContentEditorType.values,
  269. this.availableTools = CPDFConfigTool.values,
  270. this.initAttribute = const CPDFContentEditorAttribute()});
  271. Map<String, dynamic> toJson() => {
  272. 'availableTypes': availableTypes.map((e) => e.name).toList(),
  273. 'availableTools': availableTools.map((e) => e.name).toList(),
  274. 'initAttribute': initAttribute.toJson()
  275. };
  276. }
  277. class CPDFContentEditorAttribute {
  278. final CPDFEditorTextAttr text;
  279. const CPDFContentEditorAttribute({this.text = const CPDFEditorTextAttr()});
  280. Map<String, dynamic> toJson() => {'text': text.toJson()};
  281. }
  282. class CPDFFormsConfig {
  283. /// In [CPDFViewMode.forms] mode, the list of form types at the bottom of the view.
  284. final List<CPDFFormType> availableTypes;
  285. /// Only supports [CPDFConfigTool.undo] and [CPDFConfigTool.redo].
  286. final List<CPDFConfigTool> availableTools;
  287. /// Form default attribute configuration
  288. final CPDFFormAttribute initAttribute;
  289. const CPDFFormsConfig(
  290. {this.availableTypes = CPDFFormType.values,
  291. this.availableTools = const [CPDFConfigTool.undo, CPDFConfigTool.redo],
  292. this.initAttribute = const CPDFFormAttribute()});
  293. Map<String, dynamic> toJson() => {
  294. 'availableTypes': availableTypes.map((e) => e.name).toList(),
  295. 'availableTools': availableTools.map((e) => e.name).toList(),
  296. 'initAttribute': initAttribute.toJson()
  297. };
  298. }
  299. class CPDFFormAttribute {
  300. final CPDFTextFieldAttr textFieldAttr;
  301. final CPDFCheckBoxAttr checkBoxAttr;
  302. final CPDFRadioButtonAttr radioButtonAttr;
  303. final CPDFListBoxAttr listBoxAttr;
  304. final CPDFComboBoxAttr comboBoxAttr;
  305. final CPDFPushButtonAttr pushButtonAttr;
  306. final CPDFSignatureWidgetAttr signaturesFieldsAttr;
  307. const CPDFFormAttribute({
  308. this.textFieldAttr = const CPDFTextFieldAttr(),
  309. this.checkBoxAttr = const CPDFCheckBoxAttr(),
  310. this.radioButtonAttr = const CPDFRadioButtonAttr(),
  311. this.listBoxAttr = const CPDFListBoxAttr(),
  312. this.comboBoxAttr = const CPDFComboBoxAttr(),
  313. this.pushButtonAttr = const CPDFPushButtonAttr(),
  314. this.signaturesFieldsAttr = const CPDFSignatureWidgetAttr(),
  315. });
  316. Map<String, dynamic> toJson() => {
  317. 'textField': textFieldAttr.toJson(),
  318. 'checkBox': checkBoxAttr.toJson(),
  319. 'radioButton': radioButtonAttr.toJson(),
  320. 'listBox': listBoxAttr.toJson(),
  321. 'comboBox': comboBoxAttr.toJson(),
  322. 'pushButton': pushButtonAttr.toJson(),
  323. 'signaturesFields': signaturesFieldsAttr.toJson()
  324. };
  325. }
  326. class CPDFGlobalConfig {
  327. /// Only supports Android platform in version 2.0.2
  328. // Set the view theme mode except the PDF area, the default value is [CPDFThemeMode.system]
  329. final CPDFThemeMode themeMode;
  330. /// In version V2.1.0, you can set whether to save a subset of fonts when the document is saved.
  331. /// The default value is true. Saving font subsets may increase file size.
  332. final bool fileSaveExtraFontSubset;
  333. const CPDFGlobalConfig(
  334. {this.themeMode = CPDFThemeMode.system,
  335. this.fileSaveExtraFontSubset = true});
  336. Map<String, dynamic> toJson() => {
  337. "themeMode": themeMode.name,
  338. "fileSaveExtraFontSubset": fileSaveExtraFontSubset
  339. };
  340. }