Load and Save PowerPoint files in C# and VB.NET

With GemBox.Presentation, you can easily create a new presentation from an existing template presentation (PPTX) in your C# or VB.NET application.

The procedure is simple:

  • Load a template presentation.
  • Fill it with your data.
  • Save it as a new presentation.

The example below shows how you can generate a financial report from a PowerPoint template presentation by inserting data into the presentation content.

PowerPoint template populated with GemBox.Presentation
Screenshot of PowerPoint template populated with GemBox.Presentation
using System.IO;
using System.Linq;
using GemBox.Presentation;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var presentation = PresentationDocument.Load("%#Template.pptx%");

        // Retrieve first slide.
        var slide = presentation.Slides[0];

        // Retrieve "Title" placeholder and set shape text.
        var shape = slide.Content.Drawings
            .OfType<Shape>()
            .First(item => item.Placeholder?.PlaceholderType == PlaceholderType.Title);
        shape.Text.Paragraphs[0].AddRun("ACME Corp - 4th Quarter Financial Results");

        // Retrieve a picture and replace its image data.
        var picture = slide.Content.Drawings
            .OfType<Picture>()
            .First();
        using (var image = File.OpenRead("%#Acme.png%"))
            picture.Fill.SetData(PictureContentType.Png, image);

        // Retrieve "Content" placeholder.
        shape = slide.Content.Drawings
            .OfType<Shape>()
            .First(item => item.Placeholder?.PlaceholderType == PlaceholderType.Content);

        // Set list text.
        shape.Text.Paragraphs[0].Elements.Clear();
        shape.Text.Paragraphs[0].AddRun("First item, new services.");

        shape.Text.Paragraphs[1].Elements.Clear();
        shape.Text.Paragraphs[1].AddRun("Second item, new division plan.");

        shape.Text.Paragraphs[2].Elements.Clear();
        shape.Text.Paragraphs[2].AddRun("Third item, new marketing campaign.");

        // Retrieve second slide.
        slide = presentation.Slides[1];

        // Retrieve "Title" placeholder and set shape text.
        shape = slide.Content.Drawings
            .OfType<Shape>()
            .First(item => item.Placeholder?.PlaceholderType == PlaceholderType.Title);
        shape.Text.Paragraphs[0].AddRun("4th Quarter Financial Highlights");

        // Retrieve a table.
        var table = slide.Content.Drawings
            .OfType<GraphicFrame>()
            .First(item => item.Table != null)
            .Table;

        // Fill table data.
        table.Rows[1].Cells[1].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "$14.2M";
        table.Rows[1].Cells[2].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "(0.5%)";

        table.Rows[2].Cells[1].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "$1.6M";
        table.Rows[2].Cells[2].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "0.7%";

        table.Rows[3].Cells[1].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "$12.5M";
        table.Rows[3].Cells[2].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "0.3%";

        table.Rows[4].Cells[1].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "$2.3M";
        table.Rows[4].Cells[2].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "(0.2%)";

        presentation.Save("Template Use.%OutputFileType%");
    }
}
Imports System.IO
Imports System.Linq
Imports GemBox.Presentation

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim presentation = PresentationDocument.Load("%#Template.pptx%")

        ' Retrieve first slide.
        Dim slide = presentation.Slides(0)

        ' Retrieve "Title" placeholder and set shape text.
        Dim shape = slide.Content.Drawings _
            .OfType(Of Shape) _
            .First(Function(item) item.Placeholder?.PlaceholderType = PlaceholderType.Title)
        shape.Text.Paragraphs(0).AddRun("ACME Corp - 4th Quarter Financial Results")

        ' Retrieve a picture and replace its image data.
        Dim picture = slide.Content.Drawings _
            .OfType(Of Picture) _
            .First()
        Using image = File.OpenRead("%#Acme.png%")
            picture.Fill.SetData(PictureContentType.Png, image)
        End Using

        ' Retrieve "Content" placeholder.
        shape = slide.Content.Drawings _
            .OfType(Of Shape) _
            .First(Function(item) item.Placeholder?.PlaceholderType = PlaceholderType.Content)

        ' Set list text.
        shape.Text.Paragraphs(0).Elements.Clear()
        shape.Text.Paragraphs(0).AddRun("First item, new services.")

        shape.Text.Paragraphs(1).Elements.Clear()
        shape.Text.Paragraphs(1).AddRun("Second item, new division plan.")

        shape.Text.Paragraphs(2).Elements.Clear()
        shape.Text.Paragraphs(2).AddRun("Third item, new marketing campaign.")

        ' Retrieve second slide.
        slide = presentation.Slides(1)

        ' Retrieve "Title" placeholder And set shape text.
        shape = slide.Content.Drawings _
            .OfType(Of Shape) _
            .First(Function(item) item.Placeholder?.PlaceholderType = PlaceholderType.Title)
        shape.Text.Paragraphs(0).AddRun("4th Quarter Financial Highlights")

        ' Retrieve a table.
        Dim table = slide.Content.Drawings _
            .OfType(Of GraphicFrame) _
            .First(Function(item) item.Table IsNot Nothing) _
            .Table

        ' Fill table data.
        table.Rows(1).Cells(1).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "$14.2M"
        table.Rows(1).Cells(2).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "(0.5%)"

        table.Rows(2).Cells(1).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "$1.6M"
        table.Rows(2).Cells(2).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "0.7%"

        table.Rows(3).Cells(1).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "$12.5M"
        table.Rows(3).Cells(2).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "0.3%"

        table.Rows(4).Cells(1).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "$2.3M"
        table.Rows(4).Cells(2).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "(0.2%)"

        presentation.Save("Template Use.%OutputFileType%")

    End Sub
End Module

See also


Next steps

GemBox.Presentation is a .NET component that enables you to read, write, edit, convert, and print presentation files from your .NET applications using one simple API.

Download Buy