翻译|使用教程|编辑:李显亮|2021-07-06 11:15:14.207|阅读 461 次
概述:在这篇文章中,将了解如何以编程方式创建此类演示文稿,还有如何使用 C# 创建包含文本、表格、图像和图表的 PPTX 演示文稿。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
MS PowerPoint 演示文稿允许您创建包含文本、图像、图表、动画和其他元素的幻灯片。各种附加格式选项可让您的演示文稿更具吸引力。在这篇文章中,将了解如何以编程方式创建此类演示文稿,以及将学习如何使用 C# 创建包含文本、表格、图像和图表的 PPTX 演示文稿。
Aspose.Slides for .NET是一个演示操作 API,允许您从 .NET 应用程序中创建和操作 PowerPoint 文档。API 提供了实现基本和高级 PowerPoint 自动化功能所需的几乎所有可能的功能。
>>你可以点击这里下载Aspose.Slides 最新版测试体验。
让我们首先使用 Aspose.Slides for .NET 创建一个空的 PowerPoint 演示文稿。以下是执行此操作的步骤。
以下代码示例展示了如何在 C# 中创建 PowerPoint 演示文稿。
// Instantiate a Presentation object that represents a presentation file
using (Presentation presentation = new Presentation())
{
// Get the first slide
ISlide slide = presentation.Slides[0];
// Add content to slide...
// Save presentation
presentation.Save("NewPresentation.pptx", SaveFormat.Pptx);
}
无需付出额外的努力即可打开现有的 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());
创建演示文稿后,您可以开始向其中添加幻灯片。以下是使用 Aspose.Slides for .NET 在演示文稿中添加幻灯片的步骤。
以下代码示例展示了如何使用 C# 在 PowerPoint 演示文稿中添加幻灯片。
// Instantiate Presentation class that represents the presentation file
using (Presentation pres = new Presentation())
{
// Instantiate SlideCollection calss
ISlideCollection 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); }
现在我们可以向 PowerPoint 演示文稿中的幻灯片添加内容。让我们首先使用以下步骤向幻灯片添加一段文本。
以下代码示例展示了如何使用 C# 向演示文稿中的幻灯片添加文本。
// Instantiate PresentationEx// Instantiate PresentationEx
using (Presentation pres = new Presentation())
{
// Get the first slide
ISlide sld = pres.Slides[0];
// Add an AutoShape of Rectangle type
IAutoShape ashp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);
// Add TextFrame to the Rectangle
ashp.AddTextFrame(" ");
// Accessing the text frame
ITextFrame txtFrame = ashp.TextFrame;
// Create the Paragraph object for text frame
IParagraph para = txtFrame.Paragraphs[0];
// Create Portion object for paragraph
IPortion portion = para.Portions[0];
// Set Text
portion.Text = "Aspose TextBox";
// Save the presentation to disk
pres.Save("presentation.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
Aspose.Slides for .NET 提供了一种在演示文档中创建表格的简单方法。以下是它的步骤。
以下代码示例展示了如何在 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# 在 PowerPoint 演示文稿中添加图表的步骤。
以下代码示例展示了如何使用 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 data
series.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的全部完整功能,可 联系在线客服获取30天临时授权体验。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn