PDF Rendering
Foxit PDF SDK uses the Foxit rendering engine to render PDF pages efficiently to bitmaps or platform device contexts. The SDK APIs let you configure rendering options such as form field and signature rendering, image anti-aliasing, path anti-aliasing, and more. The sections below use Java as an example for quick reference.
Rendering APIs
- Page and annotation rendering:
- Use
Renderer.setRenderContentFlagsto set rendering options and control whether the page and annotations are rendered together. - Use
Renderer.startRenderto perform rendering. - Use
Renderer.startQuickRenderfor fast rendering when generating thumbnails only.
- Use
- Single annotation rendering:
- Use Renderer.renderAnnot to render a specific annotation.
- Bitmap rendering:
- Use Renderer.startRenderBitmap to render to a bitmap.
- Reflow page rendering:
- Use Renderer.startRenderReflowPage to render a reflowed page.
Widget annotation rendering
Widget annotations are typically associated with PDF form fields and controls. The recommended workflow is:
- Initial page render:
- After loading a PDF page, use
Renderer.startRenderto render the page and all annotations, including Widget annotations.
- After loading a PDF page, use
- Form filling render:
- When filling a form with a
com.foxit.sdk.pdf.interform.Fillerobject, usepdf.interform.Filler.renderto render the form control that currently has focus, notRenderer.renderAnnot.
- When filling a form with a
Render a PDF page to a 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("@fuxinsoft/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);
...go
package main
import (
. “foxit.com/fsdk”
)
# 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 := NewBitmap(width, height, BitmapE_DIBArgb)
defer DeleteBitmap(bitmap)
bitmap.FillRect(uint(0xFFFFFFFF))
# Render page.
render := NewRenderer(bitmap, false)
render.StartRender(page, matrix)
...Render a page and its annotations
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("@fuxinsoft/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);
...go
package main
import (
. “foxit.com/fsdk”
)
# 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 := NewBitmap(width, height, BitmapE_DIBArgb)
defer DeleteBitmap(bitmap)
bitmap.FillRect(uint(0xFFFFFFFF))
# Render page.
render := NewRenderer(bitmap, false)
render.StartRender(page, matrix)
...