Image Conversion
Foxit PDF SDK provides APIs for converting between PDF files and images. The application can easily implement image creation and image conversion functions, and supports the following image formats: BMP, TIFF, PNG, JPX, JPEG, and GIF. Through Foxit PDF SDK, PDF files and supported image formats (except GIF) can be converted to and from each other. Foxit PDF SDK only supports converting GIF images to PDF files.
Convert PDF pages to bitmap files
c++
#include "include/common/fs_common.h"
#include "include/common/fs_image.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
#include "include/common/fs_render.h"
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
// Assuming PDFDoc doc has been loaded.
...
// Get page count.
int nPageCount = doc.GetPageCount();
for(int i=0;i<nPageCount;i++) {
PDFPage page = doc.GetPage(i);
/ Parse page.
page.StartParse(foxit::pdf::PDFPage::e_ParsePageNormal, NULL, false);
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, foxit::common::Bitmap::e_DIBArgb, NULL, 0);
bitmap.FillRect(0xFFFFFFFF, NULL);
/ Render page.
Renderer render(bitmap, false);
render.StartRender(page, matrix, NULL);
image.AddFrame(bitmap);
}
...C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_image_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfpage_c.h"
#include "include/fs_render_c.h"
// Assuming FS_PDFDOC_HANDLE doc has been loaded.
...
// Get page count
int nPageCount;
FSDK_PDFDoc_GetPageCount(doc, &nPageCount);
for(int i=0;i<nPageCount;i++) {
FS_PDFPAGE_HANDLE page;
FSDK_PDFDoc_GetPage(doc, i, &page);
/ Parse page.
FS_PROGRESSIVE_HANDLE progressive;
FSDK_PDFPage_StartParse(page, e_FSParseFlagsParsePageNormal, NULL, false, &progressive);
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 return_Rotation;
FSDK_PDFPage_GetRotation(page, &return_Rotation);
FSDK_PDFPage_GetDisplayMatrix(page, 0, 0, width, height, return_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 render;
FSDK_Renderer_Create(bitmap, false, &render);
FS_PROGRESSIVE_HANDLE return_StartRender;
FSDK_Renderer_StartRender(render, page, matrix, NULL, &return_StartRender);
FS_BOOL return_AddFrame;
FSDK_Image_AddFrame(image, bitmap, &return_AddFrame);
}
...java
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.common.Image;
import com.foxit.sdk.common.Renderer;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import static com.foxit.sdk.common.Bitmap.e_DIBArgb;
import static com.foxit.sdk.pdf.PDFPage.e_ParsePageNormal;
// Assuming PDFDoc doc has been loaded.
...
Image image = new Image();
// Get page count
int nPageCount = doc.getPageCount();
for (int i = 0; i < nPageCount; i++) {
PDFPage page = doc.getPage(i);
/ Parse page.
page.startParse(e_ParsePageNormal, null, false);
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);
image.addFrame(bitmap);
}
...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 PDFDoc doc has been loaded.
...
# Get page count
nPageCount = doc.GetPageCount()
for i in range(0, nPageCount):
page = doc.GetPage(i)
# Parse page.
page.StartParse(PDFPage.e_ParsePageNormal, None, False)
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)
image.AddFrame(bitmap)
...objc
#include "FSPDFObjC.h"
...
// Assuming FSPDFDoc doc has been loaded.
...
FSImage* image = [FSImage new];
// Get page count
int page_count = [doc getPageCount];
for(int i=0;i<page_count;i++) {
FSPDFPage* page = [doc getPage:i];
/ Parse page.
[page startParse:FSPDFPageParsePageNormal pause:nil is_reparse:NO];
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];
[image addFrame:bitmap];
}js
const FSDK = require("@fuxinsoft/foxit-pdf-sdk-node");
...
// Assuming PDFDoc doc has been loaded.
...
// Get page count
let page_count = doc.GetPageCount();
for(let i=0; i<page_count; i++) {
let page = doc.GetPage(i);
/ Parse page.
page.StartParse(FSDK.PDFPage.e_ParsePageNormal, null, false);
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(width, height, FSDK.Bitmap.e_DIBArgb, null, 0);
bitmap.FillRect(0xFFFFFFFF, null);
/ Render page
let render = new FSDK.Renderer(bitmap, false);
render.StartRender(page, matrix, null);
image.AddFrame(bitmap);
...
}csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
// Assuming PDFDoc doc has been loaded.
...
Image image = new Image();
// Get page count
int nPageCount = doc.GetPageCount();
for(int i=0;i<nPageCount;i++)
{
using (PDFPage page = doc.GetPage(i))
{
/ Parse page.
page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false);
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);
image.AddFrame(bitmap);
}
}
...NOTE
- For pdf2image, if a PDF contains images larger than 1 GB, use tiled (chunked) rendering. Otherwise, errors may occur. The following shows a brief tiled-rendering implementation.
c++
#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
using namespace std;
using namespace foxit;
using namespace foxit::common;
...
page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false);
int render_sum = 10;
int width = static_cast<int>(page.GetWidth());
int height = static_cast<int>(page.GetHeight());
int width_scale = 1;
int height_scale = 1;
int little_width = width * width_scale;
int little_height = height / render_sum * height_scale;
for (size_t i = 0; i<render_sum; i++)
{
/ According to Matrix, do module rendering for large PDF files.
Matrix matrix = page.GetDisplayMatrix(0, -1 * i * little_height, width * width_scale, height * height_scale, e_Rotation0);
/ Prepare a bitmap for rendering.
Bitmap bitmap(little_width, little_height, Bitmap::e_DIBArgb, NULL, 0);
bitmap.FillRect(0xFFFFFFFF, NULL);
Renderer render(bitmap, false);
render.StartRender(page, matrix, NULL);
/ The bitmap data will be added to the end of image file after rendering.
...
}C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_pdfpage_c.h"
#include "include/fs_image_c.h"
#include "include/fs_render_c.h"
...
// Parse page.
FS_PROGRESSIVE_HANDLE progressive;
code = FSDK_PDFPage_StartParse(page, e_FSParseFlagsParsePageNormal, NULL, false, &progressive);
float fWidth;
FSDK_PDFPage_GetWidth(page, &fWidth);
int width = (int)(fWidth);
float fHeight;
FSDK_PDFPage_GetHeight(page, &fHeight);
int height = (int)(fHeight);
int render_sum = 10;
int width_scale = 1;
int height_scale = 1;
int little_width = width * width_scale;
int little_height = height / render_sum * height_scale;
for (int i = 0; i < render_sum; i++)
{
/ According to Matrix, do module rendering for large PDF files.
FSRotation return_rotation;
FSDK_PDFPage_GetRotation(page, &return_rotation);
FSMatrix return_matrix;
FSDK_PDFPage_GetDisplayMatrix(page, 0, -1 * i * little_height, little_width, height * height_scale, return_rotation, &return_matrix);
/ Prepare a bitmap for rendering.
FS_BITMAP_HANDLE bitmap;
FSDK_Bitmap_Create(little_width, little_height, e_FSDIBArgb, NULL, 0, &bitmap);
FSDK_Bitmap_FillRect(bitmap, 0xFFFFFFFF, NULL);
/ Render page.
FS_RENDERER_HANDLE render = NULL;
FSDK_Renderer_Create(bitmap, false, &render);
FS_PROGRESSIVE_HANDLE return_StartRender;
FSDK_Renderer_StartRender(render, page, return_matrix, NULL, &return_StartRender);
/ The bitmap data will be added to the end of image file after rendering.
...
}java
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.common.Image;
import com.foxit.sdk.common.Renderer;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import com.foxit.sdk.pdf.PDFPage;
import static com.foxit.sdk.common.Bitmap.e_DIBArgb;
import static com.foxit.sdk.pdf.PDFPage.e_ParsePageNormal;
...
// Parse page.
page.startParse(e_ParsePageNormal, null, false);
int width = (int) page.getWidth();
int height = (int) page.getHeight();
int render_sum = 10;
int width_scale = 1;
int height_scale = 1;
int little_width = width * width_scale;
int little_height = height / render_sum * height_scale;
for(int i = 0; i < render_sum; i++) {
/ According to Matrix, do module rendering for large PDF files.
Matrix2D matrix = page.GetDisplayMatrix(0, -1 * i * little_height, little_width, height * height_scale, page.GetRotation());
/ Prepare a bitmap for rendering.
Bitmap bitmap = new Bitmap(little_width, little_height, e_DIBArgb, null, 0);
bitmap.fillRect(0xFFFFFFFF, null);
/ Render page.
Renderer render = new Renderer(bitmap, false);
render.startRender(page, matrix, null);
/ The bitmap data will be added to the end of image file after rendering.
...
}py
import os
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
...
# Parse page.
page.StartParse(PDFPage.e_ParsePageNormal, None, False)
width = int(page.GetWidth())
height = int(page.GetHeight())
render_sum = 10
width_scale = 1
height_scale = 1
little_width = width * width_scale
little_height = height / render_sum * height_scale
for i in range(0, render_sum):
# According to Matrix, do module rendering for large PDF files.
matrix = page.GetDisplayMatrix(0, -1 * i * little_height, little_width, height * height_scale, page.GetRotation())
# Prepare a bitmap for rendering.
bitmap = Bitmap(little_width, little_height, Bitmap.e_DIBArgb)
bitmap.FillRect(0xFFFFFFFF, None)
render = Renderer(bitmap, False)
render.StartRender(page, matrix, None)
# The bitmap data will be added to the end of image file after rendering.
...objc
#include "FSPDFObjC.h"
...
// Parse page.
[page startParse:FSPDFPageParsePageNormal pause:nil is_reparse:NO];
int width = (int)[page getWidth];
int height = (int)[page getHeight];
int render_sum = 10;
int width_scale = 1;
int height_scale = 1;
int little_width = width * width_scale;
int little_height = height / render_sum * height_scale;
for(int i=0; i<render_sum; i++){
/ According to Matrix, do module rendering for large PDF files.
FSMatrix2D* matrix = [page getDisplayMatrix:0 top:-1*i*little_height width:little_width height:height*height_scale rotate:page.rotation];
/ Prepare a bitmap for rendering.
FSBitmap* bitmap = [[FSBitmap alloc] initWithWidth:little_width height:little_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];
/ The bitmap data will be added to the end of image file after rendering.
...
}js
const FSDK = require("@fuxinsoft/foxit-pdf-sdk-node");
...
// Parse page.
page.StartParse(PDFPage.e_ParsePageNormal, null, false) ;
let width = int(page.GetWidth());
let height = int(page.GetHeight());
let render_sum = 10;
let width_scale = 1;
let height_scale = 1;
let little_width = width * width_scale;
let little_height = height / render_sum * height_scale;
for(var i = 0; i < render_sum; i++) {
/ According to Matrix, do module rendering for large PDF files.
let matrix = page.GetDisplayMatrix(0, -1 * i * little_height, little_width, height * height_scale, page.GetRotation());
/ Prepare a bitmap for rendering.
let bitmap = new FSDK.Bitmap(little_width, little_height, FSDK.Bitmap.e_DIBArgb)
bitmap.FillRect(0xFFFFFFFF, null) ;
let render = new FSDK.Renderer(bitmap, false) ;
render.StartRender(page, matrix, null) ;
/ The bitmap data will be added to the end of image file after rendering.
}
...csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false);
int width = (int)(page.GetWidth());
int height = (int)(page.GetHeight());
int render_sum = 10;
int width_scale = 1;
int height_scale = 1;
int little_width = width * width_scale;
int little_height = height / render_sum * height_scale;
for (int i = 0; i < render_sum; i++)
{
/ According to Matrix, do module rendering for large PDF files.
Matrix2D matrix = page.GetDisplayMatrix(0, -1 * i * little_height, little_width, height * height_scale, page.GetRotation());
/ Prepare a bitmap for rendering.
foxit.common.Bitmap bitmap = new foxit.common.Bitmap(little_width, little_height, foxit.common.Bitmap.DIBFormat.e_DIBArgb, System.IntPtr.Zero, 0);
bitmap.FillRect(0xFFFFFFFF, null);
Renderer render = new Renderer(bitmap, false);
render.StartRender(page, matrix, null);
/ The bitmap data will be added to the end of image file after rendering.
...
matrix.DisPose();
draw.Dispose();
render.Dispose();
}go
import (
. “foxit.com/fsdk”
)
...
# Parse page.
page.StartParse(PDFPageE_ParsePageNormal)
width := int(page.GetWidth())
height := int(page.GetHeight())
render_sum := 10
width_scale := 1
height_scale := 1
little_width := width * width_scale
little_height := height / render_sum * height_scale
for i :=0;i< render_sum;i++ {
# According to Matrix, do module rendering for large PDF files.
matrix := page.GetDisplayMatrix(0, -1 * i * little_height, little_width, height * height_scale, page.GetRotation())
# Prepare a bitmap for rendering.
bitmap := NewBitmap(little_width, little_height, BitmapE_DIBArgb)
bitmap.FillRect(0xFFFFFFFF)
render := NewRenderer(bitmap, false)
render.StartRender(page, matrix)
# The bitmap data will be added to the end of image file after rendering.
}
...Convert images to PDF files
c++
#include "include/common/fs_common.h"
#include "include/common/fs_image.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
Image image(input_file);
int count = image.GetFrameCount();
PDFDoc doc;
for (int i = 0; i < count; i++) {
PDFPage page = doc.InsertPage(i);
page.StartParse(foxit::pdf::PDFPage::e_ParsePageNormal, NULL, false);
/ Add image to page.
page.AddImage(image, i, PointF(0, 0), page.GetWidth(), page.GetHeight(), true);
}
doc.SaveAs(output_file, PDFDoc::e_SaveFlagNoOriginal);
...C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_image_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfpage_c.h"
FS_IMAGE_HANDLE image;
FSDK_Image_Create0(input_file, &image);
int count = 0;
FSDK_Image_GetFrameCount(image, &count);
FS_PDFDOC_HANDLE doc;
FSDK_PDFDoc_Create(&doc);
for (int i = 0; i < count; i++) {
FS_PDFPAGE_HANDLE page;
int width, height;
FSDK_Image_GetWidth(image, &width);
FSDK_Image_GetHeight(image, &height);
FSDK_PDFDoc_InsertPage(doc, i, width, height, &page);
FS_PROGRESSIVE_HANDLE progressive;
FSDK_PDFPage_StartParse(page, e_FSParseFlagsParsePageNormal, NULL, false, &progressive);
/ Add image to page.
FSPointF pointf;
pointf.x = 0;
pointf.y = 0;
FS_BOOL return_result;
FSDK_PDFPage_AddImage(page, image, i, pointf, width, height, true, &return_result);
}
FS_BOOL isSave;
FSDK_PDFDoc_SaveAs(doc, output_file, e_FSSaveFlagNoOriginal, &isSave);
...java
import com.foxit.sdk.common.Image;
import com.foxit.sdk.common.fxcrt.PointF;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import static com.foxit.sdk.pdf.PDFPage.e_ParsePageNormal;
import static com.foxit.sdk.pdf.PDFPage.e_SaveFlagNoOriginal;
Image image = new Image(input_file);
int count = image.getFrameCount();
PDFDoc doc = new PDFDoc();
for (int i = 0; i < count; i++) {
PDFPage page = doc.insertPage(i, PDFPage.e_SizeLetter);
page.startParse(e_ParsePageNormal, null, false);
/ Add image to page.
page.addImage(image, i, new PointF(0, 0), page.getWidth(), page.getHeight(), true);
}
doc.saveAs("convertedPDF.pdf", e_SaveFlagNoOriginal);
...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 *
...
image = Image(input_file)
count = image.GetFrameCount()
doc = PDFDoc()
for i in range(0, count):
page = doc.InsertPage(i)
page.StartParse(PDFPage.e_ParsePageNormal, None, False)
# Add image to page.
page.AddImage(image, i, PointF(0, 0), page.GetWidth(), page.GetHeight(), True)
doc.SaveAs(output_file, PDFDoc.e_SaveFlagNoOriginal)
...objc
#include "FSPDFObjC.h"
...
FSImage *image = [[FSImage alloc] initWithPath:input_file];
int count = [image getFrameCount];
FSPDFDoc *doc = [[FSPDFDoc alloc] init];
for (int i = 0; i < count; i ++) {
FSBitmap *bitmap = [image getFrameBitmap:i];
float w = 612.0;
float h = 792.0;
FSPDFPage *page = [doc insertPage:i width:w height:h];
FSPointF *ptZero = [[FSPointF alloc] init];
ptZero.x = 0;
ptZero.y = 0;
[page addImage:image frame_index:i position:ptZero width:w height:h auto_generate_content:YES];
}
[doc saveAs:output_file save_flags:FSPDFDocSaveFlagNoOriginal];
...js
const FSDK = require("@fuxinsoft/foxit-pdf-sdk-node");
...
let image = Image(input_file);
count = image.GetFrameCount()
doc = PDFDoc()
for(var i = 0; i < count; i++) {
page = doc.InsertPage(i)
page.StartParse(FSDK.PDFPage.e_ParsePageNormal, null, false)
/ Add image to page.
page.AddImage(image, i, PointF(0, 0), page.GetWidth(), page.GetHeight(), true)
}
doc.SaveAs(output_file, FSDK.PDFDoc.e_SaveFlagNoOriginal)
...csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
// Assuming PDFDoc doc has been loaded.
...
Image image = new Image(input_file);
int count = image.GetFrameCount();
for (int i = 0; i < count; i++) {
PDFPage page = doc.InsertPage(i, PDFPage.Size.e_SizeLetter);
page.StartParse((int)PDFPage.ParseFlags.e_ParsePageNormal, null, false);
// Add image to page
page.AddImage(image, i, new PointF(0, 0), page.GetWidth(), page.GetHeight(), true);
}
doc.SaveAs(output_file, (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
...go
import (
. “foxit.com/fsdk”
)
...
image := NewImage(input_file)
count := image.GetFrameCount()
doc := NewPDFDoc()
for i :=0; i<count; i++{
page := doc.InsertPage(i)
page.StartParse(PDFPageE_ParsePageNormal)
# Add image to page.
page.AddImage(image, i, NewPointF(0, 0), page.GetWidth(), page.GetHeight(), true)
}
doc.SaveAs(output_file, PDFDocE_SaveFlagNoOriginal)
...