使用 C++ 編輯 PDF

PDF 文檔敏感的編輯信息。在 C++ 使用 Aspose.PDF 以編程方式修改 PDF 文件

如何使用 C++ 庫編輯 PDF 文件

為了編輯 PDF 文件,我們將使用 Aspose.PDF for C++ API,這是一個功能豐富、強大且易於使用的文件操作 API,適用於 cpp 平台。開啟 NuGet 套件管理器,搜尋 Aspose.PDF 並安裝。您也可以在套件管理器控制台中使用下列命令。

透過 C++ 編輯 PDF 文件


您需要 Aspose.PDF for C++ 才能在您的環境中嘗試程式碼。

1.載入帶有「文件」實例的 PDF。 1.使用搜索詞作為引數創建文本 FragmentAbsorber 對象。 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");