Free C# .NET Library to Generate PowerPoint PPTX from Template
Open Source C# .NET PPTX API allows to Dynamically Generate PowerPoint Presentations (.pptx) from Templates. Replace Text or Images, Add/Remove or Clone Slides in C# Apps.
What is PPTXTemplater?
Tired of manually creating and updating countless PowerPoint presentations? If your application needs to generate dynamic, data-driven .pptx files, doing so programmatically has traditionally been a complex and frustrating task. Manually manipulating XML within the PPTX container is a nightmare, and often, developers resort to clumsy methods like copying slides or using hard-to-control COM objects. What if you could design a beautiful template in PowerPoint itself and then programmatically fill it with data using a simple, lightweight, and purely programmatic .NET library? Enter PPTXTemplater, an ingenious open-source project that changes the game for PowerPoint automation.
PPTXTemplater is a C# library that empowers software developers to programmatically generate and manage PowerPoint (.pptx) files based on predefined templates. It leverages the PPTXTemplater, providing a higher-level abstraction that simplifies the otherwise complex task of manipulating PowerPoint documents. The core idea is to create a template with placeholders, and then use PPTXTemplater to fill in those placeholders with your data, making it an ideal solution for generating reports, certificates, proposals, and more.
Getting Started with PPTXTemplater
The recommend way to install PPTXTemplater is using GitHub. Please use the following command for a smooth installation.
Install PPTXTemplater from GitHub
git clone https://github.com/tkrotoff/PptxTemplater.git
Template-Based PPTX Files Generation via C#
The foundation of PptxTemplater is its template-based approach. You start by creating a standard PowerPoint presentation that will serve as your template. In this template, you can define placeholders for dynamic content using a simple and intuitive syntax, typically {{placeholder_name}}. This allows you to design your slides in PowerPoint itself, giving you full control over the visual aspects of your presentation. Here is a simple example that shows how to create a PowerPoint template with two slides via C# library?
How to Create a PowerPoint Template with two Slides via C# Library?
const string srcFileName = "template.pptx";
const string dstFileName = "final.pptx";
File.Delete(dstFileName);
File.Copy(srcFileName, dstFileName);
Pptx pptx = new Pptx(dstFileName, FileAccess.ReadWrite);
int nbSlides = pptx.SlidesCount();
Assert.AreEqual(2, nbSlides);
// First slide
{
PptxSlide slide = pptx.GetSlide(0);
slide.ReplaceTag("{{hello}}", "HELLO HOW ARE YOU?", PptxSlide.ReplacementType.Global);
slide.ReplaceTag("{{bonjour}}", "BONJOUR TOUT LE MONDE", PptxSlide.ReplacementType.Global);
slide.ReplaceTag("{{hola}}", "HOLA MAMA QUE TAL?", PptxSlide.ReplacementType.Global);
}
// Second slide
{
PptxSlide slide = pptx.GetSlide(1);
slide.ReplaceTag("{{hello}}", "H", PptxSlide.ReplacementType.Global);
slide.ReplaceTag("{{bonjour}}", "B", PptxSlide.ReplacementType.Global);
slide.ReplaceTag("{{hola}}", "H", PptxSlide.ReplacementType.Global);
}
Text Replacement in PPTX Presentation via C#
One of the most fundamental features of PptxTemplater is its ability to replace text placeholders with your data. This is incredibly useful for personalizing presentations with names, dates, numbers, and other textual information. Here is a simple example that shows, how software developers can replace text inside a PPTX presentation inside C# .NET applications.
How to Replace Text inside PPTX Presentation using C# Library?
using PptxTemplater;
// Load the template
using (var presentation = new Presentation("template.pptx"))
{
// Define the data
var data = new Dictionary
{
{ "name", "John Doe" },
{ "date", DateTime.Now.ToShortDateString() }
};
// Fill the template
presentation.Fill(data);
// Save the new presentation
presentation.SaveAs("output.pptx");
}
Replace Images in PPTX Presentation via C#
The open source PptxTemplater library is not limited to just text. Software developers can also replace images inside their templates with just a couple of lines of code. This is particularly useful for generating product catalogs, employee badges, or any other presentation that requires dynamic images. Here is a simple example that show to replace images inside PPTX presentations. To replace an image, you first need to add a placeholder image in your template and give it a specific name (e.g., "photo1").
How to Replace Text inside PPTX Presentation using C# Library?
// Assuming you have a slide with an image named "photo1"
var slide = presentation.Slides[0];
slide.ReplacePicture("photo1", "new_image.png");
Add, Remove or Clone Slide via C#
The open source PptxTemplater library gives you granular control over the slides in your presentation. You can clone existing slides to create dynamic sections or remove slides that are not needed based on your data. Moreover, it is also possible to add new slides and remove unwanted one. The following example shows, how to clone or remove a slide inside C# applications.
How to Clone or Remove a Slide inside PPTX Presentation via C# Library?
/// Clone a slide
var newSlide = presentation.CloneSlide(0);
// Remove a slide
presentation.RemoveSlide(1);