cpdf_configuration.dart 14 KB

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