MS PowerPoint 演示文稿允许您创建包含文本、图像、图表、动画和其他元素的幻灯片。各种附加格式选项可让您的演示文稿更具吸引力。在这篇文章中,将了解如何以编程方式创建此类演示文稿,以及将学习如何使用 C# 创建包含文本、表格、图像和图表的 PPTX 演示文稿。

  • 创建 PowerPoint 演示文稿
  • 打开现有的 PowerPoint 演示文稿
  • 将幻灯片添加到演示文稿
  • 将文本添加到演示文稿的幻灯片
  • 在演示文稿中创建表格
  • 在演示文稿中创建图表

Aspose.Slides for .NET是一个演示操作 API,允许您从 .NET 应用程序中创建和操作 PowerPoint 文档。API 提供了实现基本和高级 PowerPoint 自动化功能所需的几乎所有可能的功能。

>>你可以下载Aspose.Slides 最新版测试体验。


在 C# 中创建 PowerPoint 演示文稿

让我们首先使用 Aspose.Slides for .NET 创建一个空的 PowerPoint 演示文稿。以下是执行此操作的步骤。

  • 创建Presentation类的实例。
  • 使用Presentation.Save(String, SaveFormat)方法将其保存为 PPTX 。

以下代码示例展示了如何在 C# 中创建 PowerPoint 演示文稿。

// Instantiate a Presentation object that represents a presentation file
using (Presentation presentation = new Presentation())
{// Get the first slideISlide slide = presentation.Slides[0];// Add content to slide...// Save presentationpresentation.Save("NewPresentation.pptx", SaveFormat.Pptx);
}

在 C# 中打开现有的 PowerPoint 演示文稿

无需付出额外的努力即可打开现有的 PowerPoint 演示文稿。只需将 PPTX 文件的路径提供给Presentation类的构造函数即可完成。以下代码示例展示了如何打开现有的 PPTX 演示文稿。

// Opening the presentation file by passing the file path to the constructor of Presentation class
Presentation pres = new Presentation("OpenPresentation.pptx");// Printing the total number of slides in the presentation
System.Console.WriteLine(pres.Slides.Count.ToString());

在 C# 中向演示文稿添加幻灯片

创建演示文稿后,您可以开始向其中添加幻灯片。以下是使用 Aspose.Slides for .NET 在演示文稿中添加幻灯片的步骤。

  • 创建Presentation 类的实例。
  • 通过设置对Presentations.Slides属性的引用来实例化 ISlideCollection类。
  • 使用ISlideCollection对象公开的Slide.AddEmptySlide (ILayoutSlide)方法将空幻灯片添加到演示文稿
  • 使用Presentation.Save(String, SaveFormat) 方法保存演示文件 。

以下代码示例展示了如何使用 C# 在 PowerPoint 演示文稿中添加幻灯片。

// Instantiate Presentation class that represents the presentation file
using (Presentation pres = new Presentation())
{// Instantiate SlideCollection calssISlideCollection slds = pres.Slides;for (int i = 0; i < pres.LayoutSlides.Count; i++) { // Add an empty slide to the Slides collection slds.AddEmptySlide(pres.LayoutSlides[i]); } // Save the PPTX file to the Disk pres.Save("EmptySlide_out.pptx", SaveFormat.Pptx); }

使用 C# 在幻灯片中插入文本

现在我们可以向 PowerPoint 演示文稿中的幻灯片添加内容。让我们首先使用以下步骤向幻灯片添加一段文本。

  • 使用Presentation 类创建一个新的演示文稿。
  • 获取演示文稿中幻灯片的参考。
  • 添加 IAutoShape 与 ShapeType 如矩形在幻灯片的一个指定位置。
  • 获取新添加的IAutoShape对象的引用。
  • 将TextFrame添加到包含默认文本的自选图形。
  • 将演示文稿另存为 PPTX 文件。

以下代码示例展示了如何使用 C# 向演示文稿中的幻灯片添加文本。

// Instantiate PresentationEx// Instantiate PresentationEx
using (Presentation pres = new Presentation())
{// Get the first slideISlide sld = pres.Slides[0];// Add an AutoShape of Rectangle typeIAutoShape ashp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);// Add TextFrame to the Rectangleashp.AddTextFrame(" ");// Accessing the text frameITextFrame txtFrame = ashp.TextFrame;// Create the Paragraph object for text frameIParagraph para = txtFrame.Paragraphs[0];// Create Portion object for paragraphIPortion portion = para.Portions[0];// Set Textportion.Text = "Aspose TextBox";// Save the presentation to diskpres.Save("presentation.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

使用 C# 在演示文稿中创建表格

Aspose.Slides for .NET 提供了一种在演示文档中创建表格的简单方法。以下是它的步骤。

  • 创建Presentation 类的实例。
  • 使用幻灯片的索引获取幻灯片的参考。
  • 定义具有宽度的列和具有高度的行的数组。
  • 使用由IShapes对象公开的Slide.Shapes.AddTable()方法将表格添加到幻灯片,并获取对ITable实例中表格的引用。
  • 遍历每个单元格以应用格式。
  • 使用Table.Rows[][].TextFrame.Text属性向单元格添加文本。
  • 将演示文稿另存为 PPTX 文件。

以下代码示例展示了如何在 PowerPoint 演示文稿的幻灯片中创建表格。

// Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();// Access first slide
ISlide sld = pres.Slides[0];// Define columns with widths and rows with heights
double[] dblCols = { 50, 50, 50 };
double[] dblRows = { 50, 30, 30, 30, 30 };// Add table shape to slide
ITable tbl = sld.Shapes.AddTable(100, 50, dblCols, dblRows);// Set border format for each cell
for (int row = 0; row < tbl.Rows.Count; row++) { for (int cell = 0; cell < tbl.Rows[row].Count; cell++) { tbl.Rows[row][cell].CellFormat.BorderTop.FillFormat.FillType = FillType.Solid; tbl.Rows[row][cell].CellFormat.BorderTop.FillFormat.SolidFillColor.Color = Color.Red; tbl.Rows[row][cell].CellFormat.BorderTop.Width = 5; tbl.Rows[row][cell].CellFormat.BorderBottom.FillFormat.FillType = (FillType.Solid); tbl.Rows[row][cell].CellFormat.BorderBottom.FillFormat.SolidFillColor.Color= Color.Red; tbl.Rows[row][cell].CellFormat.BorderBottom.Width =5; tbl.Rows[row][cell].CellFormat.BorderLeft.FillFormat.FillType = FillType.Solid; tbl.Rows[row][cell].CellFormat.BorderLeft.FillFormat.SolidFillColor.Color =Color.Red; tbl.Rows[row][cell].CellFormat.BorderLeft.Width = 5; tbl.Rows[row][cell].CellFormat.BorderRight.FillFormat.FillType = FillType.Solid; tbl.Rows[row][cell].CellFormat.BorderRight.FillFormat.SolidFillColor.Color = Color.Red; tbl.Rows[row][cell].CellFormat.BorderRight.Width = 5; } } // Merge cells 1 & 2 of row 1 tbl.MergeCells(tbl.Rows[0][0], tbl.Rows[1][1], false); // Add text to the merged cell tbl.Rows[0][0].TextFrame.Text = "Merged Cells"; // Save PPTX to Disk pres.Save("table.pptx", SaveFormat.Pptx);

使用 C# 在演示文稿中创建图表

以下是使用 C# 在 PowerPoint 演示文稿中添加图表的步骤。

  • 创建Presentation 类的实例 。
  • 通过索引获取幻灯片的参考。
  • 使用ISlide.Shapes.AddChart(ChartType, Single, Single, Single, Single)方法添加所需类型的图表。
  • 添加图表标题。
  • 访问图表数据工作表。
  • 清除所有默认系列和类别。
  • 添加新系列和类别。
  • 为图表系列添加新的图表数据。
  • 设置图表系列的填充颜色。
  • 添加图表系列标签。
  • 将演示文稿另存为 PPTX 文件。

以下代码示例展示了如何使用 C# 在演示文稿中添加图表。

// Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();// Access first slide
ISlide sld = pres.Slides[0];// Add chart with default data
IChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);// Setting chart Title
// Chart.ChartTitle.TextFrameForOverriding.Text = "Sample Title";
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;// Set first series to Show Values
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;// Getting the chart data worksheet
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;// Delete default generated series and categories
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
int s = chart.ChartData.Series.Count;
s = chart.ChartData.Categories.Count;// Adding new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);// Adding new categories
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));// Take first chart series
IChartSeries series = chart.ChartData.Series[0];// Now populating series dataseries.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));// Setting fill color for series
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Red;// Take second chart series
series = chart.ChartData.Series[1];// Now populating series data
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));// Setting fill color for series
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Green;// First label will be show Category name
IDataLabel lbl = series.DataPoints[0].Label;
lbl.DataLabelFormat.ShowCategoryName = true;lbl = series.DataPoints[1].Label;
lbl.DataLabelFormat.ShowSeriesName = true;// Show value for third label
lbl = series.DataPoints[2].Label;
lbl.DataLabelFormat.ShowValue = true;
lbl.DataLabelFormat.ShowSeriesName = true;
lbl.DataLabelFormat.Separator = "/";// Save presentation with chart
pres.Save("AsposeChart_out.pptx", SaveFormat.Pptx);

如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询

软件功能开发,在 C# 中创建 MS PowerPoint 演示文稿相关推荐

  1. 计算机故障排除ppt,在 PowerPoint 中对损坏的演示文稿进行故障排除 | Microsoft Docs...

    如何在 PowerPoint 中对损坏的演示文稿进行故障排除 2021/4/16 适用于: PowerPoint 2019, PowerPoint 2016, PowerPoint 2013, Pow ...

  2. 将其他程序中的大纲文本插入到 PowerPoint 演示文稿中

    如果要根据现有报告或其他分级显示的文档(在 Microsoft Office Word 2007 中或其他任何支持使用标题样式的程序中创建)制作演示文稿,则可以通过将文档插入到 Microsoft O ...

  3. PPT处理控件Aspose.Slides功能演示:使用 C# 在 PowerPoint 演示文稿中创建 SmartArt

    演示文稿中的 SmartArt 用于以视觉形式提供信息.有时,选择使简单的文本更具吸引力.而在其他情况下,它用于演示流程图.流程.不同实体之间的关系等.下面将介绍如何使用 C# 以编程方式在 Powe ...

  4. 文档处理教程:使用C#在PowerPoint演示文稿中创建图表

    图表用于汇总和直观表示PowerPoint演示文稿中的数据.因此,PowerPoint提供了多种图表类型以可视化数据.其中,最常用的图表类型包括饼图,折线图,条形图,直方图,股票图等.在本文中,将学习 ...

  5. 在Authorware中插入PowerPoint演示文稿的实现方法

    2019独角兽企业重金招聘Python工程师标准>>> 日常生活中,我们在制作多媒体课件的时候一般都会应用到PowerPoint以及Authorware这两款软件.在PowerPoi ...

  6. (二.Windows7操作系统基本概念 三.字处理软件Word 2010 四.电子表格系统Excel 2010 五. 演示文稿文件PowerPoint 2010)

    ※▲计算机文化基础知识 二.Windows7操作系统基本概念 1.操作系统概述 2.Windows7基础 3.Windows7 的文件夹和文件夹管理 4.Windows7控制面板 5.Windows7 ...

  7. ppt中的流程图怎么整体移动_小金在PowerPoint演示文稿中绘制了一个包含多个图形的流程图,他希望该流程图中的所有图形可以作为一个整体移动,最优的操作方法是______...

    小金在PowerPoint演示文稿中绘制了一个包含多个图形的流程图,他希望该流程图中的所有图形可以作为一个整体移动,最优的操作方法是______ 答:选择流程图中的所有图形,通过\"绘图工具 ...

  8. Aspose.Slides使用教程:使用 C++ 在 PowerPoint 演示文稿中嵌入视频

    目录 用于在 PowerPoint 演示文稿中嵌入视频的 C++ API 使用 C++ 在 PowerPoint 演示文稿中嵌入视频 在 PowerPoint 演示文稿中嵌入来自 Web 源的视频 使 ...

  9. 如何在PowerPoint演示文稿中使用iTunes音乐

    One of PowerPoint's charms is its ability to play music during the presentation. Adding music to you ...

最新文章

  1. 有趣但是没有用的linux命令
  2. 使用SAP Cloud Platform fullstack WebIDE创建SAP UI5应用并部署
  3. restful 风格 web api规范
  4. do case php,PHP 规范之编程规范
  5. 迭代器模式(Iterator)解析例子
  6. linux 读取 ntfs硬盘,嵌入式linux下ntfs格式的硬盘读写方法
  7. AI大牛Jerry Kaplan:AGI?没有技术和工程基础
  8. 获取ip地址 域名获取与解析
  9. 相机成像原理及坐标变换
  10. docker和k8s的常见命令
  11. Linux内核原语(九)——互斥体(mutex)
  12. VScode插件管理(C/C++)
  13. jit和jitx区别_JIT,JIS间的区别是什么?
  14. 数据仓库、数据湖、湖仓一体概念
  15. 程序员面试常见问题有哪些?如何回答才能拿到offer?
  16. V3S-Zero TF卡无法引导Linux启动问题
  17. java中short类型变量
  18. 使用charles修改服务器返回数据,Charles使用part4——修改网络请求
  19. mongoshake2.2 Oplog Tailer initialize failed
  20. 复旦计院2020机试真题

热门文章

  1. [KM算法]hdoj 3722:Card Game
  2. ARFoundation目录指引
  3. STM32单片机汽车儿童安全系统高温一氧化碳报警WIFI-APP报警
  4. php 数组字符串替换字符串,利用PHP怎么替换数组的字符串
  5. python+moviepy音视频处理(一):基本操作
  6. Springboot打jar包的几种方式
  7. 未来战场主宰者 人工智能改变战争【楚才国科】
  8. [java面试]宇信易诚 广州分公司 java笔试题目回顾录
  9. 云南省高校计算机等级考试c类难吗,A、B、C类的难度差距真的很大吗?
  10. 必做作业三:原型化系统-倒数日