教程:在C#中创建带有表格、图表、图片的PPT演示文稿
MS PowerPoint演示文稿允许您创建包含文本,图像,图表,动画和其他元素的幻灯片放映。各种其他格式设置选项使您的演示文稿更具吸引力。在本文中,您将了解如何以编程方式创建此类演示文稿。您将学习如何使用C#创建具有文本,表格,图像和图表的PPTX演示文稿。
>>你可以下载Aspose.Slides for Java v20.11测试。
- 创建PowerPoint演示文稿
- 打开现有的PowerPoint演示文稿
- 将幻灯片添加到演示文稿
- 将文本添加到演示文稿的幻灯片
- 在演示文稿中创建表
- 在演示文稿中创建图表
- 在演示文稿中添加图像
用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#在演示文稿中创建表
.NET的Aspose.Slides提供了一种在演示文稿文档中创建表的简便方法。以下是其步骤。
- 创建Presentation 类的实例。
- 通过使用其索引获取幻灯片的参考。
- 定义具有宽度的列和具有高度的行的数组。
- 表添加到所述滑动用Slide.Shapes.AddTable()由露出的方法IShapes对象,并获得在参考表中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,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);
在演示文稿中添加图像
以下代码示例显示了如何在演示幻灯片中添加SVG图像。
- 使用Presentation 类创建一个新的演示 文稿。
- 使用File.ReadAllText(String path)方法读取SVG图像。
- 使用Presentation.Slides [0] .Shapes.AddPictureFrame(ShapeType shapeType,float x,float y,float宽度,float高度,IPPImage图像)方法将图像添加到幻灯片。
- 保存演示文稿。
下面的代码示例演示如何在C#中向演示文稿添加图像。
// Create presentation using (var p = new Presentation()) {// Read imagevar svgContent = File.ReadAllText("image.svg");// Add image to image collectionvar emfImage = p.Images.AddFromSvg(svgContent);// Add image to slidep.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 0, 0, emfImage.Width, emfImage.Height, emfImage);// Save presentationp.Save("presentation.pptx", SaveFormat.Pptx); }
如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。
教程:在C#中创建带有表格、图表、图片的PPT演示文稿相关推荐
- mysql 创建带参数的存储过程_在MySQL中创建带有IN和OUT参数的存储过程的方法
在 MySQL 中创建储存过程的语法很难记,除非你经常跟储存过程打交道,原因很简单,语法不是什么小笑话.如果你通过命令行控制 MySQL,你需要记住准确的语法.一个快速示例可以很好的帮助你做到这点.在 ...
- 创建带有表格的PPT
1.程序说明 1.1编程语言:Java 1.2 第三方库:Apache POI Apache POI 官网: http://poi.apache.org/ 下载页面: http://poi.apach ...
- c# 获取word表格中的内容_Java 在Word中创建嵌套表格
嵌套表格,即在一个大的表格单元格中再嵌进去一个或几个小的表格,使表格内容布局合理.本文将通过java程序来演示如何在Word中创建嵌套表格. 使用工具:Free Spire.Doc for Java ...
- 计算机怎么删除表格,电脑中删除Excel2010表格多余图片的三种方法
为了让表格看起来更加直观,很多朋友都会在Excel中插入图片.那么,当我们大批量插入图片时,如果想要删除的话,应该怎么办呢?以下是系统城小编为您带来的电脑中删除Excel2010表格多余图片的三种方法 ...
- 蓝色数据分析关系折线图表格图表合集PPT模板
模板介绍 精美PPT模板设计,蓝色数据分析关系折线图表格图表合集PPT模板.一套可视图表幻灯片模板,内含青色,黑色多种配色,精美风格设计,动态播放效果,精美实用. 一份设计精美的PPT模板,可以让你在 ...
- 《Adobe Acrobat XI经典教程》—第6课转换PPT演示文稿
本节书摘来自异步社区<Adobe Acrobat XI经典教程>一书中的第6课转换PPT演示文稿,作者[美]Adobe公司,更多章节内容可以访问云栖社区"异步社区"公众 ...
- 第11章 在PPT演示文稿中呈现金字塔
现场演示PPT的大部分工作是预测观众的反应,时刻努力吸引他们的注意力,激起他们接受你所传递的信息的热情.换句话说,你必须取悦观众.商业演示和一些娱乐形式一样,也需要艺术性. 下面是设计PPT演示文稿时 ...
- Android带有边框的裁剪算法,如何在Android中创建带有边框的表格?
我对这个问题的解决方案是在每个单元格的背景字段上放置一个xml可绘制资源.通过这种方式,您可以为所有单元格定义想要的边框形状.唯一的不便之处是,极端单元的边界是其他单元的宽度的一半,但是如果您的桌子占 ...
- mysql创建带日期的表_在MySQL中创建带有日期的临时表
要创建带有日期的临时表,请在MySQL中使用CREATE TEMPORARY TABLE.以下是语法- 语法create temporary table yourTableName( yourColu ...
最新文章
- RTX组织架构刷新出现了问题
- coverage 覆盖多个测试文件时_奇技淫巧[2]:cmake中添加lcov代码覆盖测试
- 随堂小测app(nabcd)
- SQL SERVER 子查询的用法
- SAP Spartacus的site context配置参数SiteContextParamsService
- 获取表数据_mysql数据库部分表被truncate,部分数据被delete的解决过程
- 第五节 CImage和CBmp(二)
- 单片机检测220V交流电通断电路
- 车仪表台上的装饰_@云浮车主:车内装饰品摆放不当将被处罚
- 网络学习(一)网络版块主题介绍
- 实验室双显示屏安装使用记录
- 前端的IDE工具对比
- C++技术之一:C++类 _stdcall
- 注塑模具的温度对注塑工艺的影响到底有多大呢
- python 获取股票的交易数据
- 《黑白团团队》第九次团队作业:Beta冲刺第三天
- 人力资源总监面试时是怎么想的?
- Windows 7 64位无法在状态码为0xc0000225的VirtualBox / Linux上安装
- pytest官方文档 6.2 中文翻译版(第十章):警告捕捉
- Asp.net C#制作PDF文件全攻略