renderPage method

Future<Uint8List?> renderPage({
  1. int pageIndex = 0,
  2. int? x,
  3. int? y,
  4. int? width,
  5. int? height,
  6. double? scale,
  7. Color background = const Color(0xFFFFFFFF),
})

Converts a page with the given pageIndex to a bitmap.

Optionally crop the output image to a given x and y coordinate with a given width, height.

With the scale argument you can control the output resolution. Default scale is 1 which means that the output image has exactly the size of the PDF.

Optionally set the background color which will be used instead of transparency.

Implementation

Future<Uint8List?> renderPage({
  int pageIndex = 0,
  int? x,
  int? y,
  int? width,
  int? height,
  double? scale,
  Color background = const Color(0xFFFFFFFF),
}) async {
  if (_id == null) throw StateError('Please open the PDF first!');

  // Check if the page at given index is already open.
  // If not, auto-open and auto-close the page at given index.
  final autoOpenClosePage = !_pages.contains(pageIndex);

  if (autoOpenClosePage) await openPage(pageIndex: pageIndex);

  final bytes = await PdfImageRenderer.renderPDFPage(
    pdf: _id!,
    page: pageIndex,
    x: x,
    y: y,
    width: width,
    height: height,
    scale: scale,
    background: background,
  );

  if (autoOpenClosePage) await closePage(pageIndex: pageIndex);

  return bytes;
}