Aspose.PDF FOSS for C++ 是一个开源的、现代的 C++20 库,用于处理 PDF 文档——打开和保存 PDF、提取文本、将页面栅格化为图像格式,以及从头构建文档(文本、图像、表格、矢量图形、批注、AcroForm 字段和书签)。该库仅依赖 C++ 标准库:包括 TIFF、JPEG 和 PNG 编解码器以及页面栅格化器在内的每个原语均从头实现,且不依赖任何商业 PDF 堆栈。
核心 API 围绕 Document 类及其 Pages() 集合构建。文本可使用 Aspose::Pdf::Text::TextAbsorber 提取,页面通过 PngDevice、JpegDevice、BmpDevice 和 TiffDevice 等设备类渲染为栅格图像,文档可以使用 Document.Encrypt() 通过 CryptoAlgorithm 枚举选择 RC4-40、RC4-128、AES-128 或 AES-256 算法进行加密。交互式表单字段通过 Form 类管理,批注——文本注释、突出显示、形状和印章——则通过 Annotation 类层次结构添加和修改。
Aspose.PDF FOSS for C++ 在 MIT 许可证下发布,没有运行时费用或使用限制。它通过 CMake 构建为静态库——将其作为子目录添加到你的构建中或单独安装——并且只需要一个 C++20 编译器,无需其他运行时依赖。有关企业功能和支持,请参阅 Aspose.PDF for C++ — Enterprise Product。
Document 保存修改Document.Pages()(PageCollection)访问并遍历页面PageCollection.Add()/Insert()/Delete() 添加、插入和删除页面DocumentInfo(Document.Info())读取和设置文档元数据Document.Optimize()/OptimizeResources() 优化文档大小Aspose::Pdf::Text::TextAbsorber 从整个文档提取纯文本TextAbsorber.Visit() 访问单页或整个文档TextAbsorber.Text() 获取提取的文本TextFragmentAbsorber.TextFragments() 定位单个文本片段PngDevice 将页面渲染为 PNGJpegDevice、BmpDevice、TiffDevice 将页面渲染为 JPEG、BMP 或 TIFFResolution 类控制输出分辨率{Device}.Process() 处理单个页面Document.Encrypt() 并提供用户密码和所有者密码对文档进行加密CryptoAlgorithm 枚举选择 RC4-40、RC4-128、AES-128 或 AES-256Permissions 类型控制授予的权限Document.Decrypt() 解密受密码保护的文档Annotation 和 AnnotationCollection 添加、修改和扁平化注释Form 类管理 AcroForm 字段Form.Add()/Form.Delete() 添加和删除字段Form.Flatten() 将交互式表单扁平化为静态内容将库添加为 CMake 子目录,然后打开文档,统计其页数,使用 TextAbsorber 提取文本,并以 150 DPI 将第 1 页渲染为 PNG。
add_subdirectory(aspose.pdf-foss-for-cpp)
target_link_libraries(your_app PRIVATE aspose_pdf_foss)
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/page_collection.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/png_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::PngDevice png(Aspose::Pdf::Devices::Resolution(150));
std::ofstream out("page1.png", std::ios::binary);
png.Process(doc.Pages()[1], out);
}
打开文档并使用 AES-256 加密通过用户密码和所有者密码进行保护。
#include <aspose/pdf/document.hpp>
int main() {
Aspose::Pdf::Document doc("input.pdf");
doc.Encrypt("user-password", "owner-password",
Aspose::Pdf::Permissions(),
Aspose::Pdf::CryptoAlgorithm::AESx256);
doc.Save("encrypted.pdf");
}
读取现有的 /Info 条目,并通过 DocumentInfo 更新文档标题。
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/document_info.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
auto& info = doc.Info();
std::cout << "Title: " << info.Title() << "\n";
std::cout << "Author: " << info.Author() << "\n";
doc.SetTitle("Updated Report Title");
doc.Save("output.pdf");
}
它采用 MIT 许可证,可免费用于商业和非商业产品,且没有激活密钥或 API 调用限制。
需要 C++20 编译器(clang 16+、gcc 13+ 或 MSVC 2022 17.5+)和 CMake 3.22 或更高版本。Python 3 仅在构建时需要。
不。它仅链接 C++ 标准库——运行时不需要任何第三方 PDF、图像或压缩依赖。
它可以读取并保存原生 PDF 文档,并将渲染的页面导出为 Bmp、Jpeg 和 Tiff,以及纯文本。SVG 内容可以导入。
使用用户密码、所有者密码和 CryptoAlgorithm(RC4x40、RC4x128、AESx128 或 AESx256)调用 Document.Encrypt()。
是的——一些 XmpValue 类型检查助手(IsDateTime、IsField 等)是始终返回 false 的存根;请改用 IsString/IsInteger/IsDouble/IsArray。