compdfkit.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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:io';
  8. import 'package:compdfkit_flutter/configuration/cpdf_configuration.dart';
  9. import 'package:flutter/cupertino.dart';
  10. import 'package:flutter/services.dart';
  11. /// ComPDFKit plugin to load PDF and image documents on both platform iOS and Android.
  12. class ComPDFKit {
  13. static const MethodChannel _methodChannel = MethodChannel('com.compdfkit.flutter.plugin');
  14. /// Please enter your ComPDFKit license to initialize the ComPDFKit SDK.<br/>
  15. /// This method is used for offline license authentication.
  16. /// In version **1.13.0**, we have introduced a brand-new online authentication licensing scheme.
  17. /// By default, the ComPDFKit SDK performs online authentication.
  18. /// If you are unsure about the type of your license, please contact the [ComPDFKit team.](https://www.compdf.com/support).
  19. ///
  20. /// **samples:**<br/>
  21. /// ```dart
  22. /// ComPDFKit.init('your compdfkit license')
  23. /// ```
  24. static void init(String key) async {
  25. _methodChannel.invokeMethod('init_sdk', {'key': key});
  26. }
  27. /// Please enter your ComPDFKit license to initialize the ComPDFKit SDK.<br/>
  28. /// This method is used for online license authentication
  29. /// In version **1.13.0**, we have introduced a brand-new online authentication licensing scheme.
  30. /// By default, the ComPDFKit SDK performs online authentication.
  31. /// If you obtained your ComPDFKit License before the release of version 1.13.0, please use [ComPDFKit.init] <br/>
  32. /// If you are unsure about the type of your license, please contact the [ComPDFKit team.](https://www.compdf.com/support).
  33. ///
  34. /// **samples:**<br/>
  35. /// **online auth**
  36. /// ```dart
  37. /// ComPDFKit.initialize(androidOnlineLicense : 'your android platform compdfkit license', iosOnlineLicense: 'your ios platform compdfkit license')
  38. /// ```
  39. static void initialize({required String androidOnlineLicense,required String iosOnlineLicense}) {
  40. _methodChannel.invokeMethod('init_sdk_keys', {'androidOnlineLicense': androidOnlineLicense, 'iosOnlineLicense': iosOnlineLicense});
  41. }
  42. /// Get the version code of the ComPDFKit SDK.
  43. static Future<String> getVersionCode() async {
  44. String versionCode =
  45. await _methodChannel.invokeMethod('sdk_version_code');
  46. return versionCode;
  47. }
  48. /// Get the version information of ComPDFKit SDK.
  49. static Future<String> getSDKBuildTag() async {
  50. String buildTag = await _methodChannel.invokeMethod('sdk_build_tag');
  51. return buildTag;
  52. }
  53. /// Enter the local PDF file path, document password (if required),
  54. /// and configuration parameters, and display the PDF document in a new window.
  55. ///
  56. /// **for Samples:**
  57. /// ```dart
  58. /// ComPDFKit.openDocument(
  59. /// 'xxx/compdfkit.pdf',
  60. /// password : '',
  61. /// configuration:CPDFConfiguration())
  62. /// ```
  63. static void openDocument(String document,
  64. {String? password, CPDFConfiguration? configuration}) async {
  65. await _methodChannel.invokeMethod('open_document', <String, dynamic>{
  66. 'document': document,
  67. 'password': password,
  68. 'configuration': configuration?.toJson()
  69. });
  70. }
  71. /// Retrieve the path of your operating system's temporary directory.
  72. /// Support [Android] and [iOS] only for now.
  73. static Future<Directory> getTemporaryDirectory() async {
  74. final String? path =
  75. await _methodChannel.invokeMethod('get_temporary_directory');
  76. if (path == null) {
  77. throw Exception('Unable to get temporary directory');
  78. }
  79. return Directory(path);
  80. }
  81. /// Delete annotated electronic signature file list data
  82. /// **for Samples:**
  83. /// ```dart
  84. /// ComPDFKit.removeSignFileList()
  85. /// ```
  86. static Future<bool> removeSignFileList() async {
  87. return await _methodChannel.invokeMethod('remove_sign_file_list');
  88. }
  89. static Future<String?> pickFile() async {
  90. final String? filePath = await _methodChannel.invokeMethod('pick_file');
  91. return filePath;
  92. }
  93. /// This method is supported only on the Android platform. It is used to create a URI for saving a file on the Android device.
  94. /// The file is saved in the `Downloads` directory by default, but you can specify a subdirectory within `Downloads` using the
  95. /// [childDirectoryName] parameter. If the [childDirectoryName] is not provided, the file will be saved directly in the `Downloads` directory.
  96. /// The [fileName] parameter is required to specify the name of the file (e.g., `test.pdf`).
  97. ///
  98. /// Example usage:
  99. /// ```dart
  100. /// String? uri = await ComPDFKit.createUri('test.pdf');
  101. /// ```
  102. ///
  103. /// - [fileName] (required) specifies the name of the file, for example `test.pdf`.
  104. /// - [childDirectoryName] (optional) specifies a subdirectory within the `Downloads` folder.
  105. /// - [mimeType] (optional) is the MIME type of the file, defaulting to `application/pdf`.
  106. static Future<String?> createUri(
  107. String fileName,
  108. {String? childDirectoryName,
  109. String mimeType = 'application/pdf'}) async {
  110. final String? uri = await _methodChannel.invokeMethod('create_uri', {
  111. 'file_name' : fileName,
  112. 'child_directory_name' : childDirectoryName,
  113. 'mime_type' : mimeType
  114. });
  115. return uri;
  116. }
  117. /// Import font directory and configure whether to include system fonts.
  118. ///
  119. /// - Parameters:
  120. /// - **dirPath**: The directory path where the font files are stored.
  121. /// The imported fonts will be available for selection in text annotations and content editing, resolving issues with missing text in certain languages.
  122. /// - **addSysFont**: Whether to include system fonts. Default is `true`, which will show system fonts in the font list.
  123. ///
  124. /// Note:
  125. /// This method must be called before [ComPDFKit.init] or [ComPDFKit.initialize], otherwise the settings will have no effect.
  126. ///
  127. /// Example:
  128. /// ```dart
  129. /// ComPDFKit.setImportFontDir('path/to/your/font', addSysFont: true);
  130. /// ComPDF
  131. static Future<bool> setImportFontDir(String dirPath, {bool addSysFont = true}) async {
  132. try{
  133. return await _methodChannel.invokeMethod('set_import_font_directory', {
  134. 'dir_path': dirPath,
  135. 'add_sys_font': addSysFont
  136. });
  137. }catch(e){
  138. debugPrint("setImportFontDir error: $e");
  139. return false;
  140. }
  141. }
  142. static Future<bool> createDocumentInstance(String id) async {
  143. return await _methodChannel.invokeMethod('create_document_plugin', id);
  144. }
  145. }