Skip to content

福昕 PDF SDK C/C++ 库

本节主要介绍如何使用福昕 PDF SDK C/C++ 库运行示例及创建一个基础的 C/C++ 工程。该工程演示如何将 PDF 文档的首页渲染成 bitmap,并将其另存为 JPG 图片。

先决条件

开发环境

  • 编译 Linux C/C++ 或 Mac C++ 示例工程之前,请确保您已正确配置 GCC 编译器,且已安装CMake 3.1或更高版本。
  • 福昕 PDF SDK C 或 C++ 库 (请从福昕官方网站下载并安装与您的操作系统和架构匹配的 C 或 C++ 库)。

系统支持

我们提供 Windows、Linux 和 Mac 平台的详细系统支持信息,包括操作系统版本、编译器要求等。请选择您的操作系统平台,查看详细信息。

提示

当前 C 语言库仅支持 Windows 平台。

使用命令行运行示例

使用命令行运行示例

请参阅 示例,其中包含了示例工程简介,特定示例的依赖信息,以及如何通过命令行运行示例。

使用 Visual Studio 运行示例

使用 Visual Studio 运行示例

Windows 下,SDK 库通过 Vistual Stuidio 编译后,会在 \examples\simple_demo\bin 文件夹下将生成 .exe 可执行文件。可执行文件的名称取决于工程编译的配置。

运行所有示例

  1. 打开解决方案:\examples\simple_demo 文件夹下,根据您的 Visual Studio 版本,选择相应的解决方案文件(.sln)打开。

  2. 生成解决方案: 在 Visual Studio 中,选择 生成 > 生成解决方案 菜单,编译所有示例。

  3. 运行示例: 编译成功后,可使用以下方法运行相应的示例。
    方法一:直接运行 .exe 文件

    • 导航到 bin 文件夹,双击生成的 .exe 可执行文件即可运行相应的示例。
      方法二:在 Visual Studio 中运行
    • 解决方案资源管理器 中,右键单击您想要运行的示例项目,并选择 设为启动项目
    • F5 键或点击 调试 -> 开始调试 运行示例。
  4. 查看输出:

    • 示例运行结果将在 Visual Studio 的 “输出” 窗口或控制台窗口中显示。

    NOTE

    若需查看详细的示例执行过程,请通过命令行运行。

    • 打开 cmd.exe,使用 cd 命令导航至 \examples\simple_demo\bin 目录,然后执行特定的 .exe 文件。

运行某个特定示例

  1. 生成示例工程: 在 Visual Studio 的解决方案资源管理器中,右键单击目标示例工程,选择生成。或者,直接双击示例工程文件夹下的 .vcxproj 文件,打开并生成该工程。
  2. 运行示例: 编译成功后,双击生成的 .exe 可执行文件即可运行该示例。

NOTE

  • 若需查看详细的示例执行过程,请通过命令行运行。
  • 打开 cmd.exe,使用 cd 命令导航至 \examples\simple_demo\bin 目录,然后执行特定的 .exe 文件。

使用 CMake 和 Make 编译并运行 C++ 示例

使用 CMake 和 Make 编译并运行 C++ 示例

在 Linux / Mac 下, SDK C++ 库支持 使用 CMake 和 Make 编译和运行示例。具体步骤如下:

  1. 导航到示例目录:
    • 通过终端,定位到包含示例代码的 .../examples/simple_demo 目录。
  2. 使用 CMake 配置编译:
    • 运行 cmake -DPRJ_NAME=XXX 命令,其中 XXX 是要编译的示例名称 (例如 "annotation", "attachment", "pdf2text" 等)。
    • CMake 会根据 PRJ_NAME 参数生成 Makefile 文件,用于后续的编译。
  3. 使用 Make 构建示例:
    • 运行 make 命令,根据之前生成的 Makefile 文件编译示例代码,并生成可执行文件。
    • 生成的可执行文件命名格式为 XXX_xxx,其中 XXX 是示例名称,xxx 是架构名称(例如 annotation_linux64)。
  4. 运行示例:
    • 执行 ./XXX_xxx 命令,运行编译生成的可执行文件。例如,执行 ./annotation_linux64 来运行 annotation 示例。

使用 Visual Studio 构建工程

Windows 下,可使用 Visual Studio 构建工程。具体步骤如下:

  1. 打开 Visual Studio,创建一个名为 test_win 的Win32控制台应用程序。
  2. 将 福昕 SDK 包文件夹下的 includelib 文件夹拷贝到 test_win 工程目录下。
  3. 添加 include 文件夹到 Additional Include Directories。在 Solution Explorer 中右击test_win 工程,选择 Properties,然后找到 Configuration Properties > C/C++ > General > Additional Include Directories
  4. test_win.cpptest_win.c 的开头添加 include 头文件声明。
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. 添加 using namespace 命名空间声明。(C语言库跳过这步)
c++
using namespace std;
using namespace foxit;
using namespace common;
using namespace pdf;
using namespace foxit::common;
  1. 包含福昕 PDF SDK 库。
c++
//c++
#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
---------------------------------------------------------
//c
#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
  1. 初始化福昕 PDF SDK 库。请查阅 初始化 SDK 库
  2. 加载一个PDF文档,然后解析该文档的首页。假设您已经在 test_win\test_win 文件夹下放入了一个 Sample.pdf 文件。
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. 将页面渲染成bitmap,然后将其另存为 JPG 图片。
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. 点击 Build > Build Solution 编译该工程。可执行文件 test_win.exe 将在 test_win\Debug 或者 test_win\Release 文件夹下生成,具体取决于编译配置。
  2. lib 文件夹下的 **_win32.dll 或者 **_win64.dll 拷贝到输出目录(test_win\Debug 或者 test_win\Release)。请确保 您所选的 **.dll 架构应与应用程序的目标平台 (Win32 或者 Win64) 相匹配。
  3. 选择如下其中一种方式运行该工程:
    • 在 Visual Studio 中点击 Debug > Start Without Debugging 运行工程,然后在 test_win\test_win 文件夹下将会生成 testpage.jpg
    • 双击可执行文件 test_win.exe 运行工程。使用这种方式,您需要将 Sample.pdf 文档放在与 test_win.exe 相同的文件夹中,并且 testpage.jpg 也将在同一文件夹中生成。

test_win.cpp 的完整内容如下:

c++
#include "stdafx.h"

#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"

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;
}

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;
}

使用 Makefile 构建工程

Linux / Mac 下, 可使用 Makefile 构建工程。 具体步骤如下:

  1. 创建项目目录: 新建一个名为 test 的文件夹,作为工程的根目录。
  2. 复制 SDK 库文件: 将福昕 PDF SDK 包文件夹下的 includelib 文件夹复制到 test 文件夹下。
  3. 创建示例入口文件:在 test 文件夹下,放入一个 Sample.pdf文件,并创建一个 test.cpp 文件, 示例代码如下:
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 = ` `;
	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;
}
  1. 创建 Makefile 文件: 在 test 文件夹下创建 Makefile 文件,用于定义编译规则和依赖关系。配置库文件路径,确保链接到 PDF SDK 库。

    • Linux x64 系统使用 libfsdk_linux64.so
    • Linux x86 位系统使用 libfsdk_linux32.so
    • Linux arm64系统使用 libfsdk_linuxarmv7.so 或者 libfsdk_linuxarmv8.so
    • Mac X64 系统使用 libfsdk_mac64.dylib
    • Mac arm64 系统使用 libfsdk_macarm.dylib

Makefile 文件示例如下所示:

c++
//----------------------------x86/64 架构
CXX=g++

# Foxit PDF SDK lib and head files include
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) 
 
//--------------------------- arm64 架构

CXX=g++

# Foxit PDF SDK lib and head files include
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. 构建工程。打开终端,导航到 test目录,然后运行 make test命令,在 test/bin/rel_gcc 文件夹下生成二进制文件 test
  2. 编译工程:
  3. 运行示例:在终端中导航到二进制文件所在的目录 test/bin/rel_gcc,运行 ./test,在当前文件夹下会生成 testpage.jpg