cpdf_configuration.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. ]});
  121. Map<String, dynamic> toJson() => {
  122. 'androidAvailableActions':
  123. androidAvailableActions.map((e) => e.name).toList(),
  124. 'iosLeftBarAvailableActions':
  125. iosLeftBarAvailableActions.map((e) => e.name).toList(),
  126. 'iosRightBarAvailableActions':
  127. iosRightBarAvailableActions.map((e) => e.name).toList(),
  128. 'availableMenus': availableMenus.map((e) => e.name).toList()
  129. };
  130. }
  131. /// pdf readerView configuration
  132. class CPDFReaderViewConfig {
  133. // Highlight hyperlink annotations in pdf
  134. final bool linkHighlight;
  135. // Highlight hyperlink form field
  136. final bool formFieldHighlight;
  137. /// Display mode of the PDF document, single page, double page, or book mode.
  138. /// Default: [CPDFDisplayMode.singlePage]
  139. final CPDFDisplayMode displayMode;
  140. /// Whether PDF page flipping is continuous scrolling.
  141. final bool continueMode;
  142. /// Whether scrolling is in vertical direction.
  143. ///
  144. /// `true`: Vertical scrolling.<br/>
  145. /// `false`: Horizontal scrolling. <br/>
  146. /// Default: true
  147. final bool verticalMode;
  148. /// Cropping mode.
  149. ///
  150. /// Whether to crop blank areas of PDF pages.<br/>
  151. /// Default: false
  152. final bool cropMode;
  153. /// Theme color.
  154. ///
  155. /// Default: [CPDFThemes.light]
  156. final CPDFThemes themes;
  157. /// Whether to display the sidebar quick scroll bar.
  158. final bool enableSliderBar;
  159. /// Whether to display the bottom page indicator.
  160. final bool enablePageIndicator;
  161. /// Spacing between each page of the PDF, default 10px.
  162. final int pageSpacing;
  163. /// Page scale value, default 1.0.
  164. final double pageScale;
  165. /// only android platform
  166. final bool pageSameWidth;
  167. const CPDFReaderViewConfig(
  168. {this.linkHighlight = true,
  169. this.formFieldHighlight = true,
  170. this.displayMode = CPDFDisplayMode.singlePage,
  171. this.continueMode = true,
  172. this.verticalMode = true,
  173. this.cropMode = false,
  174. this.themes = CPDFThemes.light,
  175. this.enableSliderBar = true,
  176. this.enablePageIndicator = true,
  177. this.pageSpacing = 10,
  178. this.pageScale = 1.0,
  179. this.pageSameWidth = true});
  180. Map<String, dynamic> toJson() => {
  181. 'linkHighlight': linkHighlight,
  182. 'formFieldHighlight': formFieldHighlight,
  183. 'displayMode': displayMode.name,
  184. 'continueMode': continueMode,
  185. 'verticalMode': verticalMode,
  186. 'cropMode': cropMode,
  187. 'themes': themes.name,
  188. 'enableSliderBar': enableSliderBar,
  189. 'enablePageIndicator': enablePageIndicator,
  190. 'pageSpacing': pageSpacing,
  191. 'pageScale': pageScale,
  192. 'pageSameWidth': pageSameWidth
  193. };
  194. }
  195. class CPDFAnnotationsConfig {
  196. /// In the **V2.1.0** version, a new comment reply function is added,
  197. /// and the name of the comment author can be set here.
  198. final String annotationAuthor;
  199. /// [CPDFViewMode.annotations] mode,
  200. /// list of annotation functions shown at the bottom of the view.
  201. final List<CPDFAnnotationType> availableTypes;
  202. /// [CPDFViewMode.ANNOTATIONS] mode,
  203. /// annotation tools shown at the bottom of the view.
  204. final List<CPDFConfigTool> availableTools;
  205. /// When adding an annotation, the annotation’s default attributes.
  206. final CPDFAnnotAttribute initAttribute;
  207. const CPDFAnnotationsConfig(
  208. {this.availableTypes = CPDFAnnotationType.values,
  209. this.availableTools = CPDFConfigTool.values,
  210. this.initAttribute = const CPDFAnnotAttribute(),
  211. this.annotationAuthor = ""});
  212. Map<String, dynamic> toJson() => {
  213. 'availableTypes': availableTypes.map((e) => e.name).toList(),
  214. 'availableTools': availableTools.map((e) => e.name).toList(),
  215. 'initAttribute': initAttribute.toJson(),
  216. 'annotationAuthor': annotationAuthor
  217. };
  218. }
  219. class CPDFAnnotAttribute {
  220. /// Note annotation attribute configuration.
  221. final CPDFTextAttr noteAttr;
  222. final CPDFHighlightAttr highlightAttr;
  223. final CPDFUnderlineAttr underlineAttr;
  224. final CPDFSquigglyAttr squigglyAttr;
  225. final CPDFStrikeoutAttr strikeoutAttr;
  226. final CPDFInkAttr inkAttr;
  227. final CPDFSquareAttr squareAttr;
  228. final CPDFCircleAttr circleAttr;
  229. final CPDFLineAttr lineAttr;
  230. final CPDFArrowAttr arrowAttr;
  231. final CPDFFreetextAttr freeTextAttr;
  232. const CPDFAnnotAttribute({
  233. this.noteAttr = const CPDFTextAttr(),
  234. this.highlightAttr = const CPDFHighlightAttr(),
  235. this.underlineAttr = const CPDFUnderlineAttr(),
  236. this.squigglyAttr = const CPDFSquigglyAttr(),
  237. this.strikeoutAttr = const CPDFStrikeoutAttr(),
  238. this.inkAttr = const CPDFInkAttr(),
  239. this.squareAttr = const CPDFSquareAttr(),
  240. this.circleAttr = const CPDFCircleAttr(),
  241. this.lineAttr = const CPDFLineAttr(),
  242. this.arrowAttr = const CPDFArrowAttr(),
  243. this.freeTextAttr = const CPDFFreetextAttr(),
  244. });
  245. Map<String, dynamic> toJson() => {
  246. 'note': noteAttr.toJson(),
  247. 'highlight': highlightAttr.toJson(),
  248. 'underline': underlineAttr.toJson(),
  249. 'squiggly': squigglyAttr.toJson(),
  250. 'strikeout': strikeoutAttr.toJson(),
  251. 'ink': inkAttr.toJson(),
  252. 'square': squareAttr.toJson(),
  253. 'circle': circleAttr.toJson(),
  254. 'line': lineAttr.toJson(),
  255. 'arrow': arrowAttr.toJson(),
  256. 'freeText': freeTextAttr.toJson()
  257. };
  258. }
  259. class CPDFContentEditorConfig {
  260. /// Content editing mode, the editing mode displayed at the bottom of the view
  261. /// Default order: editorText, editorImage
  262. final List<CPDFContentEditorType> availableTypes;
  263. /// Available tools, including: Setting, Undo, Redo.
  264. final List<CPDFConfigTool> availableTools;
  265. final CPDFContentEditorAttribute initAttribute;
  266. const CPDFContentEditorConfig(
  267. {this.availableTypes = CPDFContentEditorType.values,
  268. this.availableTools = CPDFConfigTool.values,
  269. this.initAttribute = const CPDFContentEditorAttribute()});
  270. Map<String, dynamic> toJson() => {
  271. 'availableTypes': availableTypes.map((e) => e.name).toList(),
  272. 'availableTools': availableTools.map((e) => e.name).toList(),
  273. 'initAttribute': initAttribute.toJson()
  274. };
  275. }
  276. class CPDFContentEditorAttribute {
  277. final CPDFEditorTextAttr text;
  278. const CPDFContentEditorAttribute({this.text = const CPDFEditorTextAttr()});
  279. Map<String, dynamic> toJson() => {'text': text.toJson()};
  280. }
  281. class CPDFFormsConfig {
  282. /// In [CPDFViewMode.forms] mode, the list of form types at the bottom of the view.
  283. final List<CPDFFormType> availableTypes;
  284. /// Only supports [CPDFConfigTool.undo] and [CPDFConfigTool.redo].
  285. final List<CPDFConfigTool> availableTools;
  286. /// Form default attribute configuration
  287. final CPDFFormAttribute initAttribute;
  288. const CPDFFormsConfig(
  289. {this.availableTypes = CPDFFormType.values,
  290. this.availableTools = const [CPDFConfigTool.undo, CPDFConfigTool.redo],
  291. this.initAttribute = const CPDFFormAttribute()});
  292. Map<String, dynamic> toJson() => {
  293. 'availableTypes': availableTypes.map((e) => e.name).toList(),
  294. 'availableTools': availableTools.map((e) => e.name).toList(),
  295. 'initAttribute': initAttribute.toJson()
  296. };
  297. }
  298. class CPDFFormAttribute {
  299. final CPDFTextFieldAttr textFieldAttr;
  300. final CPDFCheckBoxAttr checkBoxAttr;
  301. final CPDFRadioButtonAttr radioButtonAttr;
  302. final CPDFListBoxAttr listBoxAttr;
  303. final CPDFComboBoxAttr comboBoxAttr;
  304. final CPDFPushButtonAttr pushButtonAttr;
  305. final CPDFSignatureWidgetAttr signaturesFieldsAttr;
  306. const CPDFFormAttribute({
  307. this.textFieldAttr = const CPDFTextFieldAttr(),
  308. this.checkBoxAttr = const CPDFCheckBoxAttr(),
  309. this.radioButtonAttr = const CPDFRadioButtonAttr(),
  310. this.listBoxAttr = const CPDFListBoxAttr(),
  311. this.comboBoxAttr = const CPDFComboBoxAttr(),
  312. this.pushButtonAttr = const CPDFPushButtonAttr(),
  313. this.signaturesFieldsAttr = const CPDFSignatureWidgetAttr(),
  314. });
  315. Map<String, dynamic> toJson() => {
  316. 'textField': textFieldAttr.toJson(),
  317. 'checkBox': checkBoxAttr.toJson(),
  318. 'radioButton': radioButtonAttr.toJson(),
  319. 'listBox': listBoxAttr.toJson(),
  320. 'comboBox': comboBoxAttr.toJson(),
  321. 'pushButton': pushButtonAttr.toJson(),
  322. 'signaturesFields': signaturesFieldsAttr.toJson()
  323. };
  324. }
  325. class CPDFGlobalConfig {
  326. /// Only supports Android platform in version 2.0.2
  327. // Set the view theme mode except the PDF area, the default value is [CPDFThemeMode.system]
  328. final CPDFThemeMode themeMode;
  329. /// In version V2.1.0, you can set whether to save a subset of fonts when the document is saved.
  330. /// The default value is true. Saving font subsets may increase file size.
  331. final bool fileSaveExtraFontSubset;
  332. const CPDFGlobalConfig(
  333. {this.themeMode = CPDFThemeMode.system,
  334. this.fileSaveExtraFontSubset = true});
  335. Map<String, dynamic> toJson() => {
  336. "themeMode": themeMode.name,
  337. "fileSaveExtraFontSubset": fileSaveExtraFontSubset
  338. };
  339. }