IronPPT入门指南
IronPowerPoint:适用于 .NET 的 PowerPoint 库
IronPPT是由Iron Software开发的PowerPoint库。 它在为 .NET 应用程序中处理 PowerPoint 演示文稿提供强大的功能方面表现出色。
- 加载、编辑和保存 PowerPoint 演示文稿。 轻松处理 .pptx 和 .ppt 文件。
- 幻灯片设置:配置幻灯片大小、方向、背景颜色和布局。
- 文本:处理文本内容、样式、拆分、追加文本和添加文本框。
- 文本样式:管理字体系列、大小、颜色、粗体、斜体、下划线和对齐方式。
- 形状:添加和操作形状,包括设置大小、位置、类型和旋转。
- 图片:在幻灯片中插入图片,并可设置缩放、对齐和定位等选项。
适用于 .NET 的 PowerPoint 演示文稿 C# 库
- 下载用于处理 PowerPoint 演示文稿的 C# 库
- 创建和修改 .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.");
}
}
}代码示例
让我们来看一些代码示例和可用功能。
创建 PowerPoint 文件
使用PresentationDocument类的构造函数之一实例化该类,即可创建 PowerPoint 演示文稿。 使用AddSlide和AddText方法分别添加幻灯片和文本。 之后,使用Save方法导出 PowerPoint 演示文稿。
:path=/static-assets/ppt/content-code-examples/get-started/get-started-1.csusing 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");添加形状
您可以使用幻灯片对象的AddShape方法添加形状。可以配置各种形状属性,例如填充颜色、轮廓颜色、位置、角度、类型等等。
:path=/static-assets/ppt/content-code-examples/get-started/get-started-2.csusing 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");添加图片
向任何幻灯片添加图片也是一件很简单的事情。 下面的代码示例向第一张幻灯片添加图像,修改图像的属性,例如位置、角度、名称、宽度和高度,然后将更新后的演示文稿保存为 .pptx 文件。
:path=/static-assets/ppt/content-code-examples/get-started/get-started-3.csusing 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");可用的许可和支持
IronPPT是一个商业库,但提供免费试用许可证。
有关 Iron Software 的更多详细信息,请访问我们的网站:https://ironsoftware.com/ 。 如果您需要帮助或有任何疑问,请联系我们的团队。
Iron 软件支持
如需一般帮助或有任何技术问题,请随时发送电子邮件至:support@ironsoftware.com 。







