cpdf_document.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. Future<void> getInfo() async {}
  117. }