cpdf_document.dart 5.1 KB

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