Skip to content

Render Image

This API allows rendering a specified PDF page as an image. It is useful for generating previews or exporting page content.

The returned data type is base64-encoded, which can be used directly for display or saving as an image file.

Method Declaration

tsx
renderPage({
        pageIndex,
        width,
        height,
        backgroundColor = '#FFFFFF',
        drawAnnot = true,
        drawForm = true,
        pageCompression = CPDFPageCompression.PNG
    } : {
        pageIndex: number,
        width: number,
        height: number,
        backgroundColor?: HexColor,
        drawAnnot?: boolean,
        drawForm?: boolean,
        pageCompression?: CPDFPageCompression
    })

Parameters

ParameterTypeRequiredDescription
pageIndexintYesThe index of the page to render, starting from 0
widthintYesWidth of the rendered image in pixels
heightintYesHeight of the rendered image in pixels
backgroundColorColorNoPage background color, default is white, Android only
drawAnnotboolNoWhether to render annotations, default true, Android only
drawFormboolNoWhether to render form fields, default true, Android only
pageCompressionCPDFPageCompressionNoRendering format, png, jpeg

Example Usage

tsx
// Get the size of the specified page
const size = await pdfReaderRef.current?._pdfDocument.getPageSize(pageIndex);
// Render the page as an image and return a base64 string
const image = await pdfReaderRef.current?._pdfDocument.renderPage({
  pageIndex,
  width: size.width,
  height: size.height,
  backgroundColor: '#FFFFFF',
  drawAnnot: true,
  drawForm: true,
});

// Displaying rendered images in React Native
<Image  source={{ uri: CPDFImageUtil.base64ToUri(image) }} 
  style={{ width: '90%', height: '90%', resizeMode: 'contain', alignSelf: 'center' }}/>