在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
PowerPoint 簡報 對於商業報告、銷售簡報、學術演講以及軟體生成的儀表板來說都是必不可少的。 然而,手動製作 PowerPoint 幻燈片可能既繁瑣又耗時。
如果不需要下載互操作庫就能在C#中自動化創建PowerPoint,那不是很棒嗎? 這就是 Iron Software 強大的 PowerPoint 程式庫,IronPPT 的用武之地。 IronPPT 是一個強大的 .NET 函式庫,允許開發人員程式化地輕鬆建立、修改和匯出 PowerPoint 簡報,而無需使用 Microsoft PowerPoint interop 函式庫。 無論您是需要動態生成幻燈片、插入圖表,還是將簡報轉換為PDF,IronPPT都能提供簡單且有效的解決方案。
今天,我們將探討如何使用 IronPPT 在 C# 中創建 PowerPoint 簡報。 我們將涵蓋:
PowerPoint 自動化的最佳實踐
讓我們開始吧! 🚀
在我們深入探討程式化地製作簡報之前,我們先來設置開發環境。
要使用IronPPT,請在您的C# 專案中安裝NuGet套件:
開啟Visual Studio。
打開套件管理器主控台(工具 > NuGet 套件管理員 > 套件管理器主控台)。
Install-Package IronPPT
4. 或者,您可以透過前往**「工具 -> NuGet 套件管理員 -> 為解決方案管理 NuGet 套件」**,搜尋 IronPPT,然後點擊「安裝」來進行安裝。

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");
}
}
輸出檔案
當我們在 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")
輸出
🔹 除了添加這些簡單的幻燈片之外,您還可以使用 IronPPT 添加多個幻燈片,並動態修改它們。 在我們的範例中,我們只在展示中新增了兩張投影片,但您可以根據需要添加更多投影片。
在您添加幻灯片后,下一步是插入內容。 IronPPT允許您添加:
自訂格式:字體、顏色和對齊。
例如,您可以為幻燈片添加一個標題,插入一張圖片,並根據您的佈局需求動態設定位置。 使用IronPPT的ParagraphStyle和TextStyle類別,我們可以在將文本添加到幻燈片時應用格式化和自訂樣式。
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")
輸出
在上面的代碼中,我們在標題畫面的現有文本框中添加了新文本。 我們還為第二張投影片創建了一個新的文字框,然後在裡面添加樣式化段落文本。 這是一個簡單的範例,說明如何使用 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")
輸出
除了簡單的文字和圖片外,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")
輸出
在自動化 PowerPoint 生成時,請考慮以下最佳實踐:
確保匯出的文件具有正確的格式和權限。
藉由遵循這些最佳實踐,您可以創建高品質、專業的PowerPoint簡報,同時確保其可靠性和效率。
在 C# 中使用IronPPT 自動化 PowerPoint 創建對於需要快速高效生成動態演示文稿的開發者來說是頗具變革性的工具。 無論您是在構建企業報告系統、教育工具還是業務儀表板,IronPPT 都能讓您在 .NET 應用程式中以程式化方式輕鬆創建和操作 PowerPoint 檔案。
IronPPT 提供所有您所需的無縫 PowerPoint 自動化功能,如幻燈片自訂、圖片插入和 PDF 匯出。
準備好試試看了嗎? 立即下載免費試用版,開始在C#中構建自動化PowerPoint演示文稿!