在C++中編輯 PDF 格式

本機和高性能 PDF 文檔敏感的編校資訊使用伺服器端 Aspose.PDF用於 C++ API,而無需使用任何軟體,如微軟或 Adobe PDF。

如何使用 C++ 庫編輯PDF檔

為了編輯PDF檔,我們將使用[Aspose.PDF for C++](https://products.aspose.com/pdf/cpp)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 cpp 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

通過C++編輯 PDF 文件


您需要為C++.PDF [Aspose https://releases.aspose.com/pdf/cpp) 才能在您的環境中嘗試代碼。

  1. 載入包含文件實例的 PDF。 創建文本碎片以搜索詞作為參數的吸收物件。
  2. 設定搜尋選項。
  3. 迴圈遍歷每個片段收集以進行密文。
  4. 儲存檔。

編輯 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");