Foxit PDF SDK C/C++ Library
This section describes how to run samples and create a basic C/C++ project using the Foxit PDF SDK C/C++ library. The project demonstrates rendering the first page of a PDF document to a bitmap and saving it as a JPG image.
Prerequisites
Development environment
- GCC and CMake: Ensure GCC is correctly configured and CMake 3.1 or later is installed for compiling Linux C/C++ or Mac C++ sample projects.
- Foxit PDF SDK C/C++ Library: Request a trial via the Foxit Developer Hub. Download and install the library that matches your OS and architecture.
System support
We provide detailed system support information for Windows, Linux, and Mac, including operating system versions and compiler requirements. Select your platform to view the details.
NOTE
- The C language library currently supports Windows only.
Run samples from the command line
Run samples from the command line
See Samples for an overview of the sample projects, dependency information for specific samples, and instructions for running samples from the command line.
Run samples in Visual Studio
Run samples in Visual Studio
On Windows, after building the SDK library with Visual Studio, .exe executables are generated in the \examples\simple_demo\bin folder. The executable name depends on the build configuration.
Run all samples
Open the solution: In the
\examples\simple_demofolder, open the solution file (.sln) that matches your Visual Studio version.Build the solution: In Visual Studio, select Build > Build Solution to compile all samples.
Run a sample: After a successful build, use one of the following methods to run the corresponding sample.
Method 1: Run the
.exedirectly- Navigate to the
binfolder and double-click the generated.exeto run the sample.
Method 2: Run from Visual Studio
- In Solution Explorer, right-click the sample project you want to run and select Set as Startup Project.
- Press
F5or choose Debug > Start Debugging to run the sample.
- Navigate to the
View output:
- Sample output appears in the Visual Studio Output window or console window.
To see detailed sample output, run from the command line.
- Open
cmd.exe, usecdto navigate to\examples\simple_demo\bin, then run the desired.exe.
Run a specific sample
- Build the sample project: In Visual Studio Solution Explorer, right-click the target sample project and select Build. Alternatively, double-click the
.vcxprojfile in the sample folder to open and build that project. - Run the sample: After a successful build, double-click the generated
.exeto run the sample.
To see detailed sample output, run from the command line.
- Open
cmd.exe, usecdto navigate to\examples\simple_demo\bin, then run the desired.exe.
Build and run C++ samples with CMake and Make
On Linux and Mac, the SDK C++ library supports building and running samples with CMake and Make. Follow these steps:
- Navigate to the sample directory:
- In a terminal, go to the
.../examples/simple_demodirectory that contains the sample code.
- In a terminal, go to the
- Configure the build with CMake:
- Run
cmake -DPRJ_NAME=XXX, whereXXXis the sample name (for example, "annotation", "attachment", "pdf2text", and so on). - CMake generates a Makefile based on the PRJ_NAME parameter for subsequent compilation.
- Run
- Build the sample with Make:
- Run
maketo compile the sample according to the generated Makefile and produce an executable. - The executable is named
XXX_xxx, where XXX is the sample name and xxx is the architecture name (for example, annotation_linux64).
- Run
- Run the sample:
- Run
./XXX_xxxto execute the compiled binary. For example, run./annotation_linux64to run the annotation sample.
- Run
Build a project with Visual Studio
On Windows, you can build projects with Visual Studio. Follow these steps:
- Open Visual Studio and create a Win32 console application named
test_win. - Copy the
includeandlibfolders from the Foxit PDF SDK package into thetest_winproject directory. - Add the
includefolder to Additional Include Directories. In Solution Explorer, right-click thetest_winproject, select Properties, then open Configuration Properties > C/C++ > General > Additional Include Directories. - Add the SDK header includes at the top of
test_win.cpportest_win.c:
c++
// test_win.cpp
#include <iostream>
#include "../include/common/fs_common.h"
#include "../include/pdf/fs_pdfdoc.h"
#include "../include/pdf/fs_pdfpage.h"
#include "../include/common/fs_render.h"
// test_win.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"- Add
using namespacedeclarations. (Skip this step for the C language library.)
c++
using namespace std;
using namespace foxit;
using namespace common;
using namespace pdf;
using namespace foxit::common;- Link the Foxit PDF SDK library.
c++
// C++
#if defined (_WIN64) // Windows 64-bit platforms.
#if defined (_DEBUG)
#pragma comment (lib, "../lib/fsdk_win64.lib")
#else
#pragma comment (lib, "../lib/fsdk_win64.lib")
#endif
#elif defined (_WIN32) // Windows 32-bit platforms.
#if defined (_DEBUG)
#pragma comment (lib, "../lib/fsdk_win32.lib")
#else
#pragma comment (lib, "../lib/fsdk_win32.lib")
#endif
#endif
---------------------------------------------------------
// C version
#if defined (_WIN64) // Windows 64-bit platforms.
#if defined (_DEBUG)
#pragma comment (lib, "../lib/fsdk_c_win64.lib")
#else
#pragma comment (lib, "../lib/fsdk_c_win64.lib")
#endif
#elif defined (_WIN32) // Windows 32-bit platforms.
#if defined (_DEBUG)
#pragma comment (lib, "../lib/fsdk_c_win32.lib")
#else
#pragma comment (lib, "../lib/fsdk_c_win32.lib")
#endif
#endif- Initialize the Foxit PDF SDK library. See Initialize the SDK library.
- Load a PDF document and parse its first page. Place a
Sample.pdffile in thetest_win\test_winfolder.
c++
PDFDoc doc("Sample.pdf");
ErrorCode error_code = doc.Load();
if (error_code!= foxit::e_ErrSuccess) return 0;
PDFPage page = doc.GetPage(0);
page.StartParse(foxit::pdf::PDFPage::e_ParsePageNormal, NULL, false);- Render the page to a bitmap and save it as a JPG image.
c++
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);
// Add the bitmap to image and save the image.
Image img;
img.AddFrame(bitmap);
img.SaveAs("testpage.jpg");- Select Build > Build Solution to compile the project. The executable
test_win.exeis generated in thetest_win\Debugortest_win\Releasefolder, depending on your build configuration. - Copy the appropriate
*_win32.dllor*_win64.dllfrom thelibfolder to the output directory (test_win\Debugortest_win\Release). Ensure the DLL architecture matches your application's target platform (Win32 or Win64). - Run the project using one of the following methods:
- In Visual Studio, select Debug > Start Without Debugging. The output file
testpage.jpgis generated in thetest_win\test_winfolder. - Double-click
test_win.exeto run the application. PlaceSample.pdfin the same folder astest_win.exe. The output filetestpage.jpgis also generated in that folder.
- In Visual Studio, select Debug > Start Without Debugging. The output file
Complete example for test_win.cpp:
c++
#include "stdafx.h"
#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 std;
using namespace foxit;
using namespace common;
using namespace pdf;
using namespace foxit::common;
// Include Foxit PDF SDK library.
#if defined (_WIN64) // windows 64bit platforms.
#if defined (_DEBUG)
#pragma comment (lib, "../lib/fsdk_win64.lib")
#else
#pragma comment (lib, "../lib/fsdk_win64.lib")
#endif
#elif defined (_WIN32) // windows 32bit platforms.
#if defined (_DEBUG)
#pragma comment (lib, "../lib/fsdk_win32.lib")
#else
#pragma comment (lib, "../lib/fsdk_win32.lib")
#endif
#endif
int _tmain(int argc, _TCHAR* argv[])
{
// The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
// The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
const char* sn = " ";
const char* key = " ";
foxit::ErrorCode code = Library::Initialize(sn, key);
if (code != foxit::e_ErrSuccess) {
return FALSE;
}
// Load a PDF document, and parse the first page of the document.
PDFDoc doc("Sample.pdf");
ErrorCode error_code = doc.Load();
if (error_code!= foxit::e_ErrSuccess) return 0;
PDFPage page = doc.GetPage(0);
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, Bitmap::e_DIBArgb, NULL, 0);
bitmap.FillRect(0xFFFFFFFF, NULL);
// Render page.
Renderer render(bitmap, false);
render.StartRender(page, matrix, NULL);
// Add the bitmap to image and save the image.
Image img;
img.AddFrame(bitmap);
img.SaveAs("testpage.jpg");
return 0;
}Complete example for test_win.c:
c
#include "stdafx.h"
#include <iostream>
#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"
// Include Foxit PDF SDK library.
#if defined (_WIN64) // windows 64bit platforms.
#if defined (_DEBUG)
#pragma comment (lib, "../lib/fsdk_c_win64.lib")
#else
#pragma comment (lib, "../lib/fsdk_c_win64.lib")
#endif
#elif defined (_WIN32) // windows 32bit platforms.
#if defined (_DEBUG)
#pragma comment (lib, "../lib/fsdk_c_win32.lib")
#else
#pragma comment (lib, "../lib/fsdk_c_win32.lib")
#endif
#endif
int _tmain(int argc, _TCHAR* argv[])
{
// The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
// The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
const char* sn = " ";
const char* key = " ";
FSErrorCode code = FSDK_Library_Initialize(sn, key);
if (code != e_FSErrSuccess) {
return false;
}
// Load a PDF document, and parse the first page of the document.
FS_PDFDOC_HANDLE doc;
FSDK_PDFDoc_Create0("Sample.pdf", &doc);
FSErrorCode error_code = FSDK_PDFDoc_Load(doc, NULL);
if (error_code!= e_FSErrSuccess) return 0;
FS_PDFPAGE_HANDLE page;
FSDK_PDFDoc_GetPage(doc, 0, &page);
FS_PROGRESSIVE_HANDLE progressivehandle;
FSDK_PDFPage_StartParse(page, e_FSParseFlagsParsePageNormal, NULL, false, &progressivehandle);
float floatwidth, floatheight;
FSDK_PDFPage_GetWidth(page, &floatwidth);
FSDK_PDFPage_GetHeight(page, &floatheight);
int width = (int)floatwidth;
int height = (int)floatheight;
FSRotation rotate;
FSDK_PDFPage_GetRotation(page, &rotate);
FSMatrix matrix;
matrix.a = 1;
matrix.b = 0;
matrix.c = 0;
matrix.d = 1;
matrix.e = 0;
matrix.f = 0;
FSDK_PDFPage_GetDisplayMatrix(page, 0, 0, width, height, rotate, &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);
FSDK_Renderer_StartRender(render, page, matrix, NULL, &progressivehandle);
// Add the bitmap to image and save the image.
FS_IMAGE_HANDLE image;
FSDK_Image_Create(&image);
FS_BOOL result = false;
FSDK_Image_AddFrame(image, bitmap, &result);
FSDK_Image_SaveAs(image, "testpage.jpg", &result);
return 0;
}Build a project with Makefile
On Linux and Mac, you can build projects with a Makefile. Follow these steps:
- Create the project directory: Create a folder named
testas the project root. - Copy SDK library files: Copy the
includeandlibfolders from the Foxit PDF SDK package into thetestfolder. - Create the sample entry file: Place a
Sample.pdffile in thetestfolder and create atest.cppfile with the following sample code:
c++
#include <iostream>
#include "common/fs_common.h"
#include "pdf/fs_pdfdoc.h"
#include "pdf/fs_pdfpage.h"
#include "common/fs_render.h"
using namespace std;
using namespace foxit;
using namespace common;
using namespace pdf;
using namespace foxit::common;
int main(int argc, char* argv[])
{
// The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
// The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
const char* sn = "<SN>";
const char* key = "<KEY>";
foxit::ErrorCode code = Library::Initialize(sn, key);
if (code != foxit::e_ErrSuccess) {
return FALSE;
}
// Load a PDF document, and parse the first page of the document.
PDFDoc doc("../../Sample.pdf");
ErrorCode error_code = doc.Load();
if (error_code!= foxit::e_ErrSuccess) return 0;
PDFPage page = doc.GetPage(0);
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, Bitmap::e_DIBArgb, NULL, 0);
bitmap.FillRect(0xFFFFFFFF, NULL);
// Render page.
Renderer render(bitmap, false);
render.StartRender(page, matrix, NULL);
// Add the bitmap to image and save the image.
Image img;
img.AddFrame(bitmap);
img.SaveAs("testpage.jpg");
return 0;
}Create a Makefile: Create a
Makefilein thetestfolder to define build rules and dependencies. Configure the library path so the project links against the Foxit PDF SDK library.- Linux x64:
libfsdk_linux64.so - Linux x86:
libfsdk_linux32.so - Linux ARM:
libfsdk_linuxarmv7.soorlibfsdk_linuxarmv8.so - Mac x64:
libfsdk_mac64.dylib - Mac ARM64:
libfsdk_macarm.dylib
Example Makefile for x86/x64:
- Linux x64:
makefile
CXX = g++
# Foxit PDF SDK lib and header file paths
INCLUDE_PATH=-Iinclude
FSDKLIB_PATH=-Llib
LIB_PATH=lib
FSDKLIB=-lfsdk64
LIBNAME=libfsdk64.so
LIBS=$(FSDKLIB) -lpthread
LDFLAGS=-Wl,--rpath=.
DEST_PATH=./bin/rel_gcc
OBJ_PATH=./obj/rel
CCFLAGS=-c
# Copy the given library to the destination path
CP_LIB=cp $(LIB_PATH)/$(LIBNAME) $(DEST_PATH)
DEST=-o $(DEST_PATH)/$@
OBJ_DEST= -o $(OBJ_PATH)/$@
all: test
dir:
mkdir -p $(DEST_PATH)
mkdir -p $(OBJ_PATH)
test.o :test.cpp
$(CXX) $(CCFLAGS) $(INCLUDE_PATH) $^ $(OBJ_DEST)
test: dir test.o
$(CXX) $(OBJ_PATH)/test.o $(DEST) $(FSDKLIB_PATH) $(LIBS) $(LDFLAGS)Example Makefile for ARM64:
makefile
CXX=g++
# Foxit PDF SDK lib and header file paths
INCLUDE_PATH=-Iinclude
FSDKLIB_PATH=-Llib
LIB_PATH=lib
FSDKLIB=-libfsdkarmv8.so
LIBNAME=libfsdkarmv8.so
LIBS=$(FSDKLIB) -lpthread
LDFLAGS=-Wl,--rpath=.
DEST_PATH=./bin/rel_gcc
OBJ_PATH=./obj/rel
CCFLAGS=-c
# Copy the given library to the destination path
CP_LIB=cp $(LIB_PATH)/$(LIBNAME) $(DEST_PATH)
DEST=-o $(DEST_PATH)/$@
OBJ_DEST= -o $(OBJ_PATH)/$@
all: test
dir:
mkdir -p $(DEST_PATH)
mkdir -p $(OBJ_PATH)
test.o :test.cpp
$(CXX) $(CCFLAGS) $(INCLUDE_PATH) $^ $(OBJ_DEST)
test: dir test.o
$(CXX) $(OBJ_PATH)/test.o $(DEST) $(FSDKLIB_PATH) $(LIBS) $(LDFLAGS)- Build the project: Open a terminal, navigate to the
testdirectory, and runmake test. The binarytestis generated in thetest/bin/rel_gccfolder. - Run the sample: Navigate to
test/bin/rel_gccand run./test. The output filetestpage.jpgis generated in the current folder.