C# Slide Element Tutorial – IronPPT
IronPPT 是一款功能強大的 PowerPoint 程式庫,旨在協助 .NET C# 開發人員將建立、讀取及編輯 PowerPoint 簡報的功能無縫整合至其應用程式中。 在 PowerPoint 簡報的語境中,投影片是結構化與組織內容的基礎元素。
快速入門:將文字插入新投影片或現有投影片
此範例展示了您如何輕鬆地使用 IronPPT 在投影片中加入文字。 只需幾行代碼,若已有第一張投影片則插入其中,否則直接建立一張,然後節省設定時間,省時省力。
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPPT
PM > Install-Package IronPPT -
請複製並執行此程式碼片段。
var doc = new IronPPT.PresentationDocument(); var text = doc.Slides.Co/unt > 0 ? doc.Slides[0].AddText("Quick Option") : doc.Slides.Add(new IronPPT.Models.Slide()).AddText("Quick Option"); doc.Save("quick.pptx"); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronPPT
目錄
新增文字
文字內容
無論您是製作新的簡報,還是編輯現有的簡報,文字管理工具都能讓您完全掌控文字的排版與格式,讓您設計出能清晰且Professional地傳達訊息的投影片。
:path=/static-assets/ppt/content-code-examples/tutorials/slide-element-add-text.cs
using IronPPT;
using IronPPT.Models;
// Create a new PowerPoint presentation
var document = new PresentationDocument();
// Ensure there is at least one slide to work with
if (document.Slides.Count == 0)
{
document.Slides.Add(new Slide());
}
// Add text to the first slide
var text = document.Slides[0].AddText("Hello");
// Append text to the existing text on the slide
text.Content += " There!";
// Check if there is any text element to remove from the first slide
if (document.Slides[0].Texts.Count > 0)
{
document.Slides[0].Texts[0].Remove();
}
// Export the PowerPoint presentation with the specified file name
document.Save("addText.pptx");
Imports IronPPT
Imports IronPPT.Models
' Create a new PowerPoint presentation
Private document = New PresentationDocument()
' Ensure there is at least one slide to work with
If document.Slides.Count = 0 Then
document.Slides.Add(New Slide())
End If
' Add text to the first slide
Dim text = document.Slides(0).AddText("Hello")
' Append text to the existing text on the slide
text.Content &= " There!"
' Check if there is any text element to remove from the first slide
If document.Slides(0).Texts.Count > 0 Then
document.Slides(0).Texts(0).Remove()
End If
' Export the PowerPoint presentation with the specified file name
document.Save("addText.pptx")
設定樣式
透過文字樣式設定,您可以透過定義字型大小、顏色、樣式、刪除線和底線等屬性,自訂文字的外觀。套用這些樣式可提升文字的呈現效果,並改善文件的整體視覺效果。
:path=/static-assets/ppt/content-code-examples/tutorials/slide-element-text-style.cs
using IronPPT;
using IronPPT.Models; // Ensure the library is available
// Create a new presentation document
var document = new PresentationDocument();
// Define and customize the text style
var textStyle = new TextStyle
{
IsBold = true, // Text is bold
IsItalic = true, // Text is italic
Color = Color.Blue, // Text color is blue
Strike = StrikeValue.SingleStrike, // Text is single struck-off
Outline = true, // Text has an outline
NoProof = true, // Disables proofing for the text
Spacing = 10.0, // Text spacing is set to 10
Underline = new Underline
{
LineValue = UnderlineValues.Single, // Single underline
Color = Color.Red // Underline color is red
},
Languages = "en-US", // Text language is set to U.S. English
SpecVanish = false, // Text does not vanish when special formatting is applied
};
// Create text content and apply the defined style
var text = new Text("Hello World"); // Instantiate text with a string
text.TextStyle = textStyle; // Apply the defined style to the text
// Add a new slide if none exist
if (document.Slides.Count == 0)
{
document.Slides.Add(new Slide()); // Add a new slide to the document
}
// Add the styled text to the first slide
document.Slides[0].AddText(text); // Add the newly created text object to the first slide
// Save the presentation document to a file
document.Save("textStyle.pptx"); // Save the document with the filename "textStyle.pptx"
Imports IronPPT
Imports IronPPT.Models ' Ensure the library is available
' Create a new presentation document
Private document = New PresentationDocument()
' Define and customize the text style
Private textStyle = New TextStyle With {
.IsBold = True,
.IsItalic = True,
.Color = Color.Blue,
.Strike = StrikeValue.SingleStrike,
.Outline = True,
.NoProof = True,
.Spacing = 10.0,
.Underline = New Underline With {
.LineValue = UnderlineValues.Single,
.Color = Color.Red
},
.Languages = "en-US",
.SpecVanish = False
}
' Create text content and apply the defined style
Private text = New Text("Hello World") ' Instantiate text with a string
text.TextStyle = textStyle ' Apply the defined style to the text
' Add a new slide if none exist
If document.Slides.Count = 0 Then
document.Slides.Add(New Slide()) ' Add a new slide to the document
End If
' Add the styled text to the first slide
document.Slides(0).AddText(text) ' Add the newly created text object to the first slide
' Save the presentation document to a file
document.Save("textStyle.pptx") ' Save the document with the filename "textStyle.pptx"
添加圖片
請調整圖片設定以獲得最佳顯示效果。 正確的配置可確保圖片視覺上美觀且與上下文相符。
:path=/static-assets/ppt/content-code-examples/tutorials/slide-element-add-image.cs
using IronPPT;
using IronPPT.Models;
using System.Drawing;
// This script demonstrates the creation of a PowerPoint presentation using the IronPPT library.
// An image is added to the presentation, its properties are modified, and then the presentation is saved.
// Create a new PowerPoint presentation
var document = new PresentationDocument();
// Create a new Image object and load an image file.
var image = new Image();
image.LoadFromFile("sample.png");
// Add the image to the first slide (index 0) of the presentation.
var newImage = document.AddImage(image, 0);
// Set the properties of the added image.
// Position property is set using a Point object, which holds X and Y coordinates.
newImage.Position = new Point(200, 200); // Set image position on the slide
newImage.Angle = 45; // Set the rotation angle of the image
newImage.Name = "new image"; // Assign a descriptive name to the image
newImage.Width = 150; // Set the width of the image in pixels
newImage.Height = 150; // Set the height of the image in pixels
// Export the PowerPoint presentation to a file named "addImage.pptx"
document.Save("addImage.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports System.Drawing
' This script demonstrates the creation of a PowerPoint presentation using the IronPPT library.
' An image is added to the presentation, its properties are modified, and then the presentation is saved.
' Create a new PowerPoint presentation
Private document = New PresentationDocument()
' Create a new Image object and load an image file.
Private image = New Image()
image.LoadFromFile("sample.png")
' Add the image to the first slide (index 0) of the presentation.
Dim newImage = document.AddImage(image, 0)
' Set the properties of the added image.
' Position property is set using a Point object, which holds X and Y coordinates.
newImage.Position = New Point(200, 200) ' Set image position on the slide
newImage.Angle = 45 ' Set the rotation angle of the image
newImage.Name = "new image" ' Assign a descriptive name to the image
newImage.Width = 150 ' Set the width of the image in pixels
newImage.Height = 150 ' Set the height of the image in pixels
' Export the PowerPoint presentation to a file named "addImage.pptx"
document.Save("addImage.pptx")
新增圖形
透過定義形狀的類型、尺寸(寬度和高度)、內填色與輪廓色,以及在投影片上的位置,即可輕鬆在簡報中新增並自訂形狀。
:path=/static-assets/ppt/content-code-examples/tutorials/slide-element-add-shape.cs
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;
// Load a PowerPoint presentation.
// The PresentationDocument is assumed to represent an entire PPTX file loaded from disk.
var document = new PresentationDocument("output.pptx");
// Configure a new shape.
// Shape is assumed to be a model object representing drawable elements on a slide.
Shape shape = new Shape
{
Name = "triangle",
Type = ShapeType.Triangle,
Width = 100,
FillColor = new Color("#444444"),
OutlineColor = Color.Black,
// Position is set via assumed X and Y positioning properties.
// It's important that these properties are set to valid coordinates for display on the slide.
XPosition = 200,
YPosition = 200
};
// Add the shape to the first slide in the presentation.
// Slides[0] refers to the first slide in the collection. Ensure a slide exists at this index.
document.Slides[0].AddShape(shape);
// Export the modified PowerPoint presentation.
// Saves the changes to a new file, ensuring the original presentation is not overwritten.
document.Save("addShape.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums
' Load a PowerPoint presentation.
' The PresentationDocument is assumed to represent an entire PPTX file loaded from disk.
Private document = New PresentationDocument("output.pptx")
' Configure a new shape.
' Shape is assumed to be a model object representing drawable elements on a slide.
Private shape As New Shape With {
.Name = "triangle",
.Type = ShapeType.Triangle,
.Width = 100,
.FillColor = New Color("#444444"),
.OutlineColor = Color.Black,
.XPosition = 200,
.YPosition = 200
}
' Add the shape to the first slide in the presentation.
' Slides[0] refers to the first slide in the collection. Ensure a slide exists at this index.
document.Slides(0).AddShape(shape)
' Export the modified PowerPoint presentation.
' Saves the changes to a new file, ensuring the original presentation is not overwritten.
document.Save("addShape.pptx")
常見問題
IronPPT 的用途為何?
IronPPT 是一款專為 .NET C# 開發者設計的全方位 PowerPoint 程式庫,讓開發者能夠在應用程式中無縫地建立、讀取及編輯 PowerPoint 簡報。
如何使用 IronPPT 在投影片中加入文字?
若要使用 IronPPT 在投影片中加入文字,您只需透過幾行程式碼,即可將文字插入現有投影片或建立新投影片,然後儲存簡報。
我可以在 IronPPT 中自訂文字樣式嗎?
是的,IronPPT 允許您透過定義字型大小、顏色、樣式、刪除線和底線等屬性來自訂文字樣式,以提升文字的外觀。
如何使用 IronPPT 在 PowerPoint 簡報中加入圖片?
IronPPT 提供工具,可從檔案或 FileStream 載入圖片,並設定其尺寸、角度及位置,以確保在簡報中呈現最佳效果。
IronPPT 是否支援新增圖形?
是的,您可以在 IronPPT 中新增並自訂圖形,透過定義其類型、尺寸、內填色與輪廓色,以及在投影片上的位置來進行調整。
IronPPT 是否支援編輯現有的 PowerPoint 簡報?
IronPPT 讓您既能建立新的簡報,也能編輯現有的簡報,讓您能完全掌控內容與格式。
我可以在 IronPPT 中設定文字和圖片的位置嗎?
是的,IronPPT 讓您能夠設定投影片上文字與圖片的位置,可進行精確定位以配合您的簡報版面配置。
IronPPT 支援哪些圖片插入的檔案格式?
IronPPT 支援插入多種檔案格式的圖片,確保與您的簡報視覺素材相容。
IronPPT 如何提升簡報的視覺吸引力?
IronPPT 透過提供自訂文字樣式、圖片配置及圖形設計的工具,提升視覺吸引力,確保簡報呈現 Professional 外觀。
IronPPT 是否相容於不同版本的 PowerPoint?
IronPPT 設計上相容於各種版本的 PowerPoint,可無縫整合至您的 C# 應用程式中。

