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