cpdf_document.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import 'dart:convert';
  2. import 'package:compdfkit_flutter/core/document/cpdf_outline_data.dart';
  3. import 'package:compdfkit_flutter/core/document/cpdf_permissions_info.dart';
  4. import 'package:compdfkit_flutter/core/document/textsearcher/cpdf_text_searcher.dart';
  5. import 'package:flutter/services.dart';
  6. import 'cpdf_bookmark.dart';
  7. import 'cpdf_document_error.dart';
  8. import 'cpdf_info.dart';
  9. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  10. ///
  11. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  12. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  13. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  14. /// This notice may not be removed from this file.
  15. class CPDFDocument {
  16. int _viewId = 0;
  17. late final MethodChannel _methodChannel;
  18. late final CPDFTextSearcher textSearcher;
  19. CPDFDocument(this._viewId) {
  20. _methodChannel =
  21. MethodChannel('com.compdfkit.flutter.pdfviewer.document.$_viewId');
  22. textSearcher = CPDFTextSearcher(_methodChannel);
  23. }
  24. CPDFDocument.create() : this(1000);
  25. Future<PDFDocumentError> open(String document, {String password = ''}) async {
  26. int errorCode = await _methodChannel
  27. .invokeMethod('openPDF', {'document': document, 'password': password});
  28. return PDFDocumentError.values
  29. .firstWhere((element) => element.index == errorCode);
  30. }
  31. /// Get the number of pages in a pdf file
  32. Future<int> getPageCount() async {
  33. return await _methodChannel.invokeMethod('getPageCount');
  34. }
  35. /// Get the page size of the specified page number in the pdf file
  36. Future<List<double>> getPageSize(int pageIndex) async {
  37. Map<dynamic, dynamic> map =
  38. await _methodChannel.invokeMethod('getPageSize', pageIndex);
  39. return Future(() => [map['width'], map['height']]);
  40. }
  41. /// Render the page with the specified page number and return the image
  42. Future<Uint8List> renderPageAtIndex(
  43. int pageIndex, int width, int height) async {
  44. Uint8List imageData = await _methodChannel.invokeMethod('renderPageAtIndex',
  45. {'pageIndex': pageIndex, 'width': width, 'height': height});
  46. return imageData;
  47. }
  48. Future<List<CPDFOutlineData>> getOutline() async {
  49. String outlineJson = await _methodChannel.invokeMethod('getOutline');
  50. List<dynamic> list = json.decode(outlineJson);
  51. List<CPDFOutlineData> datas = List.empty(growable: true);
  52. for (var value in list) {
  53. datas.add(CPDFOutlineData.formJson(value));
  54. }
  55. return datas;
  56. }
  57. /// Get pdf document information
  58. /// fileName : xxx.pdf
  59. /// fileSize : 1024
  60. /// title : xxx
  61. /// author : The name of the author of the document
  62. /// subject :
  63. /// creator : The name of the creator of the document
  64. /// producer :
  65. /// creationDate : Document Creation Timestamp
  66. /// modificationDate : Timestamp when the document was modified
  67. /// keywords :
  68. /// pageCount : Total number of document pages
  69. /// majorVersion :
  70. Future<CPDFInfo> getInfo() async {
  71. String infoJson = await _methodChannel.invokeMethod('getInfo');
  72. return CPDFInfo.formJson(json.decode(infoJson));
  73. }
  74. /// Get document permission information
  75. Future<CPDFPermissionsInfo> getPermissionsInfo() async {
  76. String permissionsInfo =
  77. await _methodChannel.invokeMethod('getPermissionsInfo');
  78. return CPDFPermissionsInfo.formJson(json.decode(permissionsInfo));
  79. }
  80. Future<List<CPDFBookmark>> getBookmarks() async {
  81. String bookmarkJson = await _methodChannel.invokeMethod('getBookmarks');
  82. if(bookmarkJson.isNotEmpty){
  83. List<dynamic> list = json.decode(bookmarkJson);
  84. return list.map((e) => CPDFBookmark.formJson(e)).toList();
  85. }else {
  86. return List.empty(growable: true);
  87. }
  88. }
  89. Future<bool> addBookmark(CPDFBookmark bookmark) async {
  90. return await _methodChannel.invokeMethod('addBookmark',
  91. {'title': bookmark.title, 'pageIndex': bookmark.pageIndex});
  92. }
  93. Future<bool> removeBookmark(int pageIndex) async {
  94. return await _methodChannel.invokeMethod('removeBookmark', pageIndex);
  95. }
  96. Future<bool> updateBookmark(int pageIndex, String title) async {
  97. return await _methodChannel.invokeMethod(
  98. 'updateBookmark', {'title': title, 'pageIndex': pageIndex});
  99. }
  100. Future<bool> hasBookmark(int pageIndex) async {
  101. return await _methodChannel.invokeMethod('hasBookmark', pageIndex);
  102. }
  103. }