創建 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);

	// 添加線條類型的自選圖形
	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";

	// 實例化表示演示文稿文件的 Presentation 類
	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);

// 為段落創建部分對象
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++支持散點圖、樹狀圖、直方圖和etc等多種類型的圖表。為了演示,讓我們創建一個普通圖表。以下是執行此操作的步驟。

  • 創建 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);

Adding, Formatting, and Manipulating Charts 了解更多關於操作圖表的信息。

在 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 的更多信息。

也可以看看