How to Populate PowerPoint with values in C#
By FoxLearn 2/17/2025 1:48:25 AM 2.64K
How to populate powerpoint with values in C#
Prepare a PowerPoint presentation template (.pptx) with placeholders for dynamic content. These placeholders can be text boxes, tables, shapes, etc., that you'll replace with your data programmatically.
Drag and drop the Label, TextBox, Button controls from your Visual Studio toolbox on to your form designer, then create a simple form allows you to enter data, then populate data to the powerpoint template file.
You need to download PptxTemplater from github website, then copy the PptxTemplater folder you downloaded into your project.
Alternatively, you can install the PptxTemplater library via NuGet Package Manager in your C# project.
Adding a click event handler to the FillData button allows you to fill data from textbox to powerpoint file.
private void btnFillData_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "PowerPoint |*.pptx" }) { if (ofd.ShowDialog() == DialogResult.OK) { // Load the PowerPoint template FileInfo fileInfo = new FileInfo(ofd.FileName); string fileName = $"{fileInfo.Directory}\\{fileInfo.Name.Replace(fileInfo.Extension, "")}_data{fileInfo.Extension}"; File.Copy(ofd.FileName, fileName); Pptx pptx = new Pptx(fileName, FileAccess.ReadWrite); int totalSlide = pptx.SlidesCount(); if (totalSlide > 0) { // Replace placeholders with actual data PptxSlide slide = pptx.GetSlide(0);//default slide 0 slide.ReplaceTag("{{fullname}}", txtFullName.Text, PptxSlide.ReplacementType.Global); slide.ReplaceTag("{{email}}", txtEmail.Text, PptxSlide.ReplacementType.Global); slide.ReplaceTag("{{address}}", txtAddress.Text, PptxSlide.ReplacementType.Global); pptx.Close(); Process.Start(fileName); } } } }
We will load the PowerPoint template file into your C# application using the PptxTemplater library, then replace the placeholders in the loaded template with your actual data. Finally, Save the PowerPoint with the populated data to a new file.
C# Powerpoint Create pptx
To create a PowerPoint file (.pptx) in C#, you can use the DocumentFormat.OpenXml
library, which allows you to work with Open XML formats, including PowerPoint presentations.
You need to install the DocumentFormat.OpenXml
package via NuGet.
In your Visual Studio project, open the Package Manager Console and type: Install-Package DocumentFormat.OpenXml
Below is a simple example of creating a PowerPoint presentation with a title slide using the Open XML.
For example, c# powerpoint create pptx
using System; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Presentation; using DocumentFormat.OpenXml.Drawing; using DocumentFormat.OpenXml; namespace PowerPointExample { class Program { static void Main(string[] args) { string filePath = "presentation.pptx"; // Create a PowerPoint presentation file using (PresentationDocument presentationDocument = PresentationDocument.Create(filePath, PresentationDocumentType.Presentation)) { // Add a presentation part to the document PresentationPart presentationPart = presentationDocument.AddPresentationPart(); presentationPart.Presentation = new Presentation(); // Add slide master and layout parts SlideMasterPart slideMasterPart = presentationPart.AddNewPart<SlideMasterPart>("rId1"); slideMasterPart.SlideMaster = new SlideMaster(); // Create a slide SlidePart slidePart = presentationPart.AddNewPart<SlidePart>("rId2"); slidePart.Slide = new Slide(new CommonSlideData(new ShapeTree())); // Add a title to the slide var slideText = new TextBody(); var paragraph = new Paragraph(); var textRun = new Run(new Text("Hello, PowerPoint!")); paragraph.Append(textRun); slideText.Append(paragraph); slidePart.Slide.CommonSlideData.ShapeTree.Append(new Shape() { TextBody = slideText }); // Save changes and close presentationPart.Presentation.Save(); } Console.WriteLine("PowerPoint file created successfully at " + filePath); } } }
You can also add more slides, change styles, colors, fonts, and other content by modifying the slide parts.