Skip to content

PDF 页面转图片

此接口用于将指定的 PDF 页面渲染为图片,可用于生成预览图或导出页面内容。

返回的数据类型为 base64-encoded,可直接用于显示或保存为图片文件。

接口声明

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
    })

参数说明

参数名类型必填描述
pageIndexint需要渲染的页面索引,从 0 开始
widthint渲染图片的宽度(像素)
heightint渲染图片的高度(像素)
backgroundColorColor页面背景颜色,默认白色,仅支持Android平台
drawAnnotbool是否渲染注释,默认 true,仅支持Android平台
drawFormbool是否渲染表单,默认 true,仅支持Android平台
pageCompressionCPDFPageCompression渲染的格式,png, jpeg

使用示例

tsx
// 获取指定页码的尺寸
const size = await pdfReaderRef.current?._pdfDocument.getPageSize(pageIndex);
// 渲染页面为图片,返回base64字符串
const image = await pdfReaderRef.current?._pdfDocument.renderPage({
  pageIndex,
  width: size.width,
  height: size.height,
  backgroundColor: '#FFFFFF',
  drawAnnot: true,
  drawForm: true,
});

// 在 React Native 中显示渲染的图片
<Image  source={{ uri: CPDFImageUtil.base64ToUri(image) }} 
  style={{ width: '90%', height: '90%', resizeMode: 'contain', alignSelf: 'center' }}/>