cpdf_document.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 'package:compdfkit_flutter/configuration/cpdf_options.dart';
  8. import 'package:flutter/services.dart';
  9. import 'package:flutter/widgets.dart';
  10. /// A class to handle PDF documents without using [CPDFReaderWidget]
  11. ///
  12. /// example:
  13. /// ```dart
  14. /// var document = CPDFDocument();
  15. /// document.open('pdf file path', 'password');
  16. ///
  17. /// /// get pdf document info.
  18. /// var info = await document.getInfo();
  19. ///
  20. /// /// get pdf document file name.
  21. /// var fileName = await document.getFileName();
  22. /// ```
  23. class CPDFDocument {
  24. late MethodChannel _channel;
  25. bool _isValid = false;
  26. get isValid => _isValid;
  27. CPDFDocument.withController(int viewId)
  28. : _channel = MethodChannel('com.compdfkit.flutter.document_$viewId'),
  29. _isValid = true;
  30. Future<CPDFDocumentError> open(String filePath, String password) async {
  31. var errorCode = await _channel.invokeMethod(
  32. 'open_document', {'filePath': filePath, 'password': password});
  33. var error = CPDFDocumentError.values[errorCode];
  34. _isValid = error == CPDFDocumentError.success;
  35. return error;
  36. }
  37. /// Gets the file name of the PDF document.
  38. ///
  39. /// example:
  40. /// ```dart
  41. /// var fileName = await document.getFileName();
  42. /// ```
  43. Future<String> getFileName() async {
  44. return await _channel.invokeMethod('get_file_name');
  45. }
  46. /// Checks if the PDF document is encrypted.
  47. ///
  48. /// example:
  49. /// ```dart
  50. /// var isEncrypted = await document.isEncrypted();
  51. /// ```
  52. Future<bool> isEncrypted() async {
  53. return await _channel.invokeMethod('is_encrypted');
  54. }
  55. /// Checks if the PDF document is an image document.
  56. /// This is a time-consuming operation that depends on the document size.
  57. ///
  58. /// example:
  59. /// ```dart
  60. /// var isImageDoc = await document.isImageDoc();
  61. /// ```
  62. Future<bool> isImageDoc() async {
  63. return await _channel.invokeMethod('is_image_doc');
  64. }
  65. /// Gets the current document's permissions. There are three types of permissions:
  66. /// No restrictions: [CPDFDocumentPermissions.none]
  67. /// If the document has an open password and an owner password,
  68. /// using the open password will grant [CPDFDocumentPermissions.user] permissions,
  69. /// and using the owner password will grant [CPDFDocumentPermissions.owner] permissions.
  70. ///
  71. /// example:
  72. /// ```dart
  73. /// var permissions = await document.getPermissions();
  74. /// ```
  75. Future<CPDFDocumentPermissions> getPermissions() async {
  76. int permissionId = await _channel.invokeMethod('get_permissions');
  77. return CPDFDocumentPermissions.values[permissionId];
  78. }
  79. /// Check if owner permissions are unlocked
  80. ///
  81. /// example:
  82. /// ```dart
  83. /// var isUnlocked = await document.checkOwnerUnlocked();
  84. /// ```
  85. Future<bool> checkOwnerUnlocked() async {
  86. return await _channel.invokeMethod('check_owner_unlocked');
  87. }
  88. /// Whether the password is correct.
  89. ///
  90. /// example:
  91. /// ```dart
  92. /// var isCorrect = await document.checkPassword('password', isOwnerPassword: true);
  93. /// ```
  94. Future<bool> checkPassword(String password,
  95. {bool isOwnerPassword = false}) async {
  96. return await _channel.invokeMethod('check_password',
  97. {'password': password, 'isOwnerPassword': isOwnerPassword});
  98. }
  99. /// Check the document for modifications
  100. ///
  101. /// example:
  102. /// ```dart
  103. /// bool hasChange = await document.hasChange();
  104. /// ```
  105. Future<bool> hasChange() async {
  106. return await _channel.invokeMethod('has_change');
  107. }
  108. /// After completing the operation of the document,
  109. /// please close the document at the appropriate location to release resources.
  110. Future<void> close() async {
  111. if (!_isValid) return;
  112. await _channel.invokeMethod('close');
  113. debugPrint('ComPDFKit:CPDFDocument.close');
  114. _isValid = false;
  115. }
  116. /// Imports annotations from the specified XFDF file into the current PDF document.
  117. ///
  118. /// **Parameters:**<br/>
  119. /// xfdfFile - Path of the XFDF file to be imported.
  120. ///
  121. /// **example:**
  122. /// ```dart
  123. /// bool result = await document.importAnnotations(xxx.xfdf);
  124. /// ```
  125. ///
  126. /// **Returns:**
  127. /// true if the import is successful; otherwise, false.
  128. Future<bool> importAnnotations(String xfdfFile) async {
  129. return await _channel.invokeMethod('import_annotations', xfdfFile);
  130. }
  131. /// Exports annotations from the current PDF document to an XFDF file.
  132. ///
  133. /// **example:**
  134. /// ```dart
  135. /// String xfdfPath = await document.exportAnnotations();
  136. /// ```
  137. ///
  138. /// Returns:
  139. /// The path of the XFDF file if export is successful; an empty string if the export fails.
  140. Future<String> exportAnnotations() async {
  141. return await _channel.invokeMethod('export_annotations');
  142. }
  143. /// Delete all comments in the current document
  144. ///
  145. /// example:
  146. /// ```dart
  147. /// bool result = await document.removeAllAnnotations();
  148. /// ```
  149. Future<bool> removeAllAnnotations() async {
  150. return await _channel.invokeMethod('remove_all_annotations');
  151. }
  152. /// Get the total number of pages in the current document
  153. ///
  154. /// example:
  155. /// ```dart
  156. /// int pageCount = await document.getPageCount();
  157. /// ```
  158. Future<int> getPageCount() async {
  159. return await _channel.invokeMethod('get_page_count');
  160. }
  161. // Future<void> getInfo() async {}
  162. }