index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /**
  2. * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  3. *
  4. * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. * This notice may not be removed from this file.
  8. */
  9. import { NativeModules } from 'react-native';
  10. import { CPDFConfiguration } from './configuration/CPDFConfiguration';
  11. import { CPDFAlignment, CPDFAnnotationType, CPDFBorderStyle, CPDFCheckStyle, CPDFConfigTool, CPDFContentEditorType, CPDFDisplayMode, CPDFFormType, CPDFLineType,CPDFThemeMode, CPDFThemes, CPDFToolbarAction, CPDFToolbarMenuAction, CPDFTypeface, CPDFViewMode } from './configuration/CPDFOptions';
  12. declare module 'react-native' {
  13. interface NativeModulesStatic {
  14. ComPDFKit: {
  15. getDefaultConfig(overrides : Partial<CPDFConfiguration>) : string;
  16. /**
  17. * Get the version number of the ComPDFKit SDK.
  18. * For example : '2.0.0'
  19. * @memberof ComPDFKit
  20. * @returns { Promise<string> } A Promise returning ComPDFKit PDF SDK Version Code
  21. *
  22. * @example
  23. * ComPDFKit.getVersionCode().then((versionCode : string) => {
  24. * console.log('ComPDFKit SDK Version:', versionCode)
  25. * })
  26. */
  27. getVersionCode(): () => Promise<string>;
  28. /**
  29. * Get the build tag of the ComPDFKit PDF SDK.
  30. *
  31. * For example: "build_beta_2.0.0_42db96987_202404081007"
  32. * @memberof ComPDFKit
  33. * @returns { Promise<string> } A Promise returning ComPDFKit PDF SDK Build Tag.
  34. *
  35. * @example
  36. * ComPDFKit.getSDKBuildTag().then((buildTag : string) => {
  37. * console.log('ComPDFKit Build Tag:', buildTag)
  38. * })
  39. */
  40. getSDKBuildTag(): () => Promise<string>;
  41. /**
  42. * Initialize the ComPDFKit PDF SDK using offline authentication.
  43. * Each ComPDFKit license is bound to a specific app bundle ID(Android Application ID).
  44. *
  45. * @method init_
  46. * @memberof ComPDFKit
  47. * @param { string } [license] Your ComPDFKit for React Native license key.
  48. * @returns { Promise<boolean> } Returns ```true``` if initialization is successful, otherwise returns ```false```.
  49. *
  50. * @example
  51. * ComPDFKit.init_('your compdfkit license')
  52. *
  53. */
  54. init_: (license: string) => Promise<boolean>;
  55. /**
  56. * Initialize the ComPDFKit PDF SDK using online authentication.
  57. * Each ComPDFKit license is bound to a specific app bundle ID(Android Application ID).
  58. *
  59. * @method initialize
  60. * @memberof ComPDFKit
  61. * @param { string } [androidOnlineLicense] Your ComPDFKit for React Native Android online license key.
  62. * @param { string } [iosOnlineLicense] Your ComPDFKit for React Native iOS online license key.
  63. * @returns { Promise<boolean> } Returns ```true``` if initialization is successful, otherwise returns ```false```.
  64. *
  65. * @example
  66. * ComPDFKit.initialize('your android compdfkit license', 'your ios compdfkit license')
  67. */
  68. initialize: (androidOnlineLicense: string, iosOnlineLicense: string) => Promise<boolean>;
  69. /**
  70. * Used to present a PDF document.
  71. * @method openDocument
  72. * @memberof ComPDFKit
  73. * @param { string } [document] document The path to the PDF document to be presented.
  74. *
  75. * * (Android) For local storage file path:
  76. * ```tsx
  77. * document = 'file:///storage/emulated/0/Download/sample.pdf'
  78. * ```
  79. * * (Android) For content Uri:
  80. * ```tsx
  81. * document = 'content://...'
  82. * ```
  83. * * (Android) For assets path:
  84. * ```tsx
  85. * document = "file:///android_asset/..."
  86. * ```
  87. * ---
  88. * * ios
  89. * ```tsx
  90. * document = 'pdf_document.pdf'
  91. * ```
  92. *
  93. * @param { string } [password] PDF document password.
  94. * @param { string } [configuration] Configuration objects to customize the appearance and behavior of ComPDFKit. See [CPDFConfiguration](configuration/CPDFConfiguration.ts)
  95. * @returns { void }
  96. *
  97. * @example
  98. * const fileName = 'pdf_document.pdf';
  99. * const document =
  100. * Platform.OS === 'ios' ? fileName
  101. * : 'file:///android_asset/' + fileName;
  102. *
  103. * const configuration : CPDFConfiguration = {
  104. * modeConfig: {
  105. * initialViewMode: CPDFModeConfig.ViewMode.VIEWER,
  106. * availableViewModes: [
  107. * CPDFModeConfig.ViewMode.VIEWER,
  108. * CPDFModeConfig.ViewMode.ANNOTATIONS,
  109. * CPDFModeConfig.ViewMode.CONTENT_EDITOR,
  110. * CPDFModeConfig.ViewMode.FORMS,
  111. * CPDFModeConfig.ViewMode.SIGNATURES
  112. * ]
  113. * }
  114. * }
  115. *
  116. * ComPDFKit.openDocument(document, 'password', JSON.stringify(configuration))
  117. *
  118. */
  119. openDocument: (document: string, password: string, configuration: string) => void;
  120. /**
  121. * Delete the saved signature file from the annotation signature list
  122. *
  123. * @example
  124. * ComPDFKit.removeSignFileList().then((result : boolean) => {
  125. * console.log('ComPDFKit removeSignFileList:', result)
  126. * })
  127. *
  128. * @returns
  129. */
  130. removeSignFileList : () => Promise<boolean>;
  131. };
  132. }
  133. }
  134. interface ComPDFKit {
  135. testConfig(configuration: string): Promise<string>;
  136. getVersionCode(): Promise<string>;
  137. getSDKBuildTag(): Promise<string>;
  138. init_(license: string): Promise<boolean>;
  139. initialize(androidOnlineLicense: string, iosOnlineLicense: string): Promise<boolean>;
  140. openDocument(document: string, password: string, configurationJson: string): void;
  141. removeSignFileList() : Promise<boolean>;
  142. }
  143. const ComPDFKit = NativeModules.ComPDFKit
  144. export { ComPDFKit };
  145. export {
  146. CPDFViewMode,
  147. CPDFToolbarAction,
  148. CPDFToolbarMenuAction,
  149. CPDFAnnotationType,
  150. CPDFConfigTool,
  151. CPDFBorderStyle,
  152. CPDFLineType,
  153. CPDFAlignment,
  154. CPDFTypeface,
  155. CPDFContentEditorType,
  156. CPDFFormType,
  157. CPDFCheckStyle,
  158. CPDFDisplayMode,
  159. CPDFThemes } from './configuration/CPDFOptions';
  160. export { CPDFReaderView } from './view/CPDFReaderView';
  161. ComPDFKit.getDefaultConfig = getDefaultConfig
  162. function getDefaultConfig(overrides : Partial<CPDFConfiguration> = {}) : string {
  163. const defaultConfig : CPDFConfiguration = {
  164. modeConfig:{
  165. initialViewMode: CPDFViewMode.VIEWER,
  166. readerOnly: false,
  167. availableViewModes: [
  168. CPDFViewMode.VIEWER,
  169. CPDFViewMode.ANNOTATIONS,
  170. CPDFViewMode.CONTENT_EDITOR,
  171. CPDFViewMode.FORMS,
  172. CPDFViewMode.SIGNATURES,
  173. ]
  174. },
  175. toolbarConfig: {
  176. mainToolbarVisible : true,
  177. androidAvailableActions: [
  178. CPDFToolbarAction.THUMBNAIL,
  179. CPDFToolbarAction.SEARCH,
  180. CPDFToolbarAction.BOTA,
  181. CPDFToolbarAction.MENU,
  182. ],
  183. iosLeftBarAvailableActions:[
  184. CPDFToolbarAction.BACK,
  185. CPDFToolbarAction.THUMBNAIL
  186. ],
  187. iosRightBarAvailableActions: [
  188. CPDFToolbarAction.SEARCH,
  189. CPDFToolbarAction.BOTA,
  190. CPDFToolbarAction.MENU
  191. ],
  192. availableMenus: [
  193. CPDFToolbarMenuAction.VIEW_SETTINGS,
  194. CPDFToolbarMenuAction.DOCUMENT_EDITOR,
  195. CPDFToolbarMenuAction.DOCUMENT_INFO,
  196. CPDFToolbarMenuAction.WATERMARK,
  197. CPDFToolbarMenuAction.SECURITY,
  198. CPDFToolbarMenuAction.FLATTENED,
  199. CPDFToolbarMenuAction.SAVE,
  200. CPDFToolbarMenuAction.SHARE,
  201. CPDFToolbarMenuAction.OPEN_DOCUMENT,
  202. CPDFToolbarMenuAction.SNIP
  203. ]
  204. },
  205. annotationsConfig: {
  206. annotationAuthor: '',
  207. availableTypes: [
  208. CPDFAnnotationType.NOTE,
  209. CPDFAnnotationType.HIGHLIGHT,
  210. CPDFAnnotationType.UNDERLINE,
  211. CPDFAnnotationType.SQUIGGLY,
  212. CPDFAnnotationType.STRIKEOUT,
  213. CPDFAnnotationType.INK,
  214. CPDFAnnotationType.CIRCLE,
  215. CPDFAnnotationType.SQUARE,
  216. CPDFAnnotationType.ARROW,
  217. CPDFAnnotationType.LINE,
  218. CPDFAnnotationType.FREETEXT,
  219. CPDFAnnotationType.SIGNATURE,
  220. CPDFAnnotationType.STAMP,
  221. CPDFAnnotationType.PICTURES,
  222. CPDFAnnotationType.LINK,
  223. CPDFAnnotationType.SOUND,
  224. ],
  225. availableTools: [
  226. CPDFConfigTool.SETTING,
  227. CPDFConfigTool.UNDO,
  228. CPDFConfigTool.REDO
  229. ],
  230. initAttribute: {
  231. note: {
  232. color: '#1460F3',
  233. alpha: 255
  234. },
  235. highlight: {
  236. color: '#1460F3',
  237. alpha: 77
  238. },
  239. underline: {
  240. color: '#1460F3',
  241. alpha: 77
  242. },
  243. squiggly: {
  244. color: '#1460F3',
  245. alpha: 77
  246. },
  247. strikeout: {
  248. color: '#1460F3',
  249. alpha: 77
  250. },
  251. ink: {
  252. color: '#1460F3',
  253. alpha: 100,
  254. borderWidth: 10
  255. },
  256. square: {
  257. fillColor: '#1460F3',
  258. borderColor: '#000000',
  259. colorAlpha : 128,
  260. borderWidth: 2,
  261. borderStyle: {
  262. style: CPDFBorderStyle.SOLID,
  263. dashGap: 8.0
  264. }
  265. },
  266. circle: {
  267. fillColor: '#1460F3',
  268. borderColor: '#000000',
  269. colorAlpha : 128,
  270. borderWidth: 2,
  271. borderStyle: {
  272. style: CPDFBorderStyle.SOLID,
  273. dashGap: 8.0
  274. }
  275. },
  276. line: {
  277. borderColor: '#1460F3',
  278. borderAlpha: 100,
  279. borderWidth: 5,
  280. borderStyle: {
  281. style: CPDFBorderStyle.SOLID,
  282. dashGap: 8.0
  283. }
  284. },
  285. arrow: {
  286. borderColor: '#1460F3',
  287. borderAlpha: 100,
  288. borderWidth: 5,
  289. borderStyle: {
  290. style: CPDFBorderStyle.SOLID,
  291. dashGap: 8.0
  292. },
  293. startLineType: CPDFLineType.NONE,
  294. tailLineType: CPDFLineType.OPEN_ARROW
  295. },
  296. freeText: {
  297. fontColor: '#000000',
  298. fontColorAlpha: 255,
  299. fontSize: 30,
  300. isBold: false,
  301. isItalic: false,
  302. alignment: CPDFAlignment.LEFT,
  303. typeface: CPDFTypeface.HELVETICA
  304. }
  305. }
  306. },
  307. contentEditorConfig: {
  308. availableTypes: [
  309. CPDFContentEditorType.EDITOR_TEXT,
  310. CPDFContentEditorType.EDITOR_IMAGE
  311. ],
  312. availableTools: [
  313. CPDFConfigTool.SETTING,
  314. CPDFConfigTool.UNDO,
  315. CPDFConfigTool.REDO
  316. ],
  317. initAttribute: {
  318. text: {
  319. fontColor: '#000000',
  320. fontColorAlpha: 255,
  321. fontSize: 30,
  322. isBold: false,
  323. isItalic: false,
  324. typeface: CPDFTypeface.HELVETICA,
  325. alignment: CPDFAlignment.LEFT
  326. }
  327. }
  328. },
  329. formsConfig: {
  330. availableTypes: [
  331. CPDFFormType.TEXT_FIELD,
  332. CPDFFormType.CHECKBOX,
  333. CPDFFormType.RADIO_BUTTON,
  334. CPDFFormType.LISTBOX,
  335. CPDFFormType.COMBOBOX,
  336. CPDFFormType.SIGNATURES_FIELDS,
  337. CPDFFormType.PUSH_BUTTON,
  338. ],
  339. availableTools: [
  340. CPDFConfigTool.UNDO,
  341. CPDFConfigTool.REDO
  342. ],
  343. initAttribute: {
  344. textField: {
  345. fillColor: '#DDE9FF',
  346. borderColor: '#1460F3',
  347. borderWidth: 2,
  348. fontColor: '#000000',
  349. fontSize: 20,
  350. isBold: false,
  351. isItalic: false,
  352. alignment: CPDFAlignment.LEFT,
  353. multiline: true,
  354. typeface: CPDFTypeface.HELVETICA
  355. },
  356. checkBox: {
  357. fillColor: '#DDE9FF',
  358. borderColor: '#1460F3',
  359. borderWidth: 2,
  360. checkedColor: '#43474D',
  361. isChecked: false,
  362. checkedStyle: CPDFCheckStyle.CHECK
  363. },
  364. radioButton: {
  365. fillColor: '#DDE9FF',
  366. borderColor: '#1460F3',
  367. borderWidth: 2,
  368. checkedColor: '#43474D',
  369. isChecked: false,
  370. checkedStyle: CPDFCheckStyle.CIRCLE
  371. },
  372. listBox: {
  373. fillColor: '#DDE9FF',
  374. borderColor: '#1460F3',
  375. borderWidth: 2,
  376. fontColor: '#000000',
  377. fontSize: 20,
  378. typeface: CPDFTypeface.HELVETICA,
  379. isBold: false,
  380. isItalic: false
  381. },
  382. comboBox: {
  383. fillColor: '#DDE9FF',
  384. borderColor: '#1460F3',
  385. borderWidth: 2,
  386. fontColor: '#000000',
  387. fontSize: 20,
  388. typeface: CPDFTypeface.HELVETICA,
  389. isBold: false,
  390. isItalic: false
  391. },
  392. pushButton: {
  393. fillColor: '#DDE9FF',
  394. borderColor: '#1460F3',
  395. borderWidth: 2,
  396. fontColor: '#000000',
  397. fontSize: 20,
  398. title: 'Button',
  399. typeface: CPDFTypeface.HELVETICA,
  400. isBold: false,
  401. isItalic: false
  402. },
  403. signaturesFields: {
  404. fillColor: '#DDE9FF',
  405. borderColor: '#000000',
  406. borderWidth: 2
  407. }
  408. }
  409. },
  410. readerViewConfig: {
  411. linkHighlight: true,
  412. formFieldHighlight: true,
  413. displayMode: CPDFDisplayMode.SINGLE_PAGE,
  414. continueMode: true,
  415. verticalMode: true,
  416. cropMode: false,
  417. themes: CPDFThemes.LIGHT,
  418. enableSliderBar: true,
  419. enablePageIndicator: true,
  420. pageScale: 1.0,
  421. margins: [0,0,0,0],
  422. pageSpacing: 10,
  423. pageSameWidth: true
  424. },
  425. global: {
  426. themeMode: CPDFThemeMode.SYSTEM,
  427. fileSaveExtraFontSubset: true
  428. }
  429. }
  430. return JSON.stringify(mergeDeep(defaultConfig, overrides), null, 2);
  431. }
  432. function mergeDeep(defaults: any, overrides: any): any {
  433. const merged = { ...defaults };
  434. for (const key in overrides) {
  435. if (Array.isArray(overrides[key]) && Array.isArray(defaults[key])) {
  436. merged[key] = [...overrides[key]];
  437. } else if (overrides[key] instanceof Object && key in defaults) {
  438. merged[key] = mergeDeep(defaults[key], overrides[key]);
  439. } else {
  440. merged[key] = overrides[key];
  441. }
  442. }
  443. return merged;
  444. }