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 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 getPageCount() async { return await _methodChannel.invokeMethod('getPageCount'); } /// Get the page size of the specified page number in the pdf file Future> getPageSize(int pageIndex) async { Map 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 renderPageAtIndex( int pageIndex, int width, int height) async { Uint8List imageData = await _methodChannel.invokeMethod('renderPageAtIndex', {'pageIndex': pageIndex, 'width': width, 'height': height}); return imageData; } Future> getOutline() async { String outlineJson = await _methodChannel.invokeMethod('getOutline'); List list = json.decode(outlineJson); List 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 getInfo() async { String infoJson = await _methodChannel.invokeMethod('getInfo'); return CPDFInfo.formJson(json.decode(infoJson)); } /// Get document permission information Future getPermissionsInfo() async { String permissionsInfo = await _methodChannel.invokeMethod('getPermissionsInfo'); return CPDFPermissionsInfo.formJson(json.decode(permissionsInfo)); } Future> getBookmarks() async { String bookmarkJson = await _methodChannel.invokeMethod('getBookmarks'); if(bookmarkJson.isNotEmpty){ List list = json.decode(bookmarkJson); return list.map((e) => CPDFBookmark.formJson(e)).toList(); }else { return List.empty(growable: true); } } Future addBookmark(CPDFBookmark bookmark) async { return await _methodChannel.invokeMethod('addBookmark', {'title': bookmark.title, 'pageIndex': bookmark.pageIndex}); } Future removeBookmark(int pageIndex) async { return await _methodChannel.invokeMethod('removeBookmark', pageIndex); } Future updateBookmark(int pageIndex, String title) async { return await _methodChannel.invokeMethod( 'updateBookmark', {'title': title, 'pageIndex': pageIndex}); } Future hasBookmark(int pageIndex) async { return await _methodChannel.invokeMethod('hasBookmark', pageIndex); } }