Skip to content

PDF 渲染 (Render)

福昕 PDF SDK 使用强大的福昕渲染引擎,将 PDF 页面高效渲染到位图或平台设备上下文。通过 SDK 提供的 API,您可以灵活设置渲染选项,如控制表单域和签名渲染、开启图像和路径反锯齿 (anti-aliasing) 等。以下将以 JAVA 为例介绍如何快速使用这些渲染API。

渲染 API

  • 页面和注释渲染:
    • 使用 Renderer.setRenderContentFlags 接口设置渲染选项,决定是否同时渲染页面和注释。
    • 使用 Renderer.startRender 接口执行渲染。
    • 使用 Renderer.startQuickRender 接口用于快速渲染,仅适用于生成缩略图。
  • 单个注释渲染:
  • 位图渲染:
  • 重排页面渲染:

Widget 注释渲染

Widget 注释通常与 PDF 表单域和控件关联。推荐使用以下流程渲染 Widget 注释:

  1. 初始页面渲染:
    • 加载 PDF 页面后,使用 Renderer.startRender 渲染页面及其所有注释(包括 Widget 注释)。
  2. 表单填充渲染:
    • 使用 com.foxit.sdk.pdf.interform.Filler 对象填充表单时,应使用 pdf.interform.Filler.render 接口渲染当前获取焦点的表单控件,而不是 Renderer.renderAnnot 接口。

如何将 PDF 页面渲染到 bitmap

c++
#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
#include "include/common/fs_render.h"
using namespace foxit;
using namespace common;
using namespace pdf;
using namespace foxit::common;
// Assuming PDFPage page has been loaded and parsed.

int width = static_cast<int>(page.GetWidth());
int height = static_cast<int>(page.GetHeight());
Matrix matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());

// Prepare a bitmap for rendering.
Bitmap bitmap(width, height, Bitmap::e_DIBArgb, NULL, 0);
bitmap.FillRect(0xFFFFFFFF, NULL);
// Render page.
Renderer render(bitmap, false);
render.StartRender(page, matrix, NULL);
c
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfpage_c.h"
#include "include/fs_render_c.h"
// Assuming FS_PDFPAGE_HANDLE page has been loaded and parsed.

float fWidth;
float fHeight;
FSDK_PDFPage_GetWidth(page, &fWidth);
FSDK_PDFPage_GetHeight(page, &fHeight);
int width = (int)fWidth;
int height = (int)fHeight;

FSMatrix matrix;
FSRotation rotation;
FSDK_PDFPage_GetRotation(page, &rotation);
FSDK_PDFPage_GetDisplayMatrix(page, 0, 0, width, height, rotation, &matrix);

// Prepare a bitmap for rendering.
FS_BITMAP_HANDLE bitmap;
FSDK_Bitmap_Create(width, height, e_FSDIBArgb, NULL, 0, &bitmap);
FSDK_Bitmap_FillRect(bitmap, 0xFFFFFFFF, NULL);
// Render page.
FS_RENDERER_HANDLE renderer;
FSDK_Renderer_Create(bitmap, false, &renderer);
FS_PROGRESSIVE_HANDLE progreessive;
FSDK_Renderer_StartRender(renderer, page, matrix, NULL, &progreessive);
...
java
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.common.Renderer;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import static com.foxit.sdk.common.Bitmap.*e\_DIBArgb*;

// Assuming PDFPage page has been loaded and parsed.
int width = (int) page.getWidth();
int height = (int) page.getHeight();
Matrix2D matrix = page.getDisplayMatrix(0, 0, width, height, page.getRotation());

// Prepare a bitmap for rendering.
Bitmap bitmap = new Bitmap(width, height, *e\_DIBArgb*, null, 0);
bitmap.fillRect(0xFFFFFFFF, null);

// Render page.
Renderer render = new Renderer(bitmap, false);
render.startRender(page, matrix, null);
...
py
import sys
import site

if sys.version_info.major == 2:
    _PYTHON2_ = True
else:
    _PYTHON2_ = False

if _PYTHON2_:
    # replace with the python2 lib path
    site.addsitedir(‘../../../’)
    from FoxitPDFSDKPython2 import *
else:
    from FoxitPDFSDKPython3 import *

# Assuming PDFPage page has been loaded and parsed.

width = int(page.GetWidth())
height = int(page.GetHeight())
matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation())

# Prepare a bitmap for rendering.
bitmap = Bitmap(width, height, Bitmap.e_DIBArgb)
bitmap.FillRect(0xFFFFFFFF, None)
# Render page.
render = Renderer(bitmap, False)
render.StartRender(page, matrix, None)
...
objc
#include "FSPDFObjC.h"
...

// Assuming FSPDFPage page has been loaded and parsed.

int width = (int)[page getWidth];
int height = (int)[page getHeight];
FSMatrix2D* matrix = [page getDisplayMatrix:0 top:0 width:width height:height rotate:page.rotation];
   
// Prepare a bitmap for rendering.
FSBitmap* bitmap = [[FSBitmap alloc] initWithWidth:width height:height format:FSBitmapDIBArgb buffer:nil pitch:0];
[bitmap fillRect:0xFFFFFFFF rect:nil];
   
// Render page.
FSRenderer* render = [[FSRenderer alloc] initWithBitmap:bitmap is_rgb_order:NO];
[render startRender:page matrix:matrix pause:nil];
...
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");

// Assuming PDFPage page has been loaded and parsed.

let width = page.GetWidth();
let height = page.GetHeight(); 
let matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());

// Prepare a bitmap for rendering.
let bitmap = new FSDK.Bitmap(bitmap_width, bitmap_height, FSDK.Bitmap.e_DIBArgb, null, 0);
bitmap.FillRect(0xFFFFFF, null);
// Render page.
let render = new FSDK.Renderer(bitmap, false);
render.StartRender(page, matrix, null);
...
csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
// Assuming PDFPage page has been loaded and parsed.

int width = (int)(page.GetWidth());
int height = (int)(page.GetHeight());
Matrix2D matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());

// Prepare a bitmap for rendering.
foxit.common.Bitmap bitmap = new foxit.common.Bitmap(width, height, foxit.common.Bitmap.DIBFormat.e_DIBArgb, System.IntPtr.Zero, 0);
bitmap.FillRect(0xFFFFFFFF, null);

// Render page.
Renderer render = new Renderer(bitmap, false);
render.StartRender(page, matrix, null);
...

如何渲染页面和注释

c++
#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
#include "include/common/fs_render.h"
using namespace foxit;
using namespace common;
using namespace pdf;
using namespace foxit::common;
// Assuming PDFPage page has been loaded and parsed.
int width = static_cast<int>(page.GetWidth());
int height = static_cast<int>(page.GetHeight());
Matrix matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());

// Prepare a bitmap for rendering.
Bitmap bitmap(width, height, Bitmap::e_DIBArgb, NULL, 0);
bitmap.FillRect(0xFFFFFFFF, NULL);

Renderer render(bitmap, false);
uint32 dwRenderFlag = Renderer::e_RenderAnnot | Renderer::e_RenderPage;
render.SetRenderContentFlags(dwRenderFlag);
render.StartRender(page, matrix, NULL);
...
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfpage_c.h"
#include "include/fs_render_c.h"
// Assuming FS_PDFPAGE_HANDLE page has been loaded and parsed.
float fWidth;
float fHeight;
FSDK_PDFPage_GetWidth(page, &fWidth);
FSDK_PDFPage_GetHeight(page, &fHeight);
int width = (int)fWidth;
int height = (int)fHeight;
FSMatrix matrix;
FSRotation rotation;
FSDK_PDFPage_GetRotation(page, &rotation);
FSDK_PDFPage_GetDisplayMatrix(page, 0, 0, width, height, rotation, &matrix);

// Prepare a bitmap for rendering.
FS_BITMAP_HANDLE bitmap;
FSDK_Bitmap_Create(width, height, e_FSDIBArgb, NULL, 0, &bitmap);
FSDK_Bitmap_FillRect(bitmap, 0xFFFFFFFF, NULL);
FS_RENDERER_HANDLE renderer;
FSDK_Renderer_Create(bitmap, false, &renderer);
FS_UINT32 dwRenderFlag = e_FSRenderAnnot | e_FSRenderPage;
FSDK_Renderer_SetRenderContentFlags(render, dwRenderFlag);
FS_PROGRESSIVE_HANDLE progreessive;
FSDK_Renderer_StartRender(render, page, matrix, NULL, &progreessive);
...
java
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.common.Renderer;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import static com.foxit.sdk.common.Bitmap.*e\_DIBArgb*;

// Assuming PDFPage page has been loaded and parsed.
int width = (int) page.getWidth();
int height = (int) page.getHeight();
Matrix2D matrix = page.getDisplayMatrix(0, 0, width, height, page.getRotation());

// Prepare a bitmap for rendering.
Bitmap bitmap = new Bitmap(width, height, *e\_DIBArgb*, null, 0);
bitmap.fillRect(0xFFFFFFFF, null);
Renderer render(bitmap, false);
render.setRenderContentFlags(*e\_RenderAnnot* | *e\_RenderPage*);
render.startRender(page, matrix, null);
...
py
import sys
import site

if sys.version_info.major == 2:
    _PYTHON2_ = True
else:
    _PYTHON2_ = False

if _PYTHON2_:
    # replace with the python2 lib path
    site.addsitedir(‘../../../’)
    from FoxitPDFSDKPython2 import *
else:
    from FoxitPDFSDKPython3 import *

# Assuming PDFPage page has been loaded and parsed.

width = int(page.GetWidth())
height = int(page.GetHeight())
matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation())

# Prepare a bitmap for rendering.
bitmap = Bitmap(width, height, Bitmap.e_DIBArgb)
bitmap.FillRect(0xFFFFFFFF, None)

render = Renderer(bitmap, False)
dwRenderFlag = Renderer.e_RenderAnnot | Renderer.e_RenderPage
render.SetRenderContentFlags(dwRenderFlag)
render.StartRender(page, matrix, None)
...
objc
#include "FSPDFObjC.h"
...

// Assuming PDFPage page has been loaded and parsed.

int width = (int)[page getWidth];
int height = (int)[page getHeight];
FSMatrix2D* matrix = [page getDisplayMatrix:0 top:0 width:width height:height rotate:page.rotation];
   
// Prepare a bitmap for rendering.
FSBitmap* bitmap = [[FSBitmap alloc] initWithWidth:width height:height format:FSBitmapDIBArgb buffer:nil pitch:0];
[bitmap fillRect:0xFFFFFFFF rect:nil];

// Render page.
FSRenderer* render = [[FSRenderer alloc] initWithBitmap:bitmap is_rgb_order:NO];
unsigned int render_flag = FSRendererRenderPage | FSRendererRenderAnnot;
[render setRenderContentFlags:render_flag];
[render startRender:page matrix:matrix pause:nil];
...
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");

// Assuming PDFPage page has been loaded and parsed.

let width = page.GetWidth();
let height = page.GetHeight(); 
let matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());

// Prepare a bitmap for rendering.
let bitmap = new FSDK.Bitmap(bitmap_width, bitmap_height, FSDK.Bitmap.e_DIBArgb, null, 0);
bitmap.FillRect(0xFFFFFF, null);

let render = new FSDK.Renderer(bitmap, false);
let dwRenderFlag = FSDK.Renderer.e_RenderAnnot | FSDK.Renderer.e_RenderPage
render.SetRenderContentFlags(dwRenderFlag)
render.StartRender(page, matrix, null)
...
csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;

// Assuming PDFPage page has been loaded and parsed.
...

int width = (int)(page.GetWidth());
int height = (int)(page.GetHeight());
Matrix2D matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());

// Prepare a bitmap for rendering.
foxit.common.Bitmap bitmap = new foxit.common.Bitmap(width, height, foxit.common.Bitmap.DIBFormat.e_DIBArgb, System.IntPtr.Zero, 0);
bitmap.FillRect(0xFFFFFFFF, null);

// Render page
Renderer render = new Renderer(bitmap, false);
render.SetRenderContentFlags((int)Renderer.ContentFlag.e_RenderAnnot | (int)Renderer.ContentFlag.e_RenderPage);
render.StartRender(page, matrix, null);
...