介绍

Aspose.Slides FOSS for .NET 提供了一套完整的类,用于在 C# 中完全管理 PowerPoint 演示文稿。该库允许您从头创建新的 .pptx 文件,加载已有文件,操作幻灯片和形状,并将结果保存回磁盘——全部无需安装 Microsoft Office 或任何外部依赖。

Aspose.Slides.Foss NuGet 包针对 .NET 9.0,且在 Windows、macOS、Linux 和 Docker 容器上运行完全相同。它在 MIT 许可证下发布,因此您可以在商业和开源项目中无限制地使用它。

本文介绍了核心的演示文稿管理功能:处理幻灯片、构建形状层次结构、组织文本、管理表格以及保存为 PPTX。


包含内容

创建和加载演示文稿

Presentation 类是所有操作的入口点。您可以创建一个空的演示文稿,从文件路径加载,或从流中读取。该类实现了 IDisposable,因此将其包装在 using 语句中可确保资源得到正确释放。

// Create a brand-new, empty presentation
using var pres = new Presentation();

// Load an existing file
using var existing = new Presentation("report.pptx");

Presentation 对象通过 SlidesMastersLayoutSlidesSectionsCommentAuthorsImagesNotesSize 等属性公开完整的幻灯片层次结构。

使用 Slides

SlideCollection 返回的 Presentation.Slides 支持添加、克隆、插入、重新排序和删除幻灯片。您可以在同一演示文稿或跨演示文稿中克隆幻灯片,同时保留或重新映射布局。

using var pres = new Presentation();

// The default presentation contains one blank slide
var firstSlide = pres.Slides[0];

// Clone the first slide and append it
pres.Slides.AddClone(firstSlide);

// Insert a clone at a specific position
pres.Slides.InsertClone(1, firstSlide);

// Remove a slide by index
pres.Slides.RemoveAt(2);

// Total slide count
int count = pres.Slides.Count;

每个Slide公开SlideNumberHiddenLayoutSlideNotesSlideManagerName。您可以使用foreach遍历该集合,或使用ToArray()将其转换为数组。

形状和自动形状

每张幻灯片都包含一个 ShapeCollection,可通过 Slide.Shapes 访问。该集合提供最常见形状类型的工厂方法。

using var pres = new Presentation();
var slide = pres.Slides[0];

// Add a rectangle AutoShape
var rect = slide.Shapes.AddAutoShape(
    ShapeType.Rectangle, 50, 50, 300, 150);

// Add a connector
var connector = slide.Shapes.AddConnector(
    ShapeType.BentConnector3, 100, 200, 200, 50);

// Reorder a shape to bring it forward
slide.Shapes.Reorder(0, rect);

// Remove all shapes
slide.Shapes.Clear();

AddAutoShapeInsertAutoShape 方法接受一个 ShapeType 枚举值以及位置和大小坐标。AddConnectorInsertConnectorAddPictureFrameInsertPictureFrame 遵循相同的模式。

文本: 段落和部分

演示文稿中的文本位于 TextFrame,其中包含一个 ParagraphCollection。每个 Paragraph 包含一个 PortionCollection,该 PortionCollectionPortion 对象组成,代表具有独立格式的单个文本运行。

using var pres = new Presentation();
var slide = pres.Slides[0];

var shape = slide.Shapes.AddAutoShape(
    ShapeType.Rectangle, 50, 50, 400, 200);

// Access the shape's text frame
var tf = shape.TextFrame;

// Read or set the full text
tf.Text = "Hello, presentation world!";

// Work with individual paragraphs and portions
var para = new Paragraph();
var portion = new Portion("Formatted run");
para.Portions.Add(portion);
tf.Paragraphs.Add(para);

每个Portion都有一个PortionFormat属性,用于控制字体、大小、粗体/斜体、颜色、下划线以及其他字符级属性。ParagraphFormat控制对齐、项目符号样式、间距和缩进。

表格

表格通过 ShapeCollection.AddTable 添加,该方法接受列宽和行高的数组。返回的 Table 对象提供对 RowsColumns 和各个 Cell 对象的访问。

using var pres = new Presentation();
var slide = pres.Slides[0];

double[] colWidths = { 150, 150, 150 };
double[] rowHeights = { 40, 40, 40 };

var table = slide.Shapes.AddTable(
    50, 50, colWidths, rowHeights);

// Set text in a cell
table.Rows[0][0].TextFrame.Text = "Header 1";
table.Rows[0][1].TextFrame.Text = "Header 2";
table.Rows[0][2].TextFrame.Text = "Header 3";

// Merge cells
table.MergeCells(table.Rows[1][0], table.Rows[1][2], false);

// Style presets
table.StylePreset = TableStylePreset.MediumStyle2Accent1;

保存演示文稿

Presentation.Save 方法将文档写入文件或流。SaveFormat 枚举指定输出格式。PPTX 是主要支持的输出格式。

using var pres = new Presentation();
// ... build slides ...

// Save to a file
pres.Save("output.pptx", SaveFormat.Pptx);

// Save to a stream
using var ms = new MemoryStream();
pres.Save(ms, SaveFormat.Pptx);

// Save specific slides by index
pres.Save("subset.pptx", new[] { 1, 3 }, SaveFormat.Pptx);

您还可以传递一个 SaveOptions 对象,以便对输出进行额外控制。


快速入门

通过 .NET CLI 安装包:

dotnet add package aspose.slides.foss

然后创建并保存一个最小的演示文稿:

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

// 1. Create a new presentation
using var pres = new Presentation();

// 2. Get the default slide
var slide = pres.Slides[0];

// 3. Add a title shape
var title = slide.Shapes.AddAutoShape(
    ShapeType.Rectangle, 50, 30, 600, 60);
title.TextFrame.Text = "Quarterly Report";

// 4. Add a content shape
var body = slide.Shapes.AddAutoShape(
    ShapeType.Rectangle, 50, 120, 600, 300);
var para = new Paragraph();
para.Portions.Add(new Portion("Revenue grew 12% year-over-year."));
body.TextFrame.Paragraphs.Add(para);

// 5. Save as PPTX
pres.Save("quarterly-report.pptx", SaveFormat.Pptx);

支持的格式

格式扩展名读取写入
PPTX.pptx
PPSX.ppsx
PPTM.pptm
POTX.potx
ODP.odp
FODP.fodp

注意: 该库原生读取和写入 PPTX。API 级别声明的 SaveFormat 枚举中列出的其他格式;实际导出保真度可能有所不同。旧版 .ppt 二进制格式不受支持。


开源与许可

Aspose.Slides FOSS for .NET 在 MIT 许可证下发布。完整源代码可在 GitHub 上获取。您可以在商业和开源项目中使用它,修改代码,并在不受限制的情况下重新分发。无需 API 密钥、许可证文件或注册。


快速入门