PPT 工具

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

里根普恩
里根普恩
2024年3月6日
已更新 2025年4月3日
分享:

介紹

PowerPoint 簡報 對於商業報告、銷售簡報、學術演講以及軟體生成的儀表板來說都是必不可少的。 然而,手動製作 PowerPoint 幻燈片可能既繁瑣又耗時。

如果不需要下載互操作庫就能在C#中自動化創建PowerPoint,那不是很棒嗎? 這就是 Iron Software 強大的 PowerPoint 程式庫,IronPPT 的用武之地。 IronPPT 是一個強大的 .NET 函式庫,允許開發人員程式化地輕鬆建立、修改和匯出 PowerPoint 簡報,而無需使用 Microsoft PowerPoint interop 函式庫。 無論您是需要動態生成幻燈片、插入圖表,還是將簡報轉換為PDF,IronPPT都能提供簡單且有效的解決方案。

今天,我們將探討如何使用 IronPPT 在 C# 中創建 PowerPoint 簡報。 我們將涵蓋:

  • 安裝 IronPPT
  • 建立與儲存 PowerPoint 文件
  • 添加投影片、文字、圖片和圖形
  • PowerPoint 自動化的最佳實踐

    讓我們開始吧! 🚀

開始使用IronPPT

Csharp Create Powerpoint Tutorial 1 related to 開始使用IronPPT

在我們深入探討程式化地製作簡報之前,我們先來設置開發環境。

安裝 IronPPT

要使用IronPPT,請在您的C# 專案中安裝NuGet套件:

  1. 開啟Visual Studio

  2. 打開套件管理器主控台工具 > NuGet 套件管理員 > 套件管理器主控台)。

  3. 運行以下命令:

Install-Package IronPPT


4. 或者,您可以透過前往**「工具 -> NuGet 套件管理員 -> 為解決方案管理 NuGet 套件」**,搜尋 IronPPT,然後點擊「安裝」來進行安裝。

 ![透過 NuGet 套件管理器 screen 安裝 IronPPT](/static-assets/pdf/blog/csharp-create-powerpoint-tutorial/csharp-create-powerpoint-tutorial-2.webp)

5. 建立一個新的主控台應用程式專案。

 安裝完成後,您就可以在我們的 .NET Framework 中開始創建 PowerPoint 簡報。

## **在 C# 中建立 PowerPoint 簡報**

### **1. 初始化 PowerPoint 文件**

PowerPoint 自動化的第一步是建立新的 PowerPoint 文件。使用 IronPPT,您可以以程式化方式生成 PowerPoint 簡報,並將其儲存為 .pptx 文件。 預設情況下,生成的 PowerPoint 文件與 Microsoft PowerPoint、Google 幻燈片和其他支援**.pptx 格式**的軟體相容。 在今天的範例中,我們將創建一個新的 Visual Studio 專案。

因此,在我們的第一個代碼範例中,我們將[創建](https://ironsoftware.com/csharp/ppt/examples/create-empty-presentation/)一個新的空白 PowerPoint 簡報文件。這將是我們的起點,一個簡單的 PowerPoint 簡報文件,之後我們可以為其添加文本、樣式等。

```cs
using IronPPT;

class Program
{
    static void main()
    {
        // Creating a new PresentationDocument object
        var ppt = new PresentationDocument();

    // Save the blank file presentation
        ppt.Save("example.pptx");
    }
}

輸出檔案

使用 IronPPT 創建的空白簡報

當我們在 PowerPoint 程式中開啟新的簡報時,我們可以看到此時它只包含一個空白幻燈片。

2. 添加幻燈片到 PowerPoint

PowerPoint 演示文稿由多張幻燈片組成,每張幻燈片可以具有不同的版面配置、文字、圖像和設計元素。 使用IronPPT,您可以動態地添加幻燈片到演示文稿中、更改其佈局,並以程式化方式插入內容。 這對於自動報告系統、儀表板匯出和簡報生成非常有用。

此時,我們的 PowerPoint 文件只是一個沒有任何內容或額外幻燈片的空白幻燈片。 那麼,讓我們看看如何開始向您的 PowerPoint 簡報中添加幻燈片。

using IronPPT;
using IronPPT.Models;

// Loading in our presentation file
var ppt = new PresentationDocument("output.pptx");

ppt.AddSlide();

ppt.Save("output.pptx");
using IronPPT;
using IronPPT.Models;

// Loading in our presentation file
var ppt = new PresentationDocument("output.pptx");

ppt.AddSlide();

ppt.Save("output.pptx");
Imports IronPPT
Imports IronPPT.Models

' Loading in our presentation file
Private ppt = New PresentationDocument("output.pptx")

ppt.AddSlide()

ppt.Save("output.pptx")
$vbLabelText   $csharpLabel

輸出

Csharp Create Powerpoint Tutorial 4 related to 2. 添加幻燈片到 PowerPoint

🔹 除了添加這些簡單的幻燈片之外,您還可以使用 IronPPT 添加多個幻燈片,並動態修改它們。 在我們的範例中,我們只在展示中新增了兩張投影片,但您可以根據需要添加更多投影片。

3. 添加文字、圖片及格式化

在您添加幻灯片后,下一步是插入內容。 IronPPT允許您添加:

  • 文字:標題、段落和格式化內容。
  • 圖像:標誌、圖表和螢幕截圖。
  • 自訂格式:字體、顏色和對齊。

    例如,您可以為幻燈片添加一個標題,插入一張圖片,並根據您的佈局需求動態設定位置。 使用IronPPT的ParagraphStyleTextStyle類別,我們可以在將文本添加到幻燈片時應用格式化和自訂樣式。

將文字新增到 PowerPoint 簡報中

using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument("test.pptx");

// Adding simple text to the title slide
ppt.Slides[0].TextBoxes[0].AddText("Welcome to IronPPT");

TextBox textBox = new TextBox();
textBox.Type = ShapeType.Rectangle;
textBox.Width = 500;
textBox.Height = 200;
textBox.Position = (100, 100);
ppt.Slides[1].AddChild(textBox);

// Adding a styled paragraph to the second slide
var style = new ParagraphStyle()
{
    Alignment = TextAlignmentTypeValues.Center,
    NoBullet = true,
    LineSpacing = 30,
    ContextualSpacing = true,
};
TextStyle textStyle = new TextStyle()
{
    IsBold = true,
    Color = Color.Blue,
};

var paragraph = new Paragraph();
paragraph.Style = style;
paragraph.AddText("Let's create some awesome presentations with IronPPT!");
paragraph.TextStyle = textStyle;
ppt.Slides[1].TextBoxes[0].AddParagraph(paragraph);

ppt.Save("example.pptx");
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument("test.pptx");

// Adding simple text to the title slide
ppt.Slides[0].TextBoxes[0].AddText("Welcome to IronPPT");

TextBox textBox = new TextBox();
textBox.Type = ShapeType.Rectangle;
textBox.Width = 500;
textBox.Height = 200;
textBox.Position = (100, 100);
ppt.Slides[1].AddChild(textBox);

// Adding a styled paragraph to the second slide
var style = new ParagraphStyle()
{
    Alignment = TextAlignmentTypeValues.Center,
    NoBullet = true,
    LineSpacing = 30,
    ContextualSpacing = true,
};
TextStyle textStyle = new TextStyle()
{
    IsBold = true,
    Color = Color.Blue,
};

var paragraph = new Paragraph();
paragraph.Style = style;
paragraph.AddText("Let's create some awesome presentations with IronPPT!");
paragraph.TextStyle = textStyle;
ppt.Slides[1].TextBoxes[0].AddParagraph(paragraph);

ppt.Save("example.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums

' Creating a new PresentationDocument object
Private ppt = New PresentationDocument("test.pptx")

' Adding simple text to the title slide
ppt.Slides(0).TextBoxes(0).AddText("Welcome to IronPPT")

Dim textBox As New TextBox()
textBox.Type = ShapeType.Rectangle
textBox.Width = 500
textBox.Height = 200
textBox.Position = (100, 100)
ppt.Slides(1).AddChild(textBox)

' Adding a styled paragraph to the second slide
Dim style = New ParagraphStyle() With {
	.Alignment = TextAlignmentTypeValues.Center,
	.NoBullet = True,
	.LineSpacing = 30,
	.ContextualSpacing = True
}
Dim textStyle As New TextStyle() With {
	.IsBold = True,
	.Color = Color.Blue
}

Dim paragraph As New Paragraph()
paragraph.Style = style
paragraph.AddText("Let's create some awesome presentations with IronPPT!")
paragraph.TextStyle = textStyle
ppt.Slides(1).TextBoxes(0).AddParagraph(paragraph)

ppt.Save("example.pptx")
$vbLabelText   $csharpLabel

輸出

添加文字和文字框

在上面的代碼中,我們在標題畫面的現有文本框中添加了新文本。 我們還為第二張投影片創建了一個新的文字框,然後在裡面添加樣式化段落文本。 這是一個簡單的範例,說明如何使用 IronPPT 向您的 PowerPoint 簡報添加完全自訂的文本。

新增圖片

接下來,讓我們添加一個圖片到我們的標題幻燈片,並自定義它的大小和位置,以便將它放置在我們想要的位置上。

using IronPPT;
using IronPPT.Models;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument();

Image image = new Image();
image.LoadFromFile("example.png");
var newImage = ppt.Slides[0].AddImage(image);
newImage.Position = (150, 300);
newImage.Name = "IronPPT";
newImage.Width = 450;
newImage.Height = 200;

ppt.Save("output.pptx");
using IronPPT;
using IronPPT.Models;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument();

Image image = new Image();
image.LoadFromFile("example.png");
var newImage = ppt.Slides[0].AddImage(image);
newImage.Position = (150, 300);
newImage.Name = "IronPPT";
newImage.Width = 450;
newImage.Height = 200;

ppt.Save("output.pptx");
Imports IronPPT
Imports IronPPT.Models

' Creating a new PresentationDocument object
Private ppt = New PresentationDocument()

Private image As New Image()
image.LoadFromFile("example.png")
Dim newImage = ppt.Slides(0).AddImage(image)
newImage.Position = (150, 300)
newImage.Name = "IronPPT"
newImage.Width = 450
newImage.Height = 200

ppt.Save("output.pptx")
$vbLabelText   $csharpLabel

輸出

在第一張幻燈片中添加圖片

4. 使用形狀和圖表自訂投影片

除了簡單的文字和圖片外,PowerPoint 簡報中經常包含諸如形狀等視覺元素。使用 IronPPT,我們可以像先前新增文字和圖片一樣輕鬆地在簡報檔案中新增形狀

作為示範,我們將創建一個圓形,並將其繪製到指定幻燈片上的特定位置。

using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument("example.pptx");

Shape shape = new Shape();
shape.Name = "Circle";
shape.Type = ShapeType.Ellipse;
shape.Width = 300;
shape.Height = 300;
shape.FillColor = Color.PowderBlue;
shape.OutlineColor = Color.Black;
shape.Position = (180, 200);

ppt.Slides[1].AddShape(shape);

ppt.Save("example.pptx");
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument("example.pptx");

Shape shape = new Shape();
shape.Name = "Circle";
shape.Type = ShapeType.Ellipse;
shape.Width = 300;
shape.Height = 300;
shape.FillColor = Color.PowderBlue;
shape.OutlineColor = Color.Black;
shape.Position = (180, 200);

ppt.Slides[1].AddShape(shape);

ppt.Save("example.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums

' Creating a new PresentationDocument object
Private ppt = New PresentationDocument("example.pptx")

Private shape As New Shape()
shape.Name = "Circle"
shape.Type = ShapeType.Ellipse
shape.Width = 300
shape.Height = 300
shape.FillColor = Color.PowderBlue
shape.OutlineColor = Color.Black
shape.Position = (180, 200)

ppt.Slides(1).AddShape(shape)

ppt.Save("example.pptx")
$vbLabelText   $csharpLabel

輸出

Csharp Create Powerpoint Tutorial 7 related to 4. 使用形狀和圖表自訂投影片

C# PowerPoint 自動化的最佳實踐

在自動化 PowerPoint 生成時,請考慮以下最佳實踐:

1. 優化大型簡報的性能

  • 限制幻燈片的數量以防止記憶體過載。
  • 使用壓縮圖像來減小文件大小。
  • 避免冗餘的物件以維持簡報的效率。

2. 保持一致的設計

  • 使用一致的字體和顏色以維持品牌形象。
  • 應用幻燈片範本以確保專業外觀。
  • 邏輯性地構建內容以提高可讀性。

3. 優雅地處理錯誤及例外

  • 實施錯誤處理來管理無效輸入。
  • 在插入圖像之前驗證圖像路徑。
  • 確保匯出的文件具有正確的格式和權限。

    藉由遵循這些最佳實踐,您可以創建高品質、專業的PowerPoint簡報,同時確保其可靠性和效率。

結論

在 C# 中使用IronPPT 自動化 PowerPoint 創建對於需要快速高效生成動態演示文稿的開發者來說是頗具變革性的工具。 無論您是在構建企業報告系統、教育工具還是業務儀表板,IronPPT 都能讓您在 .NET 應用程式中以程式化方式輕鬆創建和操作 PowerPoint 檔案。

IronPPT 提供所有您所需的無縫 PowerPoint 自動化功能,如幻燈片自訂、圖片插入和 PDF 匯出。

準備好試試看了嗎? 立即下載免費試用版,開始在C#中構建自動化PowerPoint演示文稿!

里根普恩
軟體工程師
Regan 畢業於雷丁大學,擁有電子工程學士學位。在加入 Iron Software 之前,他的工作角色讓他專注於單一任務;而他在 Iron Software 工作中最喜歡的是他所能承擔的工作範圍,無論是增加銷售價值、技術支持、產品開發或市場營銷。他喜歡了解開發人員如何使用 Iron Software 庫,並利用這些知識不斷改進文檔和開發產品。
下一個 >
如何使用 C# 將 PowerPoint 轉換為圖像

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

查看許可證 >