cpdf_document.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import 'dart:convert';
  2. import 'dart:ffi';
  3. import 'dart:ui' as ui;
  4. import 'package:compdfkit_flutter/models/cpdf_outline_data.dart';
  5. import 'package:flutter/services.dart';
  6. import 'cpdf_document_error.dart';
  7. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  8. ///
  9. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  10. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  11. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  12. /// This notice may not be removed from this file.
  13. class CPDFDocument {
  14. int _viewId = 0;
  15. late final MethodChannel _methodChannel;
  16. CPDFDocument(this._viewId) {
  17. _methodChannel =
  18. MethodChannel('com.compdfkit.flutter.pdfviewer.document.$_viewId');
  19. }
  20. CPDFDocument.create() : this(1000);
  21. Future<PDFDocumentError> open(String document, {String password = ''}) async {
  22. int errorCode = await _methodChannel.invokeMethod('openPDF',{
  23. 'document' : document,
  24. 'password' : password
  25. });
  26. return PDFDocumentError.values.firstWhere((element) => element.index == errorCode);
  27. }
  28. /// Get the number of pages in a pdf file
  29. Future<int> getPageCount() async {
  30. return await _methodChannel.invokeMethod('getPageCount');
  31. }
  32. /// Get the page size of the specified page number in the pdf file
  33. Future<List<double>> getPageSize(int pageIndex) async {
  34. Map<dynamic, dynamic> map =
  35. await _methodChannel.invokeMethod('getPageSize', pageIndex);
  36. return Future(() => [map['width'], map['height']]);
  37. }
  38. /// Render the page with the specified page number and return the image
  39. Future<Uint8List> renderPageAtIndex(
  40. int pageIndex, int width, int height) async {
  41. Uint8List imageData = await _methodChannel.invokeMethod('renderPageAtIndex',
  42. {'pageIndex': pageIndex, 'width': width, 'height': height});
  43. return imageData;
  44. }
  45. Future<List<CPDFOutlineData>> getOutline() async {
  46. String outlineJson = await _methodChannel.invokeMethod('getOutline');
  47. List<dynamic> list = json.decode(outlineJson);
  48. List<CPDFOutlineData> datas = List.empty(growable: true);
  49. for (var value in list) {
  50. datas.add(CPDFOutlineData.formJson(value));
  51. }
  52. return datas;
  53. }
  54. }