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