Load and Save PPTX in C# and VB.NET

With GemBox.Presentation, you can easily create a new presentation from an existing template presentation 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 demonstrates how to generate a financial report from a template presentation by inserting data into the presentation content.

Screenshot

Template Use Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.IO;
3using System.Linq;
4using System.Text;
5using GemBox.Presentation;
6using GemBox.Presentation.Tables;
7
8class Sample
9{
10    [STAThread]
11    static void Main(string[] args)
12    {
13        // If using Professional version, put your serial key below.
14        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
15
16        PresentationDocument presentation = PresentationDocument.Load("Template.pptx");
17
18        // Retrieve first slide.
19        Slide slide = presentation.Slides[0];
20
21        // Retrieve "Title" placeholder and set shape text.
22        Shape shape = slide.Content.Drawings.OfType<Shape>().Where(item => item.Placeholder?.PlaceholderType == PlaceholderType.CenteredTitle).First();
23        shape.Text.Paragraphs[0].AddRun("ACME Corp - 4th Quarter Financial Results");
24
25        // Retrieve second slide.
26        slide = presentation.Slides[1];
27
28        // Retrieve "Title" placeholder and set shape text.
29        shape = slide.Content.Drawings.OfType<Shape>().Where(item => item.Placeholder?.PlaceholderType == PlaceholderType.Title).First();
30        shape.Text.Paragraphs[0].AddRun("4th Quarter Summary");
31
32        // Retrieve "Content" placeholder.
33        shape = slide.Content.Drawings.OfType<Shape>().Where(item => item.Placeholder?.PlaceholderType == PlaceholderType.Content).First();
34
35        // Set list text.
36        shape.Text.Paragraphs[0].Elements.Clear();
37        shape.Text.Paragraphs[0].AddRun("3 new products/services in Research and Development.");
38
39        shape.Text.Paragraphs[1].Elements.Clear();
40        shape.Text.Paragraphs[1].AddRun("Rollout planned for new division.");
41
42        shape.Text.Paragraphs[2].Elements.Clear();
43        shape.Text.Paragraphs[2].AddRun("Campaigns targeting new markets.");
44
45        // Retrieve third slide.
46        slide = presentation.Slides[2];
47
48        // Retrieve "Title" placeholder and set shape text.
49        shape = slide.Content.Drawings.OfType<Shape>().Where(item => item.Placeholder?.PlaceholderType == PlaceholderType.Title).First();
50        shape.Text.Paragraphs[0].AddRun("4th Quarter Financial Highlights");
51
52        // Retrieve a table.
53        Table table = slide.Content.Drawings.OfType<GraphicFrame>().Where(item => item.Table != null).Select(item => item.Table).First();
54
55        // Fill table data.
56        table.Rows[1].Cells[1].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "$14.2M";
57        table.Rows[1].Cells[2].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "(0.5%)";
58
59        table.Rows[2].Cells[1].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "$1.6M";
60        table.Rows[2].Cells[2].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "0.7%";
61
62        table.Rows[3].Cells[1].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "$12.5M";
63        table.Rows[3].Cells[2].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "0.3%";
64
65        table.Rows[4].Cells[1].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "$2.3M";
66        table.Rows[4].Cells[2].Text.Paragraphs[0].Elements.OfType<TextRun>().First().Text = "(0.2%)";
67
68        presentation.Save("Template Use.pptx");
69    }
70}
1Imports System.IO
2Imports System.Text
3Imports GemBox.Presentation
4Imports GemBox.Presentation.Tables
5
6Module Samples
7
8    Sub Main()
9
10        ' If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12
13        Dim presentation As PresentationDocument = PresentationDocument.Load("Template.pptx")
14
15        ' Retrieve first slide.
16        Dim slide As Slide = presentation.Slides(0)
17
18        ' Retrieve "Title" placeholder And set shape text.
19        Dim shape As Shape = slide.Content.Drawings.OfType(Of Shape).Where(Function(item) item.Placeholder?.PlaceholderType = PlaceholderType.CenteredTitle).First()
20        shape.Text.Paragraphs(0).AddRun("ACME Corp - 4th Quarter Financial Results")
21
22        ' Retrieve second slide.
23        slide = presentation.Slides(1)
24
25        ' Retrieve "Title" placeholder And set shape text.
26        shape = slide.Content.Drawings.OfType(Of Shape).Where(Function(item) item.Placeholder?.PlaceholderType = PlaceholderType.Title).First()
27        shape.Text.Paragraphs(0).AddRun("4th Quarter Summary")
28
29        ' Retrieve "Content" placeholder.
30        shape = slide.Content.Drawings.OfType(Of Shape).Where(Function(item) item.Placeholder?.PlaceholderType = PlaceholderType.Content).First()
31
32        ' Set list text.
33        shape.Text.Paragraphs(0).Elements.Clear()
34        shape.Text.Paragraphs(0).AddRun("3 new products/services in Research and Development.")
35
36        shape.Text.Paragraphs(1).Elements.Clear()
37        shape.Text.Paragraphs(1).AddRun("Rollout planned for new division.")
38
39        shape.Text.Paragraphs(2).Elements.Clear()
40        shape.Text.Paragraphs(2).AddRun("Campaigns targeting new markets.")
41
42        ' Retrieve third slide.
43        slide = presentation.Slides(2)
44
45        ' Retrieve "Title" placeholder And set shape text.
46        shape = slide.Content.Drawings.OfType(Of Shape).Where(Function(item) item.Placeholder?.PlaceholderType = PlaceholderType.Title).First()
47        shape.Text.Paragraphs(0).AddRun("4th Quarter Financial Highlights")
48
49        ' Retrieve a table.
50        Dim table As Table = slide.Content.Drawings.OfType(Of GraphicFrame).Where(Function(item) item.Table IsNot Nothing).Select(Function(item) item.Table).First()
51
52        ' Fill table data.
53        table.Rows(1).Cells(1).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "$14.2M"
54        table.Rows(1).Cells(2).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "(0.5%)"
55
56        table.Rows(2).Cells(1).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "$1.6M"
57        table.Rows(2).Cells(2).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "0.7%"
58
59        table.Rows(3).Cells(1).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "$12.5M"
60        table.Rows(3).Cells(2).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "0.3%"
61
62        table.Rows(4).Cells(1).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "$2.3M"
63        table.Rows(4).Cells(2).Text.Paragraphs(0).Elements.OfType(Of TextRun).First().Text = "(0.2%)"
64
65        presentation.Save("Template Use.pptx")
66
67    End Sub
68
69End Module

Check next sample or find out more about GemBox.Presentation and GemBox Software.