C# 投影片元素教學 - IronPPT

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

IronPPT 是一個可靠的PowerPoint 程式庫,旨在幫助 .NET C# 開發者將創建、閱讀和編輯PowerPoint 簡報的能力整合到他們的應用程式中。 在 PowerPoint 簡報中,投影片是建立和組織內容的基礎元素。

快速開始:將文字插入到新幻燈片或現有幻燈片中

這個例子展示了使用 IronPPT 如何輕鬆地在投影片中新增文字。 僅需幾行代碼,即可插入到第一張幻燈片(如果存在),或創建一張,然後保存 - 快速設定,簡化操作。

  1. 1使用 IronPPT 安裝 NuGet Package Manager

    PM > Install-Package IronPPT

  2. 2複製並運行這段程式碼。

    var doc = new IronPPT.PresentationDocument();
    var text = doc.Slides.Count > 0 ? doc.Slides[0].AddText("Quick Option") : doc.AddSlide(new IronPPT.Models.Slide()).AddText("Quick Option");
    doc.Save("quick.pptx");
  3. 3部署到您的生產環境進行測試

    立即在您的項目中開始使用 IronPPT,並使用 免費試用
    arrow pointer

目錄

新增文字

文字內容

無論您是建立新的簡報還是編輯現有的簡報,文字管理工具都能讓您完全控製文字的位置和格式,從而設計出清晰、專業地傳達訊息的幻燈片。

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.Text += " 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");

造型

文字樣式可讓您透過定義字體大小、顏色、樣式、刪除線和下劃線等屬性來自訂文字的視覺外觀。套用這些樣式可以增強文字的呈現效果,並改善文件的整體外觀。

using IronPPT;
using IronPPT.Models; // Ensure the library is available
using IronPPT.Enums;

// 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 = StrikValue.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"

新增圖片

調整影像設定以獲得最佳顯示效果。 正確的配置可確保圖像在視覺上吸引人,並與其上下文相符。

using IronPPT;
using IronPPT.Models;

// 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 an ElementPosition, which holds X and Y coordinates.
newImage.Position = (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");

添加形狀

透過定義形狀的類型、尺寸(寬度和高度)、填滿和輪廓顏色以及在投影片上的位置,可以輕鬆地在簡報中新增和自訂形狀。

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,
    Height = 100,
    FillColor = new Color("#444444"),
    OutlineColor = Color.Black,

    // Position is set via the Position property, which accepts an (x, y) tuple.
    // It's important that these coordinates are valid for display on the slide.
    Position = (200, 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在C#開發中用於什麼?

IronPPT是一個功能強大的程式庫,對C#開發者而言,允許在.NET應用程式中輕鬆整合以創建、讀取和編輯PowerPoint演示。

如何使用IronPPT向PowerPoint投影片添加文字?

您可以使用IronPPT的'AddText'方法在投影片對象上添加文字,可以將其附加到現有投影片或使用IronPPT程式庫方法創建新投影片。

如何在PowerPoint演示中應用IronPPT樣式於文字?

可以通過定義字型大小、顏色和樣式等屬性,使用'TextStyle'對象將樣式應用於IronPPT中的文字元素。

IronPPT可以用來將圖片添加到PowerPoint投影片中嗎?

是的,IronPPT可以用來將圖片添加到PowerPoint投影片中。它允許您從檔案加載圖片,調整其位置、尺寸,甚至應用旋轉以實現自定義演示。

IronPPT如何管理演示文稿中的形狀插入?

IronPPT透過允許開發者定義形狀類型、尺寸和顏色以及準確定位在投影片上來促進形狀插入,提高視覺內容動態效果。

使用IronPPT創建新簡報的基本步驟是什麼?

要創建新的演示文檔,先實例化'PresentationDocument',按照需要添加投影片,並使用提供的方法插入文字、圖片和形狀。保存您的文檔將生成.pptx檔案。

是否可以使用IronPPT修改現有投影片?

是的,IronPPT允許透過訪問文檔的投影片集合來修改現有投影片,您可以添加或編輯文字、圖片和形狀。

IronPPT是否支持文字校對和語言設置?

IronPPT支持禁用文字校對並允許指定文字格式的語言,使開發者對文字呈現屬性擁有更多控制。

IronPPT如何提升PowerPoint演示中的文字管理?

IronPPT透過讓您擁有對文字位置、格式、樣式及樣式屬性的完全控制來提升文字管理,讓演示清晰且專業。

IronPPT輸出什麼文件格式?

IronPPT以.pptx文件格式輸出演示,該格式與Microsoft PowerPoint及其他支持此格式的演示軟體相容。

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 7,166版本:2026.11剛剛發布