Skip to content

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 ​

  1. Open the solution: In the \examples\simple_demo folder, open the solution file (.sln) that matches your Visual Studio version.

  2. Build the solution: In Visual Studio, select Build > Build Solution to compile all samples.

  3. Run a sample: After a successful build, use one of the following methods to run the corresponding sample.

    Method 1: Run the .exe directly

    • Navigate to the bin folder and double-click the generated .exe to 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 F5 or choose Debug > Start Debugging to run the sample.
  4. 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, use cd to navigate to \examples\simple_demo\bin, then run the desired .exe.

Run a specific sample ​

  1. Build the sample project: In Visual Studio Solution Explorer, right-click the target sample project and select Build. Alternatively, double-click the .vcxproj file in the sample folder to open and build that project.
  2. Run the sample: After a successful build, double-click the generated .exe to run the sample.

To see detailed sample output, run from the command line.

  • Open cmd.exe, use cd to 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:

  1. Navigate to the sample directory:
    • In a terminal, go to the .../examples/simple_demo directory that contains the sample code.
  2. Configure the build with CMake:
    • Run cmake -DPRJ_NAME=XXX, where XXX is the sample name (for example, "annotation", "attachment", "pdf2text", and so on).
    • CMake generates a Makefile based on the PRJ_NAME parameter for subsequent compilation.
  3. Build the sample with Make:
    • Run make to 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).
  4. Run the sample:
    • Run ./XXX_xxx to execute the compiled binary. For example, run ./annotation_linux64 to run the annotation sample.

Build a project with Visual Studio ​

On Windows, you can build projects with Visual Studio. Follow these steps:

  1. Open Visual Studio and create a Win32 console application named test_win.
  2. Copy the include and lib folders from the Foxit PDF SDK package into the test_win project directory.
  3. Add the include folder to Additional Include Directories. In Solution Explorer, right-click the test_win project, select Properties, then open Configuration Properties > C/C++ > General > Additional Include Directories.
  4. Add the SDK header includes at the top of test_win.cpp or test_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"
  1. Add using namespace declarations. (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;
  1. 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
  1. Initialize the Foxit PDF SDK library. See Initialize the SDK library.
  2. Load a PDF document and parse its first page. Place a Sample.pdf file in the test_win\test_win folder.
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);
  1. 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");
  1. Select Build > Build Solution to compile the project. The executable test_win.exe is generated in the test_win\Debug or test_win\Release folder, depending on your build configuration.
  2. Copy the appropriate *_win32.dll or *_win64.dll from the lib folder to the output directory (test_win\Debug or test_win\Release). Ensure the DLL architecture matches your application's target platform (Win32 or Win64).
  3. Run the project using one of the following methods:
    • In Visual Studio, select Debug > Start Without Debugging. The output file testpage.jpg is generated in the test_win\test_win folder.
    • Double-click test_win.exe to run the application. Place Sample.pdf in the same folder as test_win.exe. The output file testpage.jpg is also generated in that folder.

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:

  1. Create the project directory: Create a folder named test as the project root.
  2. Copy SDK library files: Copy the include and lib folders from the Foxit PDF SDK package into the test folder.
  3. Create the sample entry file: Place a Sample.pdf file in the test folder and create a test.cpp file 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;
}
  1. Create a Makefile: Create a Makefile in the test folder 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.so or libfsdk_linuxarmv8.so
    • Mac x64: libfsdk_mac64.dylib
    • Mac ARM64: libfsdk_macarm.dylib

    Example Makefile for x86/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)
  1. Build the project: Open a terminal, navigate to the test directory, and run make test. The binary test is generated in the test/bin/rel_gcc folder.
  2. Run the sample: Navigate to test/bin/rel_gcc and run ./test. The output file testpage.jpg is generated in the current folder.