Export PowerPoint in ASP.NET

GemBox.Presentation does not use Office Automation, nor any other library - only .NET Framework, which, combined with it superior performance, makes it ideal for web applications, like ASP.NET. GemBox.Presentation supports reading and writing a presentation from / to a stream, so the presentation can easily be uploaded or streamed to a browser.

The following example demonstrates how to import web form data into a template presentation and export the resulting presentation to a browser in ASP.NET application.

To see the application in action, use the following link: GemBox.Presentation Web Demo

Screenshot

Export PowerPoint in ASP.NET Screenshot

See the full code below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MediumTrustSampleCs.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GemBox.Presentation Web Demo</title>
    <style type="text/css">
        * {
            font-family: Arial;
            font-size: 12pt;
            color: rgb(45,46,45);
        }

        .summaries {
            font-size: 18.67pt;
        }

        .heading {
            font-weight: bold;
            font-size: 29.33pt;
            color: rgb(209,90,62);
        }

        .title {
            font-weight: bold;
            font-size: 36pt;
        }

        hr {
            display: block;
            height: 1px;
            border: 0;
            border-top: 1px solid rgb(209,90,62);
            margin: 1em 0;
            padding: 0;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <fieldset>
            <legend>Presentation form:</legend>
            <asp:TextBox ID="titleTextBox" runat="server" CssClass="title" Columns="50">ACME Corp - 4th Quarter Financial Results</asp:TextBox>
            <hr />
            <asp:TextBox ID="summaryHeadingTextBox" runat="server" CssClass="heading" Columns="30">4th Quarter Summary</asp:TextBox>
            <br />
            <br />
            <br />
            <asp:TextBox ID="summaryBulletsTextBox" runat="server" CssClass="summaries" TextMode="MultiLine" Wrap="False" Columns="70" Rows="5">3 new products/services in Research and Development.
Rollout planned for new division.
Campaigns targeting new markets.</asp:TextBox>
            <hr />
            <asp:TextBox ID="highlightsHeadingTextBox" runat="server" CssClass="heading" Columns="30">4th Quarter Financial Highlights</asp:TextBox>
            <br />
            <br />
            <br />
            <asp:GridView ID="highlightsGridView" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" Width="75%" OnRowCancelingEdit="highlightsGridView_RowCancelingEdit" OnRowDeleting="highlightsGridView_RowDeleting" OnRowEditing="highlightsGridView_RowEditing" OnRowUpdating="highlightsGridView_RowUpdating">
                <Columns>
                    <asp:BoundField DataField="Name">
                        <ItemStyle Width="25%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Estimated" HeaderText="Estimated">
                        <ItemStyle Width="25%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Change" HeaderText="% Change">
                        <ItemStyle Width="25%" />
                    </asp:BoundField>
                </Columns>
                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                <HeaderStyle BackColor="#D15A3E" ForeColor="#2D2E2D" Font-Bold="False" />
                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                <RowStyle BackColor="White" ForeColor="#2D2E2D" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                <SortedAscendingCellStyle BackColor="#FEFCEB" />
                <SortedAscendingHeaderStyle BackColor="#AF0101" />
                <SortedDescendingCellStyle BackColor="#F6F0C0" />
                <SortedDescendingHeaderStyle BackColor="#7E0000" />
            </asp:GridView>
            <asp:LinkButton ID="addRowLinkButton" runat="server" OnClick="addRowLinkButton_Click">Add Row</asp:LinkButton>
        </fieldset>
        <br />
        <br />
        <fieldset>
            <legend>Output format:</legend>
            <asp:RadioButtonList ID="outputFormatRadioButton" runat="server">
                <asp:ListItem Value="pptx" Selected="True">PPTX</asp:ListItem>
            </asp:RadioButtonList>
            <br />
            <asp:Button ID="generateButton" runat="server" Text="Generate" OnClick="generateButton_Click" />
        </fieldset>
    </form>
</body>
</html>
1using System;
2using System.Data;
3using System.IO;
4using System.Web.UI;
5using System.Web.UI.WebControls;
6using GemBox.Presentation;
7
8namespace MediumTrustSampleCs
9{
10    public partial class Index : Page
11    {
12        protected void Page_Load(object sender, EventArgs e)
13        {
14            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
15            ComponentInfo.FreeLimitReached += (s1, e1) => e1.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;
16
17            if (!this.Page.IsPostBack)
18            {
19                // Fill grid view with some default data.
20                var dataTable = new DataTable();
21                dataTable.Columns.Add("Name", typeof(string));
22                dataTable.Columns.Add("Estimated", typeof(string));
23                dataTable.Columns.Add("Change", typeof(string));
24
25                dataTable.Rows.Add("Revenues", "$14.2M", "(0.5%)");
26                dataTable.Rows.Add("Cash Expense", "$1.6M", "0.7%");
27                dataTable.Rows.Add("Operating Expense", "$12.5M", "0.3%");
28                dataTable.Rows.Add("Operating Income", "$2.3M", "(0.2%)");
29
30                this.Session["highlightsDataTable"] = dataTable;
31
32                this.SetDataBinding();
33            }
34        }
35
36        private static void UpdatePresentation(PresentationDocument presentation, string title, string summaryHeading, string summaryBullets, string highlightsHeading, DataTable highlightsDataTable)
37        {
38            // Populate the first slide with data.
39            var slide = presentation.Slides[0];
40            var shape = (Shape)slide.Content.Drawings[0];
41            if (!string.IsNullOrEmpty(title))
42                shape.Text.Paragraphs[0].AddRun(title.Replace('\v', ' ').Replace('\r', ' ').Replace('\n', ' '));
43
44            // Populate the second slide with data.
45            slide = presentation.Slides[1];
46            shape = (Shape)slide.Content.Drawings[0];
47            if (!string.IsNullOrEmpty(summaryHeading))
48                shape.Text.Paragraphs[0].AddRun(summaryHeading.Replace('\v', ' ').Replace('\r', ' ').Replace('\n', ' '));
49            shape = (Shape)slide.Content.Drawings[1];
50            shape.Text.Paragraphs.Clear();
51            var summaryBulletLines = summaryBullets.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
52            foreach (var summaryBulletLine in summaryBulletLines)
53                shape.Text.AddParagraph().AddRun(summaryBulletLine.Replace('\v', ' ').Replace('\r', ' ').Replace('\n', ' '));
54
55            // Populate the third slide with data.
56            slide = presentation.Slides[2];
57            shape = (Shape)slide.Content.Drawings[0];
58            if (!string.IsNullOrEmpty(highlightsHeading))
59                shape.Text.Paragraphs[0].AddRun(highlightsHeading.Replace('\v', ' ').Replace('\r', ' ').Replace('\n', ' '));
60            var frame = (GraphicFrame)slide.Content.Drawings[1];
61            var table = frame.Table;
62            for (int i = table.Rows.Count - 1; i > 0; --i)
63                table.Rows.RemoveAt(i);
64            foreach (DataRow highlightDataRow in highlightsDataTable.Rows)
65            {
66                var row = table.Rows.AddNew(table.Rows[0].Height);
67
68                var cell = row.Cells.AddNew();
69                var value = (string)highlightDataRow["Name"];
70                if (!string.IsNullOrEmpty(value))
71                    cell.Text.AddParagraph().AddRun(value.Replace('\v', ' ').Replace('\r', ' ').Replace('\n', ' '));
72
73                cell = row.Cells.AddNew();
74                value = (string)highlightDataRow["Estimated"];
75                if (!string.IsNullOrEmpty(value))
76                    cell.Text.AddParagraph().AddRun(value.Replace('\v', ' ').Replace('\r', ' ').Replace('\n', ' '));
77
78                cell = row.Cells.AddNew();
79                value = (string)highlightDataRow["Change"];
80                if (!string.IsNullOrEmpty(value))
81                    cell.Text.AddParagraph().AddRun(value.Replace('\v', ' ').Replace('\r', ' ').Replace('\n', ' '));
82            }
83        }
84
85        protected void generateButton_Click(object sender, EventArgs e)
86        {
87            string path = Path.Combine(Request.PhysicalApplicationPath, "Template.pptx");
88
89            // Load template presentation.
90            PresentationDocument presentation = PresentationDocument.Load(path);
91
92            // Populate the template presentation with data.
93            UpdatePresentation(presentation, this.titleTextBox.Text, this.summaryHeadingTextBox.Text, this.summaryBulletsTextBox.Text, this.highlightsHeadingTextBox.Text, (DataTable)Session["highlightsDataTable"]);
94
95            // Stream the presentation to the browser.
96            string fileName = "Presentation." + this.outputFormatRadioButton.SelectedValue;
97            presentation.Save(this.Response, fileName);
98        }
99
100        protected void addRowLinkButton_Click(object sender, EventArgs e)
101        {
102            var dataTable = (DataTable)Session["highlightsDataTable"];
103            dataTable.Rows.Add("", "", "");
104            this.SetDataBinding();
105        }
106
107        protected void highlightsGridView_RowEditing(object sender, GridViewEditEventArgs e)
108        {
109            this.highlightsGridView.EditIndex = e.NewEditIndex;
110            this.SetDataBinding();
111        }
112
113        protected void highlightsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
114        {
115            int i;
116            int rowIndex = e.RowIndex;
117            var dataTable = (DataTable)Session["highlightsDataTable"];
118
119            for (i = 0; i < dataTable.Columns.Count; i++)
120            {
121                var editTextBox = this.highlightsGridView.Rows[rowIndex].Cells[i + 1].Controls[0] as System.Web.UI.WebControls.TextBox;
122
123                if (editTextBox != null)
124                    dataTable.Rows[rowIndex][i] = editTextBox.Text;
125            }
126
127            this.highlightsGridView.EditIndex = -1;
128            this.SetDataBinding();
129        }
130
131        protected void highlightsGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
132        {
133            this.highlightsGridView.EditIndex = -1;
134            this.SetDataBinding();
135        }
136
137        protected void highlightsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
138        {
139            var dataTable = (DataTable)Session["highlightsDataTable"];
140
141            dataTable.Rows[e.RowIndex].Delete();
142            this.SetDataBinding();
143        }
144
145        private void SetDataBinding()
146        {
147            var dataTable = (DataTable)Session["highlightsDataTable"];
148
149            DataView dataView = dataTable.DefaultView;
150            this.highlightsGridView.DataSource = dataView;
151            dataView.AllowDelete = true;
152
153            this.highlightsGridView.DataBind();
154        }
155    }
156}
1Imports System.Data
2Imports System.IO
3Imports System.Web.UI
4Imports System.Web.UI.WebControls
5Imports GemBox.Presentation
6
7Namespace MediumTrustSampleVb
8    Partial Public Class Index
9        Inherits Page
10        Protected Sub Page_Load(sender As Object, e As EventArgs)
11            ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12            AddHandler ComponentInfo.FreeLimitReached, Sub(s1, e1) e1.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial
13
14            If Not Me.Page.IsPostBack Then
15                ' Fill grid view with some default data.
16                Dim dataTable = New DataTable()
17                dataTable.Columns.Add("Name", GetType(String))
18                dataTable.Columns.Add("Estimated", GetType(String))
19                dataTable.Columns.Add("Change", GetType(String))
20
21                dataTable.Rows.Add("Revenues", "$14.2M", "(0.5%)")
22                dataTable.Rows.Add("Cash Expense", "$1.6M", "0.7%")
23                dataTable.Rows.Add("Operating Expense", "$12.5M", "0.3%")
24                dataTable.Rows.Add("Operating Income", "$2.3M", "(0.2%)")
25
26                Me.Session("highlightsDataTable") = dataTable
27
28                Me.SetDataBinding()
29            End If
30        End Sub
31
32        Private Shared Sub UpdatePresentation(presentation As PresentationDocument, title As String, summaryHeading As String, summaryBullets As String, highlightsHeading As String, highlightsDataTable As DataTable)
33            ' Populate the first slide with data.
34            Dim slide = presentation.Slides(0)
35            Dim shape = DirectCast(slide.Content.Drawings(0), Shape)
36            If Not String.IsNullOrEmpty(title) Then
37                shape.Text.Paragraphs(0).AddRun(title.Replace(ControlChars.VerticalTab, " "c).Replace(ControlChars.Cr, " "c).Replace(ControlChars.Lf, " "c))
38            End If
39
40            ' Populate the second slide with data.
41            slide = presentation.Slides(1)
42            shape = DirectCast(slide.Content.Drawings(0), Shape)
43            If Not String.IsNullOrEmpty(summaryHeading) Then
44                shape.Text.Paragraphs(0).AddRun(summaryHeading.Replace(ControlChars.VerticalTab, " "c).Replace(ControlChars.Cr, " "c).Replace(ControlChars.Lf, " "c))
45            End If
46            shape = DirectCast(slide.Content.Drawings(1), Shape)
47            shape.Text.Paragraphs.Clear()
48            Dim summaryBulletLines = summaryBullets.Split(New String() {vbCr & vbLf}, StringSplitOptions.RemoveEmptyEntries)
49            For Each summaryBulletLine In summaryBulletLines
50                shape.Text.AddParagraph().AddRun(summaryBulletLine.Replace(ControlChars.VerticalTab, " "c).Replace(ControlChars.Cr, " "c).Replace(ControlChars.Lf, " "c))
51            Next
52
53            ' Populate the third slide with data.
54            slide = presentation.Slides(2)
55            shape = DirectCast(slide.Content.Drawings(0), Shape)
56            If Not String.IsNullOrEmpty(highlightsHeading) Then
57                shape.Text.Paragraphs(0).AddRun(highlightsHeading.Replace(ControlChars.VerticalTab, " "c).Replace(ControlChars.Cr, " "c).Replace(ControlChars.Lf, " "c))
58            End If
59            Dim frame = DirectCast(slide.Content.Drawings(1), GraphicFrame)
60            Dim table = frame.Table
61            For i As Integer = table.Rows.Count - 1 To 1 Step -1
62                table.Rows.RemoveAt(i)
63            Next
64            For Each highlightDataRow As DataRow In highlightsDataTable.Rows
65                Dim row = table.Rows.AddNew(table.Rows(0).Height)
66
67                Dim cell = row.Cells.AddNew()
68                Dim value = DirectCast(highlightDataRow("Name"), String)
69                If Not String.IsNullOrEmpty(value) Then
70                    cell.Text.AddParagraph().AddRun(value.Replace(ControlChars.VerticalTab, " "c).Replace(ControlChars.Cr, " "c).Replace(ControlChars.Lf, " "c))
71                End If
72
73                cell = row.Cells.AddNew()
74                value = DirectCast(highlightDataRow("Estimated"), String)
75                If Not String.IsNullOrEmpty(value) Then
76                    cell.Text.AddParagraph().AddRun(value.Replace(ControlChars.VerticalTab, " "c).Replace(ControlChars.Cr, " "c).Replace(ControlChars.Lf, " "c))
77                End If
78
79                cell = row.Cells.AddNew()
80                value = DirectCast(highlightDataRow("Change"), String)
81                If Not String.IsNullOrEmpty(value) Then
82                    cell.Text.AddParagraph().AddRun(value.Replace(ControlChars.VerticalTab, " "c).Replace(ControlChars.Cr, " "c).Replace(ControlChars.Lf, " "c))
83                End If
84            Next
85        End Sub
86
87        Protected Sub generateButton_Click(sender As Object, e As EventArgs)
88            Dim path_ As String = Path.Combine(Request.PhysicalApplicationPath, "Template.pptx")
89
90            ' Load template presentation.
91            Dim presentation As PresentationDocument = PresentationDocument.Load(path_)
92
93            ' Populate the template presentation with data.
94            UpdatePresentation(presentation, Me.titleTextBox.Text, Me.summaryHeadingTextBox.Text, Me.summaryBulletsTextBox.Text, Me.highlightsHeadingTextBox.Text, DirectCast(Session("highlightsDataTable"), DataTable))
95
96            ' Stream the presentation to the browser.
97            Dim fileName As String = "Presentation." + Me.outputFormatRadioButton.SelectedValue
98            presentation.Save(Me.Response, fileName)
99        End Sub
100
101        Protected Sub addRowLinkButton_Click(sender As Object, e As EventArgs)
102            Dim dataTable = DirectCast(Session("highlightsDataTable"), DataTable)
103            dataTable.Rows.Add("", "", "")
104            Me.SetDataBinding()
105        End Sub
106
107        Protected Sub highlightsGridView_RowEditing(sender As Object, e As GridViewEditEventArgs)
108            Me.highlightsGridView.EditIndex = e.NewEditIndex
109            Me.SetDataBinding()
110        End Sub
111
112        Protected Sub highlightsGridView_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
113            Dim i As Integer
114            Dim rowIndex As Integer = e.RowIndex
115            Dim dataTable = DirectCast(Session("highlightsDataTable"), DataTable)
116
117            For i = 0 To dataTable.Columns.Count - 1
118                Dim editTextBox = TryCast(Me.highlightsGridView.Rows(rowIndex).Cells(i + 1).Controls(0), System.Web.UI.WebControls.TextBox)
119
120                If editTextBox IsNot Nothing Then
121                    dataTable.Rows(rowIndex)(i) = editTextBox.Text
122                End If
123            Next
124
125            Me.highlightsGridView.EditIndex = -1
126            Me.SetDataBinding()
127        End Sub
128
129        Protected Sub highlightsGridView_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
130            Me.highlightsGridView.EditIndex = -1
131            Me.SetDataBinding()
132        End Sub
133
134        Protected Sub highlightsGridView_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
135            Dim dataTable = DirectCast(Session("highlightsDataTable"), DataTable)
136
137            dataTable.Rows(e.RowIndex).Delete()
138            Me.SetDataBinding()
139        End Sub
140
141        Private Sub SetDataBinding()
142            Dim dataTable = DirectCast(Session("highlightsDataTable"), DataTable)
143
144            Dim dataView As DataView = dataTable.DefaultView
145            Me.highlightsGridView.DataSource = dataView
146            dataView.AllowDelete = True
147
148            Me.highlightsGridView.DataBind()
149        End Sub
150        Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
151            target = value
152            Return value
153        End Function
154    End Class
155End Namespace

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