
MS PowerPoint is a feature-rich application that lets you create appealing presentations having text, graphics, animations, charts, and several other elements. In this post, you will learn how to implement the basic PowerPoint automation features within the C++ applications. Particularly, you will come to know how to create PowerPoint presentations and add text, images, charts, and tables to the slides using C++.
- C++ PowerPoint API
- Create a PowerPoint Presentation in C++
- Add Slides to a Presentation
- Add Text to a PowerPoint Slide
- Create a Table in Presentation
- Create a Chart in Presentation
- Open an Existing PowerPoint Presentation
- Get Free License
C++ PowerPoint API
Aspose.Slides for C++ is designed to automate PowerPoint presentation manipulation features within C++ applications. Using the API, you can create, edit, or convert the PowerPoint presentations seamlessly. You can download the complete package of the API files or get it from NuGet.
Create a PowerPoint Presentation in C++
Let’s start by creating an empty PowerPoint presentation using Aspose.Slides for C++. The following are the steps to perform this operation.
- Create an object of the Presentation class.
- Save the presentation using Presentation->Save(String, SaveFormat::Pptx) method.
The following code sample shows how to create a PowerPoint presentation using C++.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C | |
// The path to the documents directory. | |
const String outPath = u"../out/SampleChartresult.pptx"; | |
//Instantiate Presentation class that represents PPTX file | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(); | |
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0); | |
// Add an autoshape of type line | |
slide->get_Shapes()->AddAutoShape(Aspose::Slides::ShapeType::Line, 50.0, 150.0, 300.0, 0.0); | |
//Saving presentation | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
Add Slide to a Presentation in C++
Once you have created the PowerPoint presentation, you can insert slides into it. The following are the steps to add slides to a PowerPoint presentation:
- Create an object of the Presentation class.
- Get slides in the ISlideCollection object using Presentation->get_Slides() method.
- Add slide using ISlideCollection->AddEmptySlide() method.
- Save the presentation using Presentation->Save(String, SaveFormat::Pptx) method.
The following code sample shows how to add slides to a PowerPoint presentation using C++.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C | |
// The path to the documents directory. | |
const String outPath = u"../templates/AddSlides.pptx"; | |
// Instantiate Presentation class that represents the presentation file | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(); | |
// Instantiate SlideCollection calss | |
SharedPtr<ISlideCollection> slds = pres->get_Slides(); | |
for (int i = 0; i < pres->get_LayoutSlides()->get_Count(); i++) | |
{ | |
// Add an empty slide to the Slides collection | |
slds->AddEmptySlide(pres->get_LayoutSlides()->idx_get(i)); | |
} | |
// Save the PPTX file to the Disk | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
To learn more about manipulating slides, visit Adding, Formatting and Manipulating Slides.
Add Text to a PowerPoint Slide using C++
Now, let’s add text to a slide in the PowerPoint presentation. For this, you need to insert a textbox to the slide. Below are the steps to perform this operation.
- Create a new presentation or load an existing one using Presentation class.
- Obtain the reference of the slide into ISlide object using Presentation->get_Slides()->idx_get(int) method.
- Add a rectangle to the slide using ISlide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, int, int, int, int) method and get its reference in IAutoShape object.
- Add text frame using IAutoShape->AddTextFrame(u" “) method.
- Get the text frame using IAutoShape->get_TextFrame() into ITextFrame object.
- Get paragraph using ITextFrame->get_Paragraphs()->idx_get(0) into IParagraph object.
- Access portion of the paragraph using IParagraph->get_Portions()->idx_get(0) into IPortion object.
- Set text using IPortion->set_Text(u"Aspose TextBox”) method.
- Save the presentation using Presentation->Save(String, SaveFormat::Pptx) method.
The following code sample shows how to add text to slides in a PowerPoint presentation using C++.
// The path to the documents directory. | |
const String outPath = u"../out/TextBoxOnSlideProgram_out.pptx"; | |
const String templatePath = u"../templates/DefaultFonts_out.pptx"; | |
// Load the desired the presentation | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(); | |
// Access first slide | |
SharedPtr<ISlide> sld = pres->get_Slides()->idx_get(0); | |
// Add an AutoShape of Rectangle type | |
SharedPtr<IAutoShape> ashp = sld->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 150, 75, 150, 50); | |
// Add TextFrame to the Rectangle | |
ashp->AddTextFrame(u" "); | |
// Accessing the text frame | |
SharedPtr<ITextFrame> txtFrame = ashp->get_TextFrame(); | |
// Create the Paragraph object for text frame | |
SharedPtr<IParagraph> paragraph = txtFrame->get_Paragraphs()->idx_get(0); | |
// Create Portion object for paragraph | |
SharedPtr<IPortion> portion = paragraph->get_Portions()->idx_get(0); | |
portion->set_Text(u"Aspose TextBox"); | |
// Save PPTX to Disk | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
To learn more about manipulating text in presentations, visit Adding, Formatting, and Manipulating Text.
Create a Table in Presentation using C++
The following are the steps to add a table in a PowerPoint presentation’s slide using Aspose.Slides for C++.
- Create an instance of a Presentation class.
- Obtain the reference of a slide into an ISlide object.
- Define arrays of columns and rows by providing the width.
- Add a table to the slide using ISlide->get_Shapes()->AddTable() method.
- Iterate through each cell to apply formatting to the top, bottom, right, and left borders.
- Merge cells if required.
- Access the text frame of a cell to add some text.
- Save the presentation as a PPTX file.
The following code sample shows how to create table in PowerPoint presentation using C++.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C | |
// The path to the documents directory. | |
const String outPath = u"../out/TableFromScratch_out.pptx"; | |
// Load the desired the presentation | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(); | |
// Access first slide | |
SharedPtr<ISlide> islide = pres->get_Slides()->idx_get(0); | |
// Define columns with widths and rows with heights | |
System::ArrayPtr<double> dblCols = System::MakeObject<System::Array<double>>(4, 70); | |
System::ArrayPtr<double> dblRows = System::MakeObject<System::Array<double>>(4, 70); | |
// Add table shape to slide | |
SharedPtr<ITable> table = islide->get_Shapes()->AddTable(100, 50, dblCols, dblRows); | |
// Set border format for each cell | |
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); | |
} | |
} | |
// Merging cells (1, 1) x (2, 1) | |
table->MergeCells(table->idx_get(1, 1), table->idx_get(2, 1), false); | |
// Merging cells (1, 2) x (2, 2) | |
table->MergeCells(table->idx_get(1, 2), table->idx_get(2, 2), false); | |
// Save PPTX to Disk | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
To learn more about manipulating the tables, visit Adding, Updating and Manipulating Tables.
Create a Chart in Presentation using C++
Aspose.Slides for C++ supports various types of charts such as scattered, treemap, histogram, and etc. For the demonstration, let’s create a normal chart. Below are the steps to perform this operation.
- Create an instance of the Presentation class.
- Obtain the reference of a slide you want to add the chart on.
- Add chart with default data along with desired type using ISlide->get_Shapes()->AddChart() method.
- Get the reference of the chart in the IChart object.
- Clear the chart data and add new series and categories.
- Add new chart data for chart series.
- Set fill color for chart series.
- Add chart series labels.
- Save the presentation as a PPTX file.
The following code sample shows how to insert a chart in a PowerPoint presentation using C++.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C | |
// The path to the documents directory. | |
const String outPath = u"../out/NormalCharts_out.pptx"; | |
//Instantiate Presentation class that represents PPTX file | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(); | |
//Access first slide | |
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0); | |
// Add chart with default data | |
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500); | |
// Setting the index of chart data sheet | |
int defaultWorksheetIndex = 0; | |
// Getting the chart data worksheet | |
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook(); | |
// Setting chart Title | |
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); | |
// Delete default generated series and categories | |
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(); | |
// Now, Adding a new series | |
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()); | |
// Add Catrgories | |
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"))); | |
// Take first chart series | |
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0); | |
// Now populating series data | |
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))); | |
// Setting fill color for series | |
series->get_Format()->get_Fill()->set_FillType(FillType::Solid); | |
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red()); | |
// Take second chart series | |
series = chart->get_ChartData()->get_Series()->idx_get(1); | |
// Now populating series data | |
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))); | |
// Setting fill color for series | |
series->get_Format()->get_Fill()->set_FillType(FillType::Solid); | |
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Green()); | |
// First label will be show Category name | |
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); | |
// Show value for third label | |
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"/"); | |
// Write the presentation file to disk | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
Learn more about manipulating charts from Adding, Formatting, and Manipulating Charts.
Open a PowerPoint Presentation in C++
You can also open an existing PowerPoint presentations in order to edit the content. For this, you can follow the below steps:
- Load the PPTX file using Presentation class by providing the file’s name.
- Update the content of the presentation.
- Save the updated file using the Presentation->Save(String, SaveFormat) method.
The following code sample shows how to open an existing PowerPoint presentation using C++.
// The path to the documents directory | |
const String templatePath = u"../templates/AccessSlides.pptx"; | |
// Instantiate Presentation class that represents PPTX file | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath); | |
// Print slide count | |
printf("Total slides inside presentation are : %d\n", pres->get_Slides()->get_Count()); | |
Get a Free License
You can get a free temporary license in order to try the API without evaluation limitations.
Conclusion
In this article, you have learned how to create PowerPoint presentations from within the C++ applications. The step-by-step guide and code samples along with API references have shown how to create a presentation from scratch and insert slides, text, tables, and charts into it. You can explore more about the API using the documentation.