將多個 PowerPoint 投影片合併為單一簡報是整合報告或培訓資料時常見的需求。 GroupDocs.Merger Cloud SDK for Java 讓 Java 開發人員僅需幾行程式碼即可在 Java 中合併和合併 PowerPoint PPT PPTX。在本指南中,我們將逐步說明所需的設定、詳細的步驟實作、完整的可執行範例,以及如何使用 cURL 透過 REST API 執行相同的操作。

設定 GroupDocs.Merger Cloud SDK for Java

在開始編寫程式碼之前,請確保您具備以下條件:

  • Java 開發工具包 (JDK) 8 或更高版本。
  • 一個 IDE,例如 IntelliJ IDEA 或 Eclipse。
  • 一個 GroupDocs Cloud 帳戶,具備 client IDclient secret

將 Maven 依賴項添加到您的 pom.xml

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-merger-cloud</artifactId>
    <version>25.11</version>
</dependency>

從官方發佈頁面下載最新的庫: GroupDocs.Merger Cloud SDK for Java Download。解決依賴關係後,您就可以編寫合併 PowerPoint 文件的程式碼。

一步一步構建:在 Java 中結合與合併 Powerpoint PPT PPTX

步驟 1:初始化 API 客戶端

使用您的憑證建立 ApiClient,並實例化 MergerApi

ApiClient apiClient = new ApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
MergerApi mergerApi = new MergerApi(apiClient);

第 2 步:定義來源 PowerPoint 檔案

為您想要合併的每個簡報建立 FileInfo 物件。

FileInfo sourceFile1 = new FileInfo();
sourceFile1.setFilePath("input1.pptx"); // first PPTX file

FileInfo sourceFile2 = new FileInfo();
sourceFile2.setFilePath("input2.ppt"); // second PPT file

步驟 3:設定合併選項

將來源檔案加入清單並指定輸出路徑。

List<FileInfo> sourceFiles = new ArrayList<>();
sourceFiles.add(sourceFile1);
sourceFiles.add(sourceFile2);

MergeOptions mergeOptions = new MergeOptions();
mergeOptions.setFileInfos(sourceFiles);          // files to be merged
mergeOptions.setOutputPath("merged_output.pptx"); // result file

步驟 4:執行合併請求

建立一個 MergeRequest 並呼叫 merge 方法。

MergeRequest mergeRequest = new MergeRequest(mergeOptions);
try {
    MergeResult mergeResult = mergerApi.merge(mergeRequest);
    System.out.println("Merge successful. Output file stored at: " + mergeResult.getPath());
} catch (Exception e) {
    System.err.println("An error occurred during merging: " + e.getMessage());
    e.printStackTrace();
}

上述程式碼示範了使用 SDK 在 combine and merge Powerpoint PPT PPTX in Java 的完整工作流程。

完整程式碼範例:在 Java 中合併 Powerpoint 檔案

以下示例將所有組件組合在一起,形成一個單一的、可直接運行的程式。

import com.groupdocs.merger.cloud.ApiClient;
import com.groupdocs.merger.cloud.api.MergerApi;
import com.groupdocs.merger.cloud.model.FileInfo;
import com.groupdocs.merger.cloud.model.MergeOptions;
import com.groupdocs.merger.cloud.model.requests.MergeRequest;
import com.groupdocs.merger.cloud.model.responses.MergeResult;

import java.util.ArrayList;
import java.util.List;

public class PowerPointMergeExample {
    public static void main(String[] args) {
        // Initialize the API client with your credentials
        ApiClient apiClient = new ApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
        MergerApi mergerApi = new MergerApi(apiClient);

// Define the source PowerPoint files
        FileInfo sourceFile1 = new FileInfo();
        sourceFile1.setFilePath("input1.pptx"); // first PPTX file

FileInfo sourceFile2 = new FileInfo();
        sourceFile2.setFilePath("input2.ppt"); // second PPT file

List<FileInfo> sourceFiles = new ArrayList<>();
        sourceFiles.add(sourceFile1);
        sourceFiles.add(sourceFile2);

// Set merge options
        MergeOptions mergeOptions = new MergeOptions();
        mergeOptions.setFileInfos(sourceFiles);          // files to be merged
        mergeOptions.setOutputPath("merged_output.pptx"); // result file

// Create and execute the merge request
        MergeRequest mergeRequest = new MergeRequest(mergeOptions);
        try {
            MergeResult mergeResult = mergerApi.merge(mergeRequest);
            System.out.println("Merge successful. Output file stored at: " + mergeResult.getPath());
        } catch (Exception e) {
            System.err.println("An error occurred during merging: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

注意: 此代碼示例演示了核心功能。在將其用於您的項目之前,請確保更新文件路徑(input1.pptxinput2.pptmerged_output.pptx)以匹配實際文件位置,驗證所有必需的依賴項已正確安裝,並在開發環境中徹底測試。如遇到任何問題,請參閱官方文檔或聯繫支援團隊尋求協助。

使用 cURL 和 REST API 合併 Powerpoint 簡報

您可以透過呼叫 GroupDocs Merger REST 端點,在不編寫 Java 程式碼的情況下執行相同的合併操作。以下是一個典型的 cURL 工作流程。

1. 驗證並取得存取令牌

curl -X POST "https://api.groupdocs.cloud/v2.0/authentication/token" \
  -H "Content-Type: application/json" \
  -d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'

回應中包含一個 access_token,您將在後續呼叫中使用它。

2. 上傳源文件

curl -X POST "https://api.groupdocs.cloud/v2.0/storage/file/upload?path=input1.pptx" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -F "file=@/local/path/input1.pptx"

input2.ppt 重複該命令。

3. 執行合併操作

curl -X POST "https://api.groupdocs.cloud/v2.0/merger/merge" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
        "fileInfos": [
          {"filePath": "input1.pptx"},
          {"filePath": "input2.ppt"}
        ],
        "outputPath": "merged_output.pptx"
      }'

API 返回一個包含合併檔案路徑的 JSON 物件。

4. 下載合併檔案

curl -X GET "https://api.groupdocs.cloud/v2.0/storage/file/download?path=merged_output.pptx" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -o merged_output.pptx

欲了解更多詳情,請參閱官方 API 參考

合併選項:配置與設定

雖然基本範例適用於大多數情況,但 SDK 提供了您可以調整的其他選項。

輸出路徑

指定自訂資料夾或檔案名稱。

mergeOptions.setOutputPath("reports/combined_presentation.pptx");

密碼保護(如有需要)

mergeOptions.setPassword("StrongPassword123");

保留原始布局

mergeOptions.setPreserveLayout(true);

這些屬性屬於 MergeOptions 類別;請參閱 API 參考 以獲取完整列表。

結論

透過 GroupDocs.Merger Cloud SDK for Java,您可以輕鬆 在 Java 中合併 PowerPoint PPT、PPTX,無論您偏好原生 Java 函式庫或 RESTful cURL 方式。SDK 會處理檔案儲存、合併邏輯與輸出產生,讓您專注於應用程式的核心功能。請記得取得正式授權以供生產環境使用;價格資訊可在產品頁面查閱,您亦可於 暫時授權頁面 申請暫時授權以進行評估。立即開始整合 PowerPoint 合併功能,簡化您的文件工作流程。

常見問題

  • 我可以使用 SDK 合併哪些格式?
    SDK 支援 PPTPPTX 檔案,此外還支援 API 參考中列出的許多其他文件類型。

  • 在合併之前,我需要轉換舊的 PPT 檔案嗎?
    不需要轉換。SDK 會在合併過程中自動處理 PPTPPTX 來源的混合。

  • SDK 如何處理大型簡報?
    合併在伺服器端執行,因此客戶端機器的記憶體消耗保持在低水平。您也可以調整 PreserveLayout 選項以優化效能。

  • 我可以在不編寫 Java 程式碼的情況下合併簡報嗎?
    可以。使用前面示範的 cURL 命令調用 REST API,或將呼叫整合到任何能發送 HTTP 請求的語言中。

閱讀更多