简介
Aspose.PDF FOSS for C++ 是一个全新的开源库,用于在 C++20 代码中处理 PDF 文档。它在 MIT 许可证下发布,除了 C++ 标准库之外没有运行时依赖——BMP、JPEG、TIFF 编码器、页面光栅化器以及 PDF 解析器都在库内部实现,因此不依赖商业 PDF 引擎、外部图像编解码器或第三方加密库。
该库围绕 Aspose::Pdf::Document 类组织,该类从文件路径加载 PDF 并通过 Document.Pages() 暴露其内容,该方法返回包含各个 Page 对象的 PageCollection。从这里起,API 涵盖文本提取、页面光栅化、文档加密、注释以及 AcroForm 字段——这些操作用于读取、检查和修改现有 PDF 文件,或从头组装新文件。
此版本面向需要在 C++ 代码库中进行 PDF 处理且不想添加商业许可证或第三方依赖链的开发者:文档流水线、内容提取服务、PDF 转图像转换工具,以及需要在更大构建中加密或读取表单数据的应用程序。
包含内容
文档加载与页面访问
打开已有 PDF 只需一次构造函数调用。Document 解析文件并通过 Pages() 暴露其页面,该方法返回支持 1 基索引、Count() 以及 Add()/Insert()/Delete() 用于重构页面顺序的 PageCollection。
#include <aspose/pdf/document.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
std::cout << "Page count: " << doc.Pages().Count() << "\n";
auto page = doc.Pages()[1];
std::cout << "First page number: " << page.Number() << "\n";
}
文本提取
Aspose::Pdf::Text::TextAbsorber 会遍历 Document 或单个 Page 并收集其发现的文本内容。使用 Visit() 传入文档或页面,然后通过 Text() 读取结果。HasErrors() 会报告吸收器是否遇到无法处理的内容。
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
Aspose::Pdf::Text::TextAbsorber absorber;
absorber.Visit(doc);
std::cout << absorber.Text() << "\n";
if (absorber.HasErrors()) {
std::cerr << "Some content could not be extracted\n";
}
}
渲染为光栅图像
页面可通过 BmpDevice、JpegDevice 和 TiffDevice 类渲染为 BMP、JPEG 或 TIFF。每个设备使用 Resolution 构造,并提供 Process(page, output) 方法,将渲染后的图像写入输出流。
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/bmp_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
Aspose::Pdf::Devices::BmpDevice bmp(Aspose::Pdf::Devices::Resolution(150));
std::ofstream out("page1.bmp", std::ios::binary);
bmp.Process(doc.Pages()[1], out);
}
加密
Document.Encrypt() 使用用户密码、所有者密码、权限值以及 CryptoAlgorithm 选择来保护文档。该枚举包括 RC4-40(RC4x40)、RC4-128(RC4x128)、AES-128(AESx128)和 AES-256(AESx256)。Document.Decrypt() 逆转该操作,IsEncrypted() 报告文档当前是否已加密。
#include <aspose/pdf/document.hpp>
int main() {
Aspose::Pdf::Document doc("input.pdf");
doc.Encrypt("user-password", "owner-password",
Aspose::Pdf::Permissions::PrintDocument,
Aspose::Pdf::CryptoAlgorithm::AESx256);
doc.Save("encrypted.pdf");
}
批注
每个 Page 通过 Annotations() 暴露 AnnotationCollection。TextAnnotation 在所属文档上构造,使用 Rectangle 定位,并通过 Add() 添加到页面的集合中。
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/annotations/text_annotation.hpp>
#include <aspose/pdf/rectangle.hpp>
int main() {
Aspose::Pdf::Document doc("input.pdf");
Aspose::Pdf::Annotations::TextAnnotation note(doc);
note.Rect(Aspose::Pdf::Rectangle(100.0, 700.0, 200.0, 720.0, false));
note.Contents("Reviewed - see section 2");
doc.Pages()[1].Annotations().Add(note);
doc.Save("annotated.pdf");
}
AcroForm 字段
Document.Form() 返回文档的 Form,该对象通过 Fields() 以 std::vector<Field*> 形式暴露现有字段。每个 Field 通过 PartialName() 和 Value() 报告其名称和当前值,Form.Flatten() 则将每个字段的外观烘焙进页面内容,去除交互性。
#include <aspose/pdf/document.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("form.pdf");
auto& form = doc.Form();
std::cout << "Field count: " << form.Count() << "\n";
for (auto* field : form.Fields()) {
std::cout << field->PartialName() << " = " << field->Value() << "\n";
}
form.Flatten();
doc.Save("flattened.pdf");
}
快速入门
将该库作为 CMake 子目录添加,并链接到 aspose_pdf_foss 目标:
add_subdirectory(aspose.pdf-foss-for-cpp)
target_link_libraries(your_app PRIVATE aspose_pdf_foss)
打开文档,打印其页数,提取文本,并将首页渲染为 BMP 文件:
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/bmp_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
std::cout << "Pages: " << doc.Pages().Count() << "\n";
Aspose::Pdf::Text::TextAbsorber absorber;
absorber.Visit(doc);
std::cout << absorber.Text() << "\n";
Aspose::Pdf::Devices::BmpDevice bmp(Aspose::Pdf::Devices::Resolution(150));
std::ofstream out("page1.bmp", std::ios::binary);
bmp.Process(doc.Pages()[1], out);
}
支持的格式
| 格式 | 扩展名 | 读取 | 写入 |
|---|---|---|---|
| BMP | .bmp | — | ✓ |
| JPEG | .jpg | — | ✓ |
| TIFF | .tiff | — | ✓ |
| Text | .txt | — | ✓ |
| SVG | .svg | ✓ | — |
BMP、JPEG 和 TIFF 是由各自的设备类生成的页面渲染输出。文本通过 TextAbsorber 从 PDF 中提取。SVG 通过库的 SVG 加载路径作为矢量内容导入。
开源与许可
Aspose.PDF FOSS 的 C++ 版在 MIT 许可证下发布,完整源码可在以下位置获取 github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Cpp. 该许可证允许商业使用、修改和再分发,无需另行协议。