123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import 'dart:convert';
- import 'package:compdfkit_flutter/core/document/cpdf_outline_data.dart';
- import 'package:compdfkit_flutter/core/document/cpdf_permissions_info.dart';
- import 'package:compdfkit_flutter/core/document/textsearcher/cpdf_text_searcher.dart';
- import 'package:flutter/services.dart';
- import 'cpdf_bookmark.dart';
- import 'cpdf_document_error.dart';
- import 'cpdf_info.dart';
- /// Copyright © 2014-2023 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.
- class CPDFDocument {
- int _viewId = 0;
- late final MethodChannel _methodChannel;
- late final CPDFTextSearcher textSearcher;
- CPDFDocument(this._viewId) {
- _methodChannel =
- MethodChannel('com.compdfkit.flutter.pdfviewer.document.$_viewId');
- textSearcher = CPDFTextSearcher(_methodChannel);
- }
- CPDFDocument.create() : this(1000);
- Future<PDFDocumentError> open(String document, {String password = ''}) async {
- int errorCode = await _methodChannel
- .invokeMethod('openPDF', {'document': document, 'password': password});
- return PDFDocumentError.values
- .firstWhere((element) => element.index == errorCode);
- }
- /// Get the number of pages in a pdf file
- Future<int> getPageCount() async {
- return await _methodChannel.invokeMethod('getPageCount');
- }
- /// Get the page size of the specified page number in the pdf file
- Future<List<double>> getPageSize(int pageIndex) async {
- Map<dynamic, dynamic> map =
- await _methodChannel.invokeMethod('getPageSize', pageIndex);
- return Future(() => [map['width'], map['height']]);
- }
- /// Render the page with the specified page number and return the image
- Future<Uint8List> renderPageAtIndex(
- int pageIndex, int width, int height) async {
- Uint8List imageData = await _methodChannel.invokeMethod('renderPageAtIndex',
- {'pageIndex': pageIndex, 'width': width, 'height': height});
- return imageData;
- }
- Future<List<CPDFOutlineData>> getOutline() async {
- String outlineJson = await _methodChannel.invokeMethod('getOutline');
- List<dynamic> list = json.decode(outlineJson);
- List<CPDFOutlineData> datas = List.empty(growable: true);
- for (var value in list) {
- datas.add(CPDFOutlineData.formJson(value));
- }
- return datas;
- }
- /// Get pdf document information
- /// fileName : xxx.pdf
- /// fileSize : 1024
- /// title : xxx
- /// author : The name of the author of the document
- /// subject :
- /// creator : The name of the creator of the document
- /// producer :
- /// creationDate : Document Creation Timestamp
- /// modificationDate : Timestamp when the document was modified
- /// keywords :
- /// pageCount : Total number of document pages
- /// majorVersion :
- Future<CPDFInfo> getInfo() async {
- String infoJson = await _methodChannel.invokeMethod('getInfo');
- return CPDFInfo.formJson(json.decode(infoJson));
- }
- /// Get document permission information
- Future<CPDFPermissionsInfo> getPermissionsInfo() async {
- String permissionsInfo =
- await _methodChannel.invokeMethod('getPermissionsInfo');
- return CPDFPermissionsInfo.formJson(json.decode(permissionsInfo));
- }
- Future<List<CPDFBookmark>> getBookmarks() async {
- String bookmarkJson = await _methodChannel.invokeMethod('getBookmarks');
- if(bookmarkJson.isNotEmpty){
- List<dynamic> list = json.decode(bookmarkJson);
- return list.map((e) => CPDFBookmark.formJson(e)).toList();
- }else {
- return List.empty(growable: true);
- }
- }
- Future<bool> addBookmark(CPDFBookmark bookmark) async {
- return await _methodChannel.invokeMethod('addBookmark',
- {'title': bookmark.title, 'pageIndex': bookmark.pageIndex});
- }
- Future<bool> removeBookmark(int pageIndex) async {
- return await _methodChannel.invokeMethod('removeBookmark', pageIndex);
- }
- Future<bool> updateBookmark(int pageIndex, String title) async {
- return await _methodChannel.invokeMethod(
- 'updateBookmark', {'title': title, 'pageIndex': pageIndex});
- }
- Future<bool> hasBookmark(int pageIndex) async {
- return await _methodChannel.invokeMethod('hasBookmark', pageIndex);
- }
- }
|