创建 PowerPoint 演示文稿 C++

MS PowerPoint 是一个功能丰富的应用程序,可让您创建具有文本、图形、动画、图表和其他几个元素的吸引人的演示文稿。在这篇文章中,您将学习如何在 C++ 应用程序中实现基本的 PowerPoint 自动化功能。特别是,您将了解如何使用 C++ 创建 PowerPoint 演示文稿并在幻灯片中添加文本、图像、图表和表格。

C++ PowerPoint API

Aspose.Slides for C++ 旨在自动化 C++ 应用程序中的 PowerPoint 演示文稿操作功能。使用 API,您可以无缝地创建、编辑或转换 PowerPoint 演示文稿。您可以 下载 API 文件的完整包或从 NuGet 获取。

用 C++ 创建 PowerPoint 演示文稿

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

以下代码示例演示如何使用 C++ 创建 PowerPoint 演示文稿。

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C

	// 文档目录的路径。
	const String outPath = u"../out/SampleChartresult.pptx";

	//实例化表示 PPTX 文件的 Presentation 类
	SharedPtr<Presentation> pres = MakeObject<Presentation>();
	
	SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

	// 添加类型 line 的自选图形
	slide->get_Shapes()->AddAutoShape(Aspose::Slides::ShapeType::Line, 50.0, 150.0, 300.0, 0.0);
	
	//保存演示文稿
	pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

在 C++ 中将幻灯片添加到演示文稿

创建 PowerPoint 演示文稿后,您可以将幻灯片插入其中。以下是将幻灯片添加到 PowerPoint 演示文稿的步骤:

以下代码示例演示如何使用 C++ 将幻灯片添加到 PowerPoint 演示文稿。

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C

	// 文档目录的路径。
	const String outPath = u"../templates/AddSlides.pptx";

	// 实例化表示演示文件的演示类
	SharedPtr<Presentation> pres = MakeObject<Presentation>();

	// 实例化 SlideCollection 类
	SharedPtr<ISlideCollection> slds = pres->get_Slides();

	for (int i = 0; i < pres->get_LayoutSlides()->get_Count(); i++)
	{
		// 将一张空幻灯片添加到幻灯片集合
		slds->AddEmptySlide(pres->get_LayoutSlides()->idx_get(i));

	}

	// 将 PPTX 文件保存到磁盘
	pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

要了解有关操作幻灯片的更多信息,请访问 添加、格式化和操作幻灯片

使用 C++ 将文本添加到 PowerPoint 幻灯片

现在,让我们在 PowerPoint 演示文稿的幻灯片中添加文本。为此,您需要在幻灯片中插入一个文本框。以下是执行此操作的步骤。

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

// 文档目录的路径。
const String outPath = u"../out/TextBoxOnSlideProgram_out.pptx";
const String templatePath = u"../templates/DefaultFonts_out.pptx";

// 加载所需的演示文稿
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 访问第一张幻灯片
SharedPtr<ISlide> sld = pres->get_Slides()->idx_get(0);

// 添加矩形类型的自选图形
SharedPtr<IAutoShape>  ashp = sld->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 150, 75, 150, 50);

// 将 TextFrame 添加到矩形
ashp->AddTextFrame(u" ");

// 访问文本框
SharedPtr<ITextFrame>  txtFrame = ashp->get_TextFrame();

// 为文本框创建段落对象
SharedPtr<IParagraph> paragraph = txtFrame->get_Paragraphs()->idx_get(0);

// 为段落创建 Portion 对象
SharedPtr<IPortion> portion = paragraph->get_Portions()->idx_get(0);
portion->set_Text(u"Aspose TextBox");
	
// 将 PPTX 保存到磁盘
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

要了解有关在演示文稿中处理文本的更多信息,请访问 添加、格式化和处理文本

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

以下是使用 Aspose.Slides for C++ 在 PowerPoint 演示文稿的幻灯片中添加表格的步骤。

  • 创建一个 Presentation 类的实例。
  • 获取幻灯片到 ISlide 对象的引用。
  • 通过提供宽度来定义列和行的数组。
  • 使用 ISlide->getShapes()->AddTable() 方法向幻灯片添加表格。
  • 遍历每个单元格以将格式应用于顶部、底部、右侧和左侧边框。
  • 如果需要,合并单元格。
  • 访问单元格的文本框架以添加一些文本。
  • 将演示文稿另存为 PPTX 文件。

以下代码示例演示如何使用 C++ 在 PowerPoint 演示文稿中创建表格。

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// 文档目录的路径。
const String outPath = u"../out/TableFromScratch_out.pptx";

// 加载所需的演示文稿
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 访问第一张幻灯片
SharedPtr<ISlide> islide = pres->get_Slides()->idx_get(0);

// 定义具有宽度的列和具有高度的行
System::ArrayPtr<double> dblCols = System::MakeObject<System::Array<double>>(4, 70);
System::ArrayPtr<double> dblRows = System::MakeObject<System::Array<double>>(4, 70);

// 将表格形状添加到幻灯片
SharedPtr<ITable> table = islide->get_Shapes()->AddTable(100, 50, dblCols, dblRows);


// 为每个单元格设置边框格式
for (int x = 0; x < table->get_Rows()->get_Count(); x++)
{
	SharedPtr<IRow> row = table->get_Rows()->idx_get(x);
	for (int y = 0; y < row->get_Count(); y++)
	{
		SharedPtr<ICell> cell = row->idx_get(y);

		cell->get_BorderTop()->get_FillFormat()->set_FillType(FillType::Solid);
		cell->get_BorderTop()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
		cell->get_BorderTop()->set_Width(5);

		cell->get_BorderBottom()->get_FillFormat()->set_FillType(FillType::Solid);
		cell->get_BorderBottom()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
		cell->get_BorderBottom()->set_Width(5);

		cell->get_BorderLeft()->get_FillFormat()->set_FillType(FillType::Solid);
		cell->get_BorderLeft()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
		cell->get_BorderLeft()->set_Width(5);

		cell->get_BorderRight()->get_FillFormat()->set_FillType(FillType::Solid);
		cell->get_BorderRight()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
		cell->get_BorderRight()->set_Width(5);

	}

}

// 合并单元格 (1, 1) x (2, 1)
table->MergeCells(table->idx_get(1, 1), table->idx_get(2, 1), false);

// 合并单元格 (1, 2) x (2, 2)
table->MergeCells(table->idx_get(1, 2), table->idx_get(2, 2), false);


// 将 PPTX 保存到磁盘
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

要了解有关操作表的更多信息,请访问 添加、更新和操作表

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

Aspose.Slides for C++ 支持各种类型的图表,如散点图、树形图、直方图和 。为了演示,让我们创建一个普通图表。以下是执行此操作的步骤。

  • 创建 Presentation 类的实例。
  • 获取要在其上添加图表的幻灯片的参考。
  • 使用 ISlide->getShapes()->AddChart() 方法添加带有默认数据和所需类型的图表。
  • 获取 IChart 对象中图表的引用。
  • 清除图表数据并添加新的系列和类别。
  • 为图表系列添加新的图表数据。
  • 设置图表系列的填充颜色。
  • 添加图表系列标签。
  • 将演示文稿另存为 PPTX 文件。

以下代码示例演示如何使用 C++ 在 PowerPoint 演示文稿中插入图表。

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C

	// 文档目录的路径。
	const String outPath = u"../out/NormalCharts_out.pptx";

	//实例化表示 PPTX 文件的 Presentation 类
	SharedPtr<Presentation> pres = MakeObject<Presentation>();

	//访问第一张幻灯片
	SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

	// 添加带有默认数据的图表
	SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);


	// 设置图表数据表的索引
	int defaultWorksheetIndex = 0;

	// 获取图表数据工作表
	SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();

	// 设置图表标题
	chart->get_ChartTitle()->AddTextFrameForOverriding(u"Sample Title");
	chart->get_ChartTitle()->get_TextFrameForOverriding()->get_TextFrameFormat()->set_CenterText ( NullableBool::True);
	chart->get_ChartTitle()->set_Height(20);
	chart->set_HasTitle( true);

	// 删除默认生成的系列和类别
	chart->get_ChartData()->get_Series()->Clear();
	chart->get_ChartData()->get_Categories()->Clear();
	int s = chart->get_ChartData()->get_Series()->get_Count();
	s = chart->get_ChartData()->get_Categories()->get_Count();


	// 现在,添加一个新系列
	chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());
	chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 2, ObjectExt::Box<System::String>(u"Series 2")), chart->get_Type());

	// 添加分类
	chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box<System::String>(u"Caetegoty 1")));
	chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box<System::String>(u"Caetegoty 2")));
	chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 3, 0, ObjectExt::Box<System::String>(u"Caetegoty 3")));

	
	// 获取第一个图表系列
	SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);

	// 现在填充系列数据
	series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box<double>(20)));
	series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(50)));
	series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(30)));

	// 设置系列的填充颜色
	series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
	series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());


	// 采取第二个图表系列
	 series = chart->get_ChartData()->get_Series()->idx_get(1);

	// 现在填充系列数据
	series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 2, ObjectExt::Box<double>(30)));
	series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box<double>(10)));
	series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box<double>(60)));

	// 设置系列的填充颜色
	series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
	series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Green());


	// 第一个标签将显示类别名称
	SharedPtr<IDataLabel> lbl = series->get_DataPoints()->idx_get(0)->get_Label();
	lbl->get_DataLabelFormat()->set_ShowCategoryName(true);

	lbl = series->get_DataPoints()->idx_get(1)->get_Label();
	lbl->get_DataLabelFormat()->set_ShowSeriesName (true);

	// 显示第三个标签的值
	lbl = series->get_DataPoints()->idx_get(2)->get_Label();
	lbl->get_DataLabelFormat()->set_ShowValue (true);
	lbl->get_DataLabelFormat()->set_ShowSeriesName(true);
	lbl->get_DataLabelFormat()->set_Separator (u"/");

	// 将演示文件写入磁盘
	pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

添加、格式化和操作图表 中了解有关操作图表的更多信息。

在 C++ 中打开 PowerPoint 演示文稿

您还可以打开现有的 PowerPoint 演示文稿以编辑内容。为此,您可以按照以下步骤操作:

以下代码示例演示如何使用 C++ 打开现有的 PowerPoint 演示文稿。

// 文档目录的路径
const String templatePath = u"../templates/AccessSlides.pptx";

// 实例化表示 PPTX 文件的 Presentation 类
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);

// 打印幻灯片计数
printf("Total slides inside presentation are : %d\n", pres->get_Slides()->get_Count());

获得免费许可证

您可以 获得免费的临时许可证 以便在没有评估限制的情况下试用 API。

结论

在本文中,您学习了如何在 C++ 应用程序中创建 PowerPoint 演示文稿。分步指南和代码示例以及 API 参考展示了如何从头开始创建演示文稿并将幻灯片、文本、表格和图表插入其中。您可以使用 文档 探索有关 API 的更多信息。

也可以看看