cpdf_document.dart 1.8 KB

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