在企业自动化报告、批量生成教学课件、数据可视化输出等场景中,通过代码动态生成 PowerPoint 文档能够显著提升工作效率。本文将介绍如何基于 .NET 平台,使用 Free Spire.Presentation 组件以 C# 代码方式创建和编辑 PPTX 文档,涵盖幻灯片管理、文本、图片、形状等核心元素的操作方法。
一、环境准备
1.1 组件安装
在 Visual Studio 中,推荐通过 NuGet 包管理器引入组件。在包管理器控制台执行以下命令:
Install-Package FreeSpire.Presentation
或使用 .NET CLI:
dotnet add package FreeSpire.Presentation
安装完成后,在代码文件顶部引入所需命名空间:
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
1.2 核心对象模型
组件的 API 设计与 PowerPoint 原生对象模型对应,主要包含以下核心类:
Presentation:表示整个演示文稿文档,是所有操作的入口
ISlide:表示单张幻灯片,通过 Presentation.Slides 集合访问
IAutoShape:表示形状对象,文本内容通过 TextFrame 属性承载
IEmbedImage:表示嵌入到幻灯片中的图片对象
二、创建演示文稿与幻灯片管理
2.1 新建空白演示文稿
实例化 Presentation 对象时,会自动包含一张空白幻灯片。如果需要从零构建幻灯片集合,可以先移除默认页:
Presentation ppt = new Presentation();
ppt.Slides.RemoveAt(0);
2.2 添加与插入幻灯片
ISlide slide1 = ppt.Slides.Append();
ISlide slide2 = ppt.Slides.Insert(1);
int slideCount = ppt.Slides.Count;
2.3 设置幻灯片尺寸
默认幻灯片尺寸为标准 16:9 宽屏,可根据需求调整:
ppt.SlideSize.Type = SlideSizeType.Screen4x3;
ppt.SlideSize.Size = new SizeF(1024, 768);
三、添加文本内容
PowerPoint 中的文本承载于形状之内,通过添加矩形形状并设置其文本框架即可实现文本框效果。
3.1 基础文本框
ISlide slide = ppt.Slides[0];
RectangleF textRect = new RectangleF(100, 100, 500, 80);
IAutoShape textShape = slide.Shapes.AppendShape(ShapeType.Rectangle, textRect);
textShape.Fill.FillType = FillFormatType.None;
textShape.ShapeStyle.LineColor.Color = Color.Transparent;
textShape.TextFrame.Text = "C# 编程创建 PowerPoint 演示文稿";
3.2 文本格式设置
TextRange textRange = textShape.TextFrame.TextRange;
textRange.LatinFont = new TextFont("微软雅黑");
textRange.FontHeight = 32;
textRange.IsBold = TriState.True;
textRange.Fill.FillType = FillFormatType.Solid;
textRange.Fill.SolidColor.Color = Color.FromArgb(30, 80, 160);
textShape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
3.3 多段落文本
对于包含多个段落的内容,可通过段落集合进行管理:
TextFrame textFrame = textShape.TextFrame;
textFrame.Paragraphs.Clear();
TextParagraph para1 = new TextParagraph();
para1.Text = "第一段落内容";
para1.Alignment = TextAlignmentType.Left;
textFrame.Paragraphs.Append(para1);
TextParagraph para2 = new TextParagraph();
para2.Text = "第二段落内容";
para2.FirstLineIndent = 30;
textFrame.Paragraphs.Append(para2);
四、插入图片
4.1 嵌入本地图片
RectangleF imageRect = new RectangleF(150, 200, 400, 250);
IEmbedImage image = slide.Shapes.AppendEmbedImage(
ShapeType.Rectangle,
@"C:\images\sample.png",
imageRect
);
image.Line.FillFormat.FillType = FillFormatType.None;
4.2 设置幻灯片背景
slide.SlideBackground.Type = BackgroundType.Custom;
slide.SlideBackground.Fill.FillType = FillFormatType.Picture;
slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
Image bgImage = Image.FromFile(@"C:\images\background.jpg");
slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage =
ppt.Images.Append(bgImage);
五、保存与导出
5.1 保存为 PPTX 格式
ppt.SaveToFile("output.pptx", FileFormat.Pptx2019);
5.2 导出为其他格式
ppt.SaveToFile("output.pdf", FileFormat.PDF);
Image slideImage = ppt.Slides[0].SaveAsImage();
slideImage.Save("slide_0.png", System.Drawing.Imaging.ImageFormat.Png);
六、完整示例
以下是一个综合示例,创建包含封面页和内容页的演示文稿:
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace PptGenerator
{
class Program
{
static void Main(string[] args)
{
Presentation ppt = new Presentation();
ppt.Slides.RemoveAt(0);
ISlide coverSlide = ppt.Slides.Append();
IAutoShape titleShape = coverSlide.Shapes.AppendShape(
ShapeType.Rectangle,
new RectangleF(120, 180, 520, 80)
);
titleShape.Fill.FillType = FillFormatType.None;
titleShape.ShapeStyle.LineColor.Color = Color.Transparent;
titleShape.TextFrame.Text = "技术方案汇报";
TextRange titleRange = titleShape.TextFrame.TextRange;
titleRange.LatinFont = new TextFont("微软雅黑");
titleRange.FontHeight = 44;
titleRange.IsBold = TriState.True;
titleRange.Fill.SolidColor.Color = Color.FromArgb(30, 60, 120);
titleShape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
IAutoShape subtitleShape = coverSlide.Shapes.AppendShape(
ShapeType.Rectangle,
new RectangleF(120, 280, 520, 50)
);
subtitleShape.Fill.FillType = FillFormatType.None;
subtitleShape.ShapeStyle.LineColor.Color = Color.Transparent;
subtitleShape.TextFrame.Text = "——基于 .NET 平台的自动化方案";
TextRange subRange = subtitleShape.TextFrame.TextRange;
subRange.LatinFont = new TextFont("微软雅黑");
subRange.FontHeight = 20;
subRange.Fill.SolidColor.Color = Color.Gray;
subtitleShape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
ISlide contentSlide = ppt.Slides.Append();
IAutoShape pageTitle = contentSlide.Shapes.AppendShape(
ShapeType.Rectangle,
new RectangleF(50, 40, 600, 50)
);
pageTitle.Fill.FillType = FillFormatType.None;
pageTitle.ShapeStyle.LineColor.Color = Color.Transparent;
pageTitle.TextFrame.Text = "核心功能模块";
TextRange pageTitleRange = pageTitle.TextFrame.TextRange;
pageTitleRange.FontHeight = 28;
pageTitleRange.IsBold = TriState.True;
string[] items = { "数据采集模块", "分析处理引擎", "可视化输出", "报表自动生成" };
for (int i = 0; i < items.Length; i++)
{
IAutoShape itemShape = contentSlide.Shapes.AppendShape(
ShapeType.Rectangle,
new RectangleF(80, 120 + i * 50, 500, 40)
);
itemShape.Fill.FillType = FillFormatType.None;
itemShape.ShapeStyle.LineColor.Color = Color.Transparent;
itemShape.TextFrame.Text = $"• {items[i]}";
itemShape.TextFrame.TextRange.FontHeight = 20;
}
ppt.SaveToFile("presentation_demo.pptx", FileFormat.Pptx2019);
}
}
}
通过以上方法,可以在不安装 Microsoft Office 的环境下,以纯代码方式完成 PowerPoint 文档的创建与编辑,适用于服务端批量生成、自动化报告输出等场景。实际项目中可结合业务数据,进一步扩展模板替换、图表动态绑定等功能。