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.");
        }
    }
}
''' <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>

Imports System ' Required for Console output
Imports IronPPT ' Ensure the IronPPT library is referenced in your project.

Namespace IronPPTApplication
	Friend Class Program
		Public Shared Sub Main(ByVal args() As String)
			' Calling the method to set the IronPPT license key.
			SetIronPPTLicense()
		End Sub

		''' <summary>
		''' Sets the license key for the IronPPT library to unlock its full features.
		''' </summary>
		Private Shared Sub 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.")
		End Sub
	End Class
End Namespace
$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");
Imports IronPPT

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

' Create a new PowerPoint presentation document
Private document = New PresentationDocument()

' Create a new slide object
Private 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");
Imports IronPPT
Imports IronPPT.Drawing ' Assuming this namespace contains `Shape` and `Color` classes
Imports IronPPT.Enums ' Assuming this namespace contains the `ShapeType` enum

' Load a PowerPoint presentation from the specified file
Private document = New PresentationDocument("output.pptx")

' Create and configure a new shape, in this case, a triangle
Private shape As New Shape With {
	.Name = "triangle",
	.Type = ShapeType.Triangle,
	.Width = 100,
	.Height = 100,
	.FillColor = New Color("#444444"),
	.OutlineColor = Color.Black,
	.Position = New System.Drawing.Point(200, 200)
}

' Ensure that the slides array has at least one slide to add the shape to
If document.Slides.Count > 0 Then
	' 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
End If

' 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");
Imports IronPPT
Imports 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
Private 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 Then
	document.Slides.Add()
End If

' Initialize an Image object
' Load an image from a file specified by the file path
' Ensure that "sample.png" exists at the specified path
Dim image As New Image()
image.LoadFromFile("sample.png")

' Add the image to the first slide of the presentation
Dim 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
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 3,325 | Version: 2025.11 刚刚发布