Load and Save PPTX 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 following example shows how you can generate a financial report from a template presentation by inserting data into the presentation content.

PowerPoint template populated with GemBox.Presentation
Screenshot of PowerPoint template populated with GemBox.Presentation
Upload your file (Drag file here)
using System.IO;
using System.Linq;
using GemBox.Presentation;

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

        var presentation = PresentationDocument.Load("%InputFileName%");

        // 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 Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim presentation = PresentationDocument.Load("%InputFileName%")

        ' 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

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Presentation yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.