入门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 文件。
  • SlideSetup:配置幻灯片大小、方向、背景颜色和布局。
  • 处理文本内容、样式、拆分、追加文本和添加文本框。
  • 文本样式:管理字体、大小、颜色、加粗、斜体、下划线和对齐方式。
  • 形状:添加和操作形状,包括设置大小、位置、类型和旋转。
  • 图像:将图像插入幻灯片,并提供缩放、对齐和定位选项。

安装

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
IronPPT.License.LicenseKey = "IRONPPT.MYLICENSE.KEY.1EF01";
IronPPT.License.LicenseKey = "IRONPPT.MYLICENSE.KEY.1EF01"
VB   C#

代码示例

让我们探索一些代码示例和可用的功能。

创建 PowerPoint 文件

通过使用其构造函数之一实例化PresentationDocument类来创建PowerPoint演示文稿。 使用 AddSlide 和 AddText 方法分别添加幻灯片和文本。 之后,使用Save方法导出PowerPoint演示文稿。

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

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

// Add empty slide
document.AddSlide();

// Add slide object
Slide slide = new Slide();
slide.AddText("Hello!");
document.AddSlide(slide);

// Export PowerPoint presentation
document.Save("output.pptx");
Imports IronPPT

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

' Add empty slide
document.AddSlide()

' Add slide object
Dim slide As New Slide()
slide.AddText("Hello!")
document.AddSlide(slide)

' Export PowerPoint presentation
document.Save("output.pptx")
VB   C#

添加形状

您可以使用幻灯片对象中的 AddShape 方法来添加形状。可以配置各种形状属性,例如填充颜色、轮廓颜色、位置、角度、类型等。

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

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

// Configure new shape
Shape shape = new Shape();
shape.Name = "triangle";
shape.Type = IronPPT.Enums.ShapeType.Triangle;
shape.Width = 100;
shape.FillColor = new Color("#444444");
shape.OutlineColor = Color.Black;
shape.Position = (200, 200);

// Add shape
document.Slides[0].AddShape(shape);

// Export PowerPoint presentation
document.Save("addShape.pptx");
Imports IronPPT

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

' Configure new shape
Private shape As New Shape()
shape.Name = "triangle"
shape.Type = IronPPT.Enums.ShapeType.Triangle
shape.Width = 100
shape.FillColor = New Color("#444444")
shape.OutlineColor = Color.Black
shape.Position = (200, 200)

' Add shape
document.Slides(0).AddShape(shape)

' Export PowerPoint presentation
document.Save("addShape.pptx")
VB   C#

添加图片

在幻灯片中添加图像也是一项简单的任务。 下面的代码示例向第一张幻灯片添加图像,修改图像的属性,如位置、角度、名称、宽度和高度,然后将更新后的演示文稿保存为 .pptx 文件。

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

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

// Add image
Image image = new Image();
image.LoadFromFile("sample.png");
var newImage = document.Slides[0].AddImage(image);

// Edit image's properties
newImage.Position = (200, 200);
newImage.Angle = 45;
newImage.Name = "new image";
newImage.Width = 150;
newImage.Height = 150;

// Export PowerPoint presentation
document.Save("addImage.pptx");
Imports IronPPT

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

' Add image
Private image As New Image()
image.LoadFromFile("sample.png")
Dim newImage = document.Slides(0).AddImage(image)

' Edit image's properties
newImage.Position = (200, 200)
newImage.Angle = 45
newImage.Name = "new image"
newImage.Width = 150
newImage.Height = 150

' Export PowerPoint presentation
document.Save("addImage.pptx")
VB   C#

许可与支持可用

IronPPT 是一个商业库,但提供免费试用许可证这里.

有关Iron Software的更多详细信息,请访问我们的网站:https://ironsoftware.com/。 如果您需要支持或有任何疑问,请联系我们的团队.

Iron Software 支持

如需一般帮助和技术问题,欢迎通过以下电子邮件联系我们:support@ironsoftware.com