使用 .NET API 編輯 PPT/PPTX 簡報

最常見且廣泛使用的簡報文件格式是 PPT、PPTX 和 ODP。著名的 Microsoft PowerPoint, OpenOffice Impress 和 Apple Keynote 支援這些格式,我們通常使用這些格式來製作精彩的簡報。作為開發人員,我們可以透過程式編輯應用程式中的簡報。在本文中,我們將討論如何使用 .NET API 進行簡報編輯,在 C# 中編輯 PPT/PPTX 簡報。

以下是本文簡要討論的主題:

用於簡報編輯和自動化的 .NET API

現在,我們將在下面的 C# 範例中使用 GroupDocs.Editor for .NET。它是一個簡報編輯 API,允許開發人員載入、編輯和保存其他格式(如 PPT、PPTX、PDF)編輯的簡報。除了簡報格式之外,API 還支援編輯文字處理文件、電子表格、HTML、XML、JSON、TXT、TSV 和 CSV 格式。

下載部分 下載 DLL 或 MSI 安裝程序,或透過 NuGet 在您的 .NET 應用程式中安裝 API。

PM> Install-Package GroupDocs.Editor

在 C# 中編輯 PPTX/PPTX 簡報

設定 API 後,您就可以快速開始編輯簡報投影片。以下步驟將讓您編輯 PPT/PPTX 和其他支援格式的簡報。

  • 載入簡報。
  • 使用可用選項進行編輯。
  • 儲存編輯後的簡報。

載入 PPT/PPTX 簡報

首先,如果簡報受到保護,請透過提供簡報文件路徑和密碼來載入簡報。

// Load Presentation
using (FileStream fs = File.OpenRead("path/presentation.pptx"))
{
    // Load Presentation
    Options.PresentationLoadOptions loadOptions = new PresentationLoadOptions();
    loadOptions.Password = "P@$$w0Rd";
}

編輯 PPT/PPTX 簡報幻燈片

載入後,您可以根據需要編輯已載入的簡報。在這裡,我使用下面的 C# 程式碼將 PPTX 簡報中所有出現的單字「文件」替換為「簡報」。

// Edit Presentation
using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
{
    Options.PresentationEditOptions editOptions = new PresentationEditOptions();
    editOptions.SlideNumber = 0;            // 1st slide
    editOptions.ShowHiddenSlides = true;

    using (EditableDocument beforeEdit = editor.Edit(editOptions))
    {
        string originalContent = beforeEdit.GetContent();
        List<IHtmlResource> allResources = beforeEdit.AllResources;
        string editedContent = originalContent.Replace("documents", "presentation");       
    }
}

使用選項儲存編輯後的 PowerPoint 簡報

最後,在儲存編輯好的簡報內容的同時,也可以進一步設定各種選項。這些選項包括:設定密碼、輸出格式設定。我在下面提到的程式碼中設定上述選項,並將編輯後的簡報儲存為受密碼保護的 PPTX 檔案。

// Save Presentation
using (EditableDocument afterEdit = EditableDocument.FromMarkup(editedContent, allResources))
{
    Options.PresentationSaveOptions saveOptions = new PresentationSaveOptions(PresentationFormats.Pptm);
    saveOptions.Password = "new_pa$$word";

    using (FileStream outputStream = File.Create("path/edited-presentation.pptx"))
    {
        editor.Save(afterEdit, outputStream, saveOptions);
    }
}

完整程式碼

為了方便起見,這裡是上面解釋的完整 C# 範例,它編輯 PowerPoint 簡報,然後將其儲存為 PPTX 格式。

// 使用 GroupDocs 簡報編輯和自動化 API 在 C# 中編輯 PPT/PPTX 簡報
using (FileStream fs = File.OpenRead("path/presentation.pptx"))
{
    // 載入演示
    Options.PresentationLoadOptions loadOptions = new PresentationLoadOptions();
    loadOptions.Password = "P@$$w0Rd";

    // 編輯簡報
    using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
    {
        Options.PresentationEditOptions editOptions = new PresentationEditOptions();
        editOptions.SlideNumber = 0;            // 1st slide
        editOptions.ShowHiddenSlides = true;

        using (EditableDocument beforeEdit = editor.Edit(editOptions))
        {
            string originalContent = beforeEdit.GetContent();
            List<IHtmlResource> allResources = beforeEdit.AllResources;
            string editedContent = originalContent.Replace("documents", "presentation");
            
            // 儲存簡報
            using (EditableDocument afterEdit = EditableDocument.FromMarkup(editedContent, allResources))
            {
                Options.PresentationSaveOptions saveOptions = new PresentationSaveOptions(PresentationFormats.Pptm);
                saveOptions.Password = "new_pa$$word";

                using (FileStream outputStream = File.Create("path/edited-presentation.pptx"))
                {
                    editor.Save(afterEdit, outputStream, saveOptions);
                }
            }
        }
    }
}

以下是輸出演示,其中所有出現的內容都使用上述程式碼替換。

使用編輯 API 編輯 pptx 簡報

輸出演示 - “文檔”出現被“演示”替換

結論

最後,我們討論了使用 .NET 應用程式的簡報編輯 API 在 C# 中編輯簡報投影片。您可以將 API 與所見即所得編輯器結合使用,對簡報進行視覺化編輯。之後,您可以繼續建立自己的簡報編輯器。同樣,您還可以將編輯功能整合到 .NET 應用程式中。

有關更多詳細資訊、選項和範例,您可以存取 文件GitHub 儲存庫。如需進一步查詢,請聯絡論壇上的支援人員。

相關文章