cpdf_configuration.dart 14 KB

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