介绍

Aspose.PDF FOSS for C++ 是一个开源的 C++20 库,用于处理 PDF 文档。API 的范围涵盖文档和页面管理、文本提取、光栅渲染、加密、注释以及交互式 AcroForm 字段——这些操作是 C++ 应用在创建、检查或转换 PDF 文件时最常需要的,且无需依赖外部 PDF 引擎或商业 SDK。

该库仅链接 C++ 标准库。内部组件如 BMP、JPEG 和 TIFF 光栅编码器以及页面光栅化器均在项目内部从头实现,因此不依赖系统 PDF 渲染器、商业 PDF 组件或任何第三方图像编解码器。这使得除符合 C++20 的编译器和标准库外,依赖足迹为零。

Aspose.PDF FOSS for C++ 在 MIT 许可证下发布。本文将逐步介绍 Document 类及其支持类型所提供的主要功能区域,随后给出快速入门示例以及当前确认的格式支持。


关键特性

文档与页面管理

Document 类是打开、检查和保存 PDF 文件的入口点。页面通过 Document.Pages() 访问,它返回一个支持 Count()、Add()、Insert(pageNumber) 和 Delete(pageNumber) 的 PageCollection。文档级元数据可通过 Document.Info()(DocumentInfo 实例)读取和更新,或直接使用 Document.SetTitle() 设置。

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/page_collection.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    std::cout << "Page count: " << doc.Pages().Count() << "\n";

    // Insert a blank page after page 1, then drop the last page
    doc.Pages().Insert(2);
    doc.Pages().Delete(doc.Pages().Count());

    doc.SetTitle("Generated Report");
    doc.Save("output.pdf");
}

Document.Optimize() 和 Document.OptimizeResources() 通过合并和修剪未使用的资源在保存前减少输出文件大小。

文本提取

Aspose::Pdf::Text::TextAbsorber 提取整个文档或单页的纯文本内容。Visit() 遍历目标并且 Text() 返回累积的字符串。TextFragmentAbsorber 以更细粒度工作,通过 TextFragmentCollection 暴露单个文本运行。

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/text_fragment_absorber.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");

    // Extract all text from the document
    Aspose::Pdf::Text::TextAbsorber absorber;
    absorber.Visit(doc);
    std::cout << absorber.Text() << "\n";

    // Extract text fragments from page 1
    Aspose::Pdf::Text::TextFragmentAbsorber fragmentAbsorber;
    fragmentAbsorber.Visit(doc.Pages()[1]);
    auto fragments = fragmentAbsorber.TextFragments();
    std::cout << "Fragments on page 1: " << fragments.Count() << "\n";
}

渲染为光栅图像

页面通过每种格式的设备类 — PngDevice、JpegDevice、BmpDevice 和 TiffDevice — 渲染为光栅格式,每个类实现 Process(page, output)。输出分辨率由 Resolution 类控制。

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/png_device.hpp>
#include <aspose/pdf/bmp_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    auto page = doc.Pages()[1];

    Aspose::Pdf::Devices::PngDevice png(Aspose::Pdf::Devices::Resolution(150));
    std::ofstream pngOut("page1.png", std::ios::binary);
    png.Process(page, pngOut);

    Aspose::Pdf::Devices::BmpDevice bmp(Aspose::Pdf::Devices::Resolution(150));
    std::ofstream bmpOut("page1.bmp", std::ios::binary);
    bmp.Process(page, bmpOut);
}

JpegDevice 对 JPEG 输出遵循相同的 Process(page, output) 模式。TiffDevice 另外通过 Process(document, fromPage, toPage, output) 在一次调用中支持多页输出,并且暴露 RenderingOptions() 用于控制表单字段如何渲染到光栅输出中。

加密与访问权限

Document.Encrypt() 使用用户密码和拥有者密码保护文档,并通过 CryptoAlgorithm 枚举选择加密算法:RC4x40、RC4x128、AESx128 或 AESx256。授予的权限使用 Permissions 类型描述,其标志值包括 PrintDocument、ModifyContent、ExtractContent、ModifyTextAnnotations、FillForm、ExtractContentWithDisabilities、AssembleDocument 和 PrintingQuality。

#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");
}

已打开的加密文档使用 Document.Decrypt() 在原位解密。Document.IsEncrypted() 报告已加载的文档当前是否包含加密字典。

批注

页面级批注——文本笔记、形状和标记——可通过 Page.Annotations() 访问,该函数返回一个 AnnotationCollection。各个条目公开共享属性,如 Contents()、Rect()、Color()、Border() 和 AnnotationType(),并且可以从页面中移除或展平成静态内容。

#include <aspose/pdf/document.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    auto& annotations = doc.Pages()[1].Annotations();

    std::cout << "Annotation count: " << annotations.Count() << "\n";
    for (int i = 0; i < static_cast<int>(annotations.Count()); ++i) {
        std::cout << "Contents: " << annotations[i].Contents() << "\n";
    }

    // Remove the first annotation on the page
    if (annotations.Count() > 0) {
        annotations.Delete(annotations[0]);
    }

    doc.Save("output.pdf");
}

任何单个 Annotation 也可以使用其自身的 Flatten() 方法直接转换为静态页面内容,且独立于下面描述的表单展平工作流。

交互式表单

AcroForm 文档字段通过 Document.Form() 管理,该函数返回一个支持 Count()、HasField(fieldName)、Delete(fieldName) 和 Flatten() 的 Form 实例。新字段使用 Form.Add(field, pageNumber) 附加,其中 field 是具体的 Field 子类型,例如 TextBoxField、CheckboxField、ChoiceField 或 ButtonField。

#include <aspose/pdf/document.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    auto& form = doc.Form();

    std::cout << "Form fields: " << form.Count() << "\n";

    if (form.HasField("Signature1")) {
        form.Delete("Signature1");
    }

    // Convert every remaining field into static page content
    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)

打开文档,提取其文本,并将首页渲染为 PNG:

#include <aspose/pdf/document.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);
}

支持的格式

格式扩展名读取写入
BMPbmp—✓
JPEGjpeg—✓
TIFFtiff—✓
Texttxt—✓
SVGsvg✓—

开源与许可

Aspose.PDF FOSS for C++ 在 MIT 许可证下发布,允许商业使用、修改和无版税的再分发。源码可在 github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Cpp 获取。

在 MIT 许可证条款下允许商业使用。如果您的项目需要企业级支持、扩展的格式覆盖或云集成,Aspose.PDF — Enterprise Product Family 是一项独立的商业产品。


入门指南

相关资源