Skip to content
  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