IronPPT入門指南

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPowerPoint:適用於 .NET 的 PowerPoint 程式庫

IronPPT是由Iron Software開發的PowerPoint庫。 它在為 .NET 應用程式中處理 PowerPoint 簡報提供強大的功能方面表現出色。

  • 載入、編輯和儲存 PowerPoint 簡報。 輕鬆處理 .pptx 和 .ppt 檔。
  • 幻燈片設定:配置幻燈片大小、方向、背景顏色和佈局。
  • 文字:處理文字內容、樣式、分割、追加文字和新增文字方塊。
  • 文字樣式:管理字型系列、大小、顏色、粗體、斜體、底線和對齊方式。
  • 形狀:新增和操作形狀,包括設定大小、位置、類型和旋轉。
  • 圖片:在投影片中插入圖片,並可設定縮放、對齊和定位等選項。

安裝

IronPPT庫

安裝 IronPPT 既快速又簡單。 使用以下方法新增軟體包:

Install-Package IronPPT

或者,您可以直接從IronPPT NuGet 官方網站下載。

安裝完成後,只需在 C# 程式碼頂部添加using IronPPT;即可開始使用。

應用許可證密鑰

若要使用 IronPPT,請透過設定LicenseKey屬性來套用有效的授權或試用金鑰。 在導入語句之後、呼叫任何 IronPPT 方法之前,立即加入以下程式碼:

:path=/static-assets/ppt/content-code-examples/get-started/get-started-license.cs
/// <summary>
/// This code sets the license key for the IronPPT library.
/// Ensure you have the correct namespace access by installing the IronPPT NuGet package
/// and adjust the license key appropriately for your use case.
/// </summary>

using System; // Required for Console output
using IronPPT; // Ensure the IronPPT library is referenced in your project.

namespace IronPPTApplication
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Calling the method to set the IronPPT license key.
            SetIronPPTLicense();
        }

        /// <summary>
        /// Sets the license key for the IronPPT library to unlock its full features.
        /// </summary>
        private static void SetIronPPTLicense()
        {
            // Correctly setting the license for the IronPPT library.
            // Replace "IRONPPT.MYLICENSE.KEY.1EF01" with your actual key.
            IronPPT.License.LicenseKey = "IRONPPT.MYLICENSE.KEY.1EF01";

            // Inform the user that the license key has been set.
            Console.WriteLine("IronPPT license key has been set.");
        }
    }
}
$vbLabelText   $csharpLabel

程式碼範例

讓我們來看一些程式碼範例和可用功能。

建立 PowerPoint 文件

使用PresentationDocument類別的建構函式之一實例化該類,即可建立 PowerPoint 簡報。 使用AddSlideAddText方法分別新增投影片和文字。 之後,使用Save方法匯出 PowerPoint 簡報。

:path=/static-assets/ppt/content-code-examples/get-started/get-started-1.cs
using IronPPT;

// This code demonstrates the creation of a PowerPoint presentation and saving it as a file.

// Create a new PowerPoint presentation document
var document = new PresentationDocument();

// Create a new slide object
var slide = new Slide();

// Add text content to the slide
slide.AddText("Hello!");

// Add the newly created slide with text to the document
document.AddSlide(slide);

// Export the PowerPoint presentation to a file named "output.pptx"
document.Save("output.pptx");
$vbLabelText   $csharpLabel

添加形狀

您可以使用投影片物件的AddShape方法新增形狀。可以配置各種形狀屬性,例如填滿顏色、輪廓顏色、位置、角度、類型等等。

:path=/static-assets/ppt/content-code-examples/get-started/get-started-2.cs
using IronPPT;
using IronPPT.Drawing; // Assuming this namespace contains `Shape` and `Color` classes
using IronPPT.Enums; // Assuming this namespace contains the `ShapeType` enum

// Load a PowerPoint presentation from the specified file
var document = new PresentationDocument("output.pptx");

// Create and configure a new shape, in this case, a triangle
Shape shape = new Shape
{
    Name = "triangle",             // Assign a name to the shape
    Type = ShapeType.Triangle,     // Set the shape type to Triangle
    Width = 100,                   // Set the width of the shape
    Height = 100,                  // Assumed height for the shape, should be set for visibility
    FillColor = new Color("#444444"), // Set the fill color of the shape
    OutlineColor = Color.Black,    // Set the outline color to black
    Position = new System.Drawing.Point(200, 200) // Set the position of the shape
};

// Ensure that the slides array has at least one slide to add the shape to
if (document.Slides.Count > 0)
{
    // Add the shape to the first slide
    document.Slides[0].AddShape(shape);
}
else
{
    // If there are no slides, handle the error or add a slide
    document.Slides.Add(new Slide()); // Assuming there's a way to add new slides
    document.Slides[0].AddShape(shape); // Add the shape to the newly added slide
}

// Export the PowerPoint presentation to a new file
document.Save("addShape.pptx");
$vbLabelText   $csharpLabel

新增圖片

在任何幻燈中添加圖片也是一件很簡單的事情。 下面的程式碼範例為第一張投影片新增影像,修改影像的屬性,例如位置、角度、名稱、寬度和高度,然後將更新後的簡報儲存為 .pptx 檔案。

:path=/static-assets/ppt/content-code-examples/get-started/get-started-3.cs
using IronPPT;
using System.Drawing;

// This code demonstrates creating a new PowerPoint presentation, adding an image to it,
// modifying the image's properties, and exporting the presentation.

// Create a new PowerPoint presentation
var document = new PresentationDocument();

// Ensure there's at least one slide in the presentation
// Create the first slide if it doesn't exist yet
if (document.Slides.Count == 0)
{
    document.Slides.Add();
}

// Initialize an Image object
// Load an image from a file specified by the file path
// Ensure that "sample.png" exists at the specified path
Image image = new Image(); 
image.LoadFromFile("sample.png");

// Add the image to the first slide of the presentation
var newImage = document.Slides[0].AddImage(image);

// Edit the image's properties
// Set the position of the image using X and Y coordinates
newImage.Position = new Point(200, 200);

// Set the rotation angle of the image in degrees
newImage.Angle = 45;

// Set a name for the image, which can be useful for identification
newImage.Name = "new image";

// Set the dimensions of the image
newImage.Width = 150;
newImage.Height = 150;

// Export the PowerPoint presentation with the new image
document.Save("addImage.pptx");
$vbLabelText   $csharpLabel

提供許可和支持

IronPPT是一個商業庫,但提供免費試用許可證

有關 Iron Software 的更多詳細信息,請訪問我們的網站:https://ironsoftware.com/ 。 如果您需要協助或有任何疑問,請聯絡我們的團隊

Iron 軟體支援

如需一般協助或有任何技術問題,請隨時發送電子郵件至:support@ironsoftware.com

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 3,739 | 版本: 2025.12 剛剛發布