PDF 工具

如何使用 C# 創建 PowerPoint 演示文稿

已更新 2024年8月13日
分享:

從辦公室到教室,Microsoft PowerPoint 是一個基本工具。經過數十年的改進,它成為了業界標準的演示工具。 然而,充分利用你的 PowerPoint 演示文稿可能會很複雜且耗時。

在本指南中,我們將解釋如何自動化創建 PowerPoint 演示文稿,優化流程,並藉助 C# 的靈活性充分利用該程序。 (C#) 以及 Microsoft PowerPoint 互操作库。

使用 C# 建立 PowerPoint 簡報 - 入門指南

  • 建立一個新的 C# 專案
  • 啟動新的 PowerPoint 程式實例
  • 建立您的 PowerPoint 簡報,包括頁面、文字、格式化及其他元素
  • 將專案匯出為新文件

首先,讓我們在深入了解如何使用 C# 製作 PowerPoint 文件之前,先學習一些 PowerPoint interop 的基本知識。開發者可以藉助 PowerPoint interop 函式庫來建立簡報、加入頁面、添加內容、套用格式等等,使您在建立新簡報時有更多的靈活性。這樣一來,開發者便可以自訂簡報文件以滿足獨特的需求並訪問豐富的功能。

使用 C# 建立 PowerPoint 簡報可以讓您即時生成報告並將簡報轉換為其他格式。您可以免費下載 interop 函式庫,但需要安裝 Microsoft Office 才能以程式的方式建立 PowerPoint 簡報。

創建一個新的 Visual Studio 專案

我們開始於 Visual Studio 中創建一個新的控制台應用專案。前往“檔案”並選擇“新建專案”。選擇 C# 語言並選擇“控制台應用程式”。輸入您的專案名稱,選擇您想要儲存的位置,然後按“Next”按鈕。選擇最新的 .NET Framework,然後點擊“Create”。專案啟動並運行後,是時候添加我們的庫了。

添加 Microsoft PowerPoint interop 庫

您可以使用 Microsoft.Office.interop.PowerPoint.Application 類直接在 C# 專案中建立、開啟和修改您的 PowerPoint 文件。以下是程式碼範例:

using PowerPoint = Microsoft.Office.Interop.PowerPoint;

class Program
{
   static void Main(string[] args)
   {
           // Create an instance of PowerPoint application
           PowerPoint.Application powerpointApp = new PowerPoint.Application();
           // Create powerpoint presentation
           PowerPoint.Presentation presentation = powerpointApp.Presentations.Add();
           // Customize the presentation
           // Add slides, insert content, apply formatting, etc.
           // Add a new slide
           PowerPoint.Slide slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);
           // Insert text into the presentation slide
           slide.Shapes[1].TextFrame.TextRange.Text = "Demo";
           slide.Shapes[2].TextFrame.TextRange.Text = "PowerPoint";
           // Add an image to the slide
           slide.Shapes.AddPicture(@"sample.png",
                                   Microsoft.Office.Core.MsoTriState.msoFalse,
                                   Microsoft.Office.Core.MsoTriState.msoCTrue,
                                   100, 100, 300, 200);
           // Save and close the presentation file
           presentation.SaveAs("Presentation.pptx");
           presentation.Close();
           // Quit PowerPoint application
           powerpointApp.Quit();
           Console.ReadKey();
   }
}
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

class Program
{
   static void Main(string[] args)
   {
           // Create an instance of PowerPoint application
           PowerPoint.Application powerpointApp = new PowerPoint.Application();
           // Create powerpoint presentation
           PowerPoint.Presentation presentation = powerpointApp.Presentations.Add();
           // Customize the presentation
           // Add slides, insert content, apply formatting, etc.
           // Add a new slide
           PowerPoint.Slide slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);
           // Insert text into the presentation slide
           slide.Shapes[1].TextFrame.TextRange.Text = "Demo";
           slide.Shapes[2].TextFrame.TextRange.Text = "PowerPoint";
           // Add an image to the slide
           slide.Shapes.AddPicture(@"sample.png",
                                   Microsoft.Office.Core.MsoTriState.msoFalse,
                                   Microsoft.Office.Core.MsoTriState.msoCTrue,
                                   100, 100, 300, 200);
           // Save and close the presentation file
           presentation.SaveAs("Presentation.pptx");
           presentation.Close();
           // Quit PowerPoint application
           powerpointApp.Quit();
           Console.ReadKey();
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

您可以修改新建的 PowerPoint 簡報,使其符合您的要求。可以插入幻燈片、文本、照片、圖表、圖表標題以及其他多媒體項目,應用格式設置,建立過渡和動畫效果,還可執行其他任務。利用 PowerPoint 互操作庫提供的功能,您可以以程式方式調整簡報的各個部分。

儲存您的輸出檔案

要將 PowerPoint 文件儲存到特定的檔案位置,請使用 SaveAs() 方法。要關閉它,請使用dismiss方法。

結論

使用 C# 來製作 PowerPoint 簡報是提高生產力、簡化流程和節省時間的有效方法。開發人員可以利用 Microsoft PowerPoint 互操作庫的功能來自動生成和自定義簡報,從而在處理 PowerPoint 文件時節省時間和精力,並取得專業效果。

無論您是演示者、教育工作者還是商業專業人士,使用這種方法可以提高您的演示技能,幫助您用視覺上引人注目的幻燈片吸引觀眾。

< 上一頁
如何使用 C# 將 PowerPoint 轉換為圖像
下一個 >
開源 PDF 編輯器 (免費與付費工具比較)

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >