123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- import 'package:compdfkit_flutter/configuration/cpdf_options.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter/widgets.dart';
- /// A class to handle PDF documents without using [CPDFReaderWidget]
- ///
- /// example:
- /// ```dart
- /// var document = CPDFDocument();
- /// document.open('pdf file path', 'password');
- ///
- /// /// get pdf document info.
- /// var info = await document.getInfo();
- ///
- /// /// get pdf document file name.
- /// var fileName = await document.getFileName();
- /// ```
- class CPDFDocument {
- late MethodChannel _channel;
- bool _isValid = false;
- get isValid => _isValid;
- CPDFDocument.withController(int viewId)
- : _channel = MethodChannel('com.compdfkit.flutter.document_$viewId'),
- _isValid = true;
- Future<CPDFDocumentError> open(String filePath, String password) async {
- var errorCode = await _channel.invokeMethod(
- 'open_document', {'filePath': filePath, 'password': password});
- var error = CPDFDocumentError.values[errorCode];
- _isValid = error == CPDFDocumentError.success;
- return error;
- }
- /// Gets the file name of the PDF document.
- ///
- /// example:
- /// ```dart
- /// var fileName = await document.getFileName();
- /// ```
- Future<String> getFileName() async {
- return await _channel.invokeMethod('get_file_name');
- }
- /// Checks if the PDF document is encrypted.
- ///
- /// example:
- /// ```dart
- /// var isEncrypted = await document.isEncrypted();
- /// ```
- Future<bool> isEncrypted() async {
- return await _channel.invokeMethod('is_encrypted');
- }
- /// Checks if the PDF document is an image document.
- /// This is a time-consuming operation that depends on the document size.
- ///
- /// example:
- /// ```dart
- /// var isImageDoc = await document.isImageDoc();
- /// ```
- Future<bool> isImageDoc() async {
- return await _channel.invokeMethod('is_image_doc');
- }
- /// Gets the current document's permissions. There are three types of permissions:
- /// No restrictions: [CPDFDocumentPermissions.none]
- /// If the document has an open password and an owner password,
- /// using the open password will grant [CPDFDocumentPermissions.user] permissions,
- /// and using the owner password will grant [CPDFDocumentPermissions.owner] permissions.
- ///
- /// example:
- /// ```dart
- /// var permissions = await document.getPermissions();
- /// ```
- Future<CPDFDocumentPermissions> getPermissions() async {
- int permissionId = await _channel.invokeMethod('get_permissions');
- return CPDFDocumentPermissions.values[permissionId];
- }
- /// Check if owner permissions are unlocked
- ///
- /// example:
- /// ```dart
- /// var isUnlocked = await document.checkOwnerUnlocked();
- /// ```
- Future<bool> checkOwnerUnlocked() async {
- return await _channel.invokeMethod('check_owner_unlocked');
- }
- /// Whether the password is correct.
- ///
- /// example:
- /// ```dart
- /// var isCorrect = await document.checkPassword('password', isOwnerPassword: true);
- /// ```
- Future<bool> checkPassword(String password,
- {bool isOwnerPassword = false}) async {
- return await _channel.invokeMethod('check_password',
- {'password': password, 'isOwnerPassword': isOwnerPassword});
- }
- /// Check the document for modifications
- ///
- /// example:
- /// ```dart
- /// bool hasChange = await document.hasChange();
- /// ```
- Future<bool> hasChange() async {
- return await _channel.invokeMethod('has_change');
- }
- /// After completing the operation of the document,
- /// please close the document at the appropriate location to release resources.
- Future<void> close() async {
- if (!_isValid) return;
- await _channel.invokeMethod('close');
- debugPrint('ComPDFKit:CPDFDocument.close');
- _isValid = false;
- }
- /// Imports annotations from the specified XFDF file into the current PDF document.
- ///
- /// **Parameters:**<br/>
- /// xfdfFile - Path of the XFDF file to be imported.
- ///
- /// **example:**
- /// ```dart
- /// bool result = await document.importAnnotations(xxx.xfdf);
- /// ```
- ///
- /// **Returns:**
- /// true if the import is successful; otherwise, false.
- Future<bool> importAnnotations(String xfdfFile) async {
- return await _channel.invokeMethod('import_annotations', xfdfFile);
- }
- /// Exports annotations from the current PDF document to an XFDF file.
- ///
- /// **example:**
- /// ```dart
- /// String xfdfPath = await document.exportAnnotations();
- /// ```
- ///
- /// Returns:
- /// The path of the XFDF file if export is successful; an empty string if the export fails.
- Future<String> exportAnnotations() async {
- return await _channel.invokeMethod('export_annotations');
- }
- /// Delete all comments in the current document
- ///
- /// example:
- /// ```dart
- /// bool result = await document.removeAllAnnotations();
- /// ```
- Future<bool> removeAllAnnotations() async {
- return await _channel.invokeMethod('remove_all_annotations');
- }
- /// Get the total number of pages in the current document
- ///
- /// example:
- /// ```dart
- /// int pageCount = await document.getPageCount();
- /// ```
- Future<int> getPageCount() async {
- return await _channel.invokeMethod('get_page_count');
- }
- // Future<void> getInfo() async {}
- }
|