免费 C++ PDF 库,用于读取和写入 PDF 文件
一个简单的开源基于 C 的 PDF 库,允许读取、写入和操作 PDF 文档。它支持图像转 PDF、内容提取、流式 PDF 读取等功能,可在 C 应用中使用。
PDFio 库是什么?
PDFio 是一个轻量级、开源的 C 库,专门用于读取和写入 PDF 文件,而无需处理渲染或查看的复杂性。该库由 Michael R Sweet 创建,遵循 Apache License 2.0 许可证,为开发者提供了一种直接的 PDF 操作方式,侧重于内容的创建、提取和转换,而非视觉呈现。该库非常易于使用,提供了多种处理 PDF 文件的方式,例如 PDF 页面提取与合并、从数据库生成自动化报告、添加水印和盖章、证书和文凭的创建、用于归档的字体嵌入、元数据修改等。
PDFio 专注于基本的 I/O 操作,使其非常适合需要从 PDF 中提取数据或创建新 PDF 的应用程序,而无需更复杂库的开销。该库支持强大的转换功能,允许软件开发者在 C 应用程序中将 markdown 转换为 PDF、将图像转换为 PDF、将 HTML 转换为 PDF,以及在 PDF 中生成条形码。它专注于核心功能,不涉及渲染开销,因而非常适合服务器端应用、命令行工具和嵌入式系统,在这些场景中需要 PDF 的创建或处理但不需要可视化显示。无论您是在构建文档管理系统、创建自动化报告生成器,还是开发 PDF 分析工具,PDFio 都提供了专业 C 应用程序所需的基本 PDF 处理功能。
PDFio 入门指南
推荐的安装 PDFio 的方式是使用 NuGet。请使用以下命令进行顺利安装。
通过 NuGet 安装 PDFio
Install-Package pdfio_native 通过 GitHub 安装 PDFio
git clone https://github.com/michaelrsweet/pdfio.git
您也可以手动安装;直接从 GitHub 仓库下载最新的发布文件。
使用 C 从头创建 PDF
开源的 PDFio 库已完整支持在 C 应用程序中从头创建新的 PDF 文档。创建新的 PDF 文档同样简单,只需几行 C 代码,软件开发者即可添加新页面、绘制文本和简单形状、添加图像(JPEG、PNG 格式)、应用颜色和变换、构建表格和结构化布局以及更多功能。下面是一个演示如何使用 C 代码创建具有自定义页面尺寸和内容的 PDF 文档的简易示例。
如何使用 C 库创建具有自定义页面尺寸和内容的简易 PDF 文档?
#include
#include
int main(void) {
pdfio_file_t *pdf;
pdfio_dict_t *dict;
pdfio_stream_t *page;
pdfio_obj_t *font;
pdfio_rect_t media_box = {0.0, 0.0, 612.0, 792.0}; // US Letter
pdfio_rect_t crop_box = {36.0, 36.0, 576.0, 756.0}; // 0.5" margins
// Create PDF file
pdf = pdfioFileCreate("output.pdf", "2.0",
&media_box, &crop_box,
NULL, NULL);
if (!pdf) {
fprintf(stderr, "Unable to create PDF\n");
return 1;
}
// Create a font object (Helvetica)
font = pdfioFileCreateFontObjFromBase(pdf, "Helvetica");
// Create page dictionary
dict = pdfioDictCreate(pdf);
pdfioPageDictAddFont(dict, "F1", font);
// Create the page
page = pdfioFileCreatePage(pdf, dict);
// Draw text
pdfioContentSetFillColorDeviceGray(page, 0.0); // Black color
pdfioContentTextBegin(page);
pdfioContentSetTextFont(page, "F1", 24.0);
pdfioContentTextMoveTo(page, 100.0, 700.0);
pdfioContentTextShow(page, false, "Hello, PDFio!");
pdfioContentTextEnd(page);
// Close page and file
pdfioStreamClose(page);
pdfioFileClose(pdf);
printf("PDF created successfully!\n");
return 0;
}
使用 C 库进行图像转 PDF 转换
将图像转换为 PDF 格式是最常见的文档处理任务之一。PDFio 库使此过程变得简便,同时提供对页面尺寸、图像放置、缩放和元数据的精细控制。软件开发者可以构建完整的图像转 PDF 转换器,支持单张图像和批量转换。以下示例演示了开发者如何在 C 应用中将单张图像转换为 PDF。
如何使用 C 库将单张图片转换为 PDF?
#include
#include
#include
bool create_pdf_from_image(const char *image_path,
const char *pdf_path,
const char *title) {
pdfio_file_t *pdf;
pdfio_obj_t *image;
pdfio_dict_t *page_dict;
pdfio_stream_t *page_stream;
pdfio_rect_t media_box;
double img_width, img_height;
double page_width, page_height;
double scale_x, scale_y, scale;
double x, y;
// Create the image object first to get dimensions
// We'll use a temporary PDF for this
pdfio_rect_t temp_box = {0, 0, 612, 792};
pdf = pdfioFileCreate(pdf_path, "2.0", &temp_box, NULL, NULL, NULL);
if (!pdf) {
fprintf(stderr, "Error: Cannot create PDF file\n");
return false;
}
// Set PDF metadata
if (title) {
pdfioFileSetTitle(pdf, title);
}
pdfioFileSetAuthor(pdf, "PDFio Image Converter");
pdfioFileSetCreator(pdf, "image2pdf v1.0");
// Load and embed the image
image = pdfioFileCreateImageObjFromFile(pdf, image_path, true);
if (!image) {
fprintf(stderr, "Error: Cannot load image file: %s\n", image_path);
pdfioFileClose(pdf);
return false;
}
// Get image dimensions in pixels
img_width = pdfioImageGetWidth(image);
img_height = pdfioImageGetHeight(image);
printf("Image dimensions: %.0f x %.0f pixels\n", img_width, img_height);
// Create page sized to image (assuming 72 DPI)
// This creates a PDF page that exactly fits the image
page_width = img_width;
page_height = img_height;
media_box.x1 = 0.0;
media_box.y1 = 0.0;
media_box.x2 = page_width;
media_box.y2 = page_height;
// Create page dictionary and add image resource
page_dict = pdfioDictCreate(pdf);
pdfioDictSetRect(page_dict, "MediaBox", &media_box);
pdfioPageDictAddImage(page_dict, "IM1", image);
// Create the page
page_stream = pdfioFileCreatePage(pdf, page_dict);
// Draw image at full size (no scaling)
pdfioContentDrawImage(page_stream, "IM1", 0, 0,
page_width, page_height);
// Close and save
pdfioStreamClose(page_stream);
pdfioFileClose(pdf);
printf("Successfully created PDF: %s\n", pdf_path);
return true;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "Usage: %s [title]\n",
argv[0]);
return 1;
}
const char *title = (argc > 3) ? argv[3] : NULL;
if (!create_pdf_from_image(argv[1], argv[2], title)) {
return 1;
}
return 0;
}
使用 C 库读取 PDF 并提取文本
最常见的任务之一是读取和提取 PDF 文件中的文本。开源的 PDFio 库通过允许开发者遍历页面及其内容流,使此过程直观。该库全面支持从现有 PDF 中提取有用信息,如普通文本、元数据、嵌入字体、数字、图像、葡萄、名称等。以下示例演示如何使用 C 库打开现有 PDF 并提取基本信息。
如何使用 C 库从 PDF 文件中提取简易文本?
#include
#include
#include
// Callback function to handle text objects
static bool text_cb(const pdfio_text_t *text, void *data) {
// Simply print the text to stdout
printf("%s", text->utf8);
return true;
}
int main(void) {
pdfio_file_t *pdf;
pdfio_page_t *page;
// Open the PDF file for reading
pdf = pdfioFileOpen("example.pdf", NULL, NULL, NULL);
if (!pdf) {
fprintf(stderr, "Failed to open example.pdf\n");
return 1;
}
printf("PDF has %d pages.\n", pdfioFileGetNumPages(pdf));
// Read the first page (pages are 1-indexed)
page = pdfioFileGetPage(pdf, 1);
if (!page) {
fprintf(stderr, "Failed to read page 1\n");
pdfioFileClose(pdf);
return 1;
}
// Parse the page content and call our text_cb for each text object
if (!pdfioPageParseContents(page, text_cb, NULL, NULL, NULL)) {
fprintf(stderr, "Failed to parse page contents\n");
}
// Clean up
pdfioFileClose(pdf);
return 0;
}
高效的流式读取
PDFio 旨在高效运行。读取时,它不会一次性将整个文件加载到内存中,而是采用拉取解析方式,允许您分块处理文件。这对于在不耗尽系统内存的情况下处理超大 PDF 至关重要。第一个示例中的 pdfioPageParseContents 函数就是这种基于流模型的典型示例。它顺序读取页面的内容流,并在过程中触发事件(如我们的文本回调)。