在 C++ 中编辑 PDF 格式

本机和高性能 PDF 文档敏感密文信息使用服务器端 Aspose.PDF for C++ API,无需使用微软或 Adobe PDF 等任何软件。

如何使用 C++ 库编辑 PDF 文件

为了编辑 PDF 文件,我们将使用 Aspose.PDF for C++ API,这是一款功能丰富、功能强大且易于使用的适用于 cpp 平台的文档操作 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以从软件包管理器控制台使用以下命令。

通过 C++ 编辑 PDF 文档


你需要 Aspose.PDF for C++ 才能在你的环境中试用这些代码。

1.使用 “文档” 实例加载 PDF。 1.使用搜索词作为参数创建 TextFragmentAbsorber 对象。 1.设置搜索选项。 1.循环浏览每个片段收集以进行编辑。 1.保存 PDF 文件。

编辑 PDF 文件-C++。


    // Load PDF file
    auto pdfDocument = MakeObject<Document>(u"sourceFile.pdf");

    // Create TextAbsorber object to find all instances of the input search phrase
    auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"Document");

    // Accept the absorber for all the pages
    pdfDocument->get_Pages()->Accept(textFragmentAbsorber);

    // Get the extracted text fragments
    auto textFragmentCollection = textFragmentAbsorber->get_TextFragments();

    // Loop through the fragments
    for (auto textFragment : textFragmentCollection){
        // Update text and other properties
        textFragment->set_Text(u"UPDATED TEXT");
        textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));
        textFragment->get_TextState()->set_FontSize(22);
    }

    // Save the resulting PDF document.
    pdfDocument->Save(u"outputFile.pdf");