Create PowerPoint files in Blazor

With GemBox.Presentation you can build Blazor applications that read, write, edit, process, and convert PowerPoint presentations. The following live demos show how you can create Blazor apps that generate PowerPoint files and download them to your browser.

Create PowerPoint files in Blazor Server App

The following example shows how you can create a Blazor Server application that:

  1. Creates a PowerPoint card for a birthday, greeting, or anniversary by importing the web form data into the Placeholders from template presentation.
  2. Saves the card in a specified file format like PPTX or PDF.
  3. Downloads the generated file with a downloadFileFromStream JS function (see ASP.NET Core Blazor file downloads).
Generating a PPTX file from Blazor Server app
Screenshot of submitted web form and created PowerPoint presentation
@page "/"
@inject IJSRuntime JS
@using BlazorServerApp.Data
@using System.IO
@using GemBox.Presentation

<h1>Card generator [Blazor Server App]</h1>

<EditForm Model="model" OnSubmit="CreatePresentation">
    <div class="form-group">Top text: <InputTextArea @bind-Value="model.Top" class="form-control"></InputTextArea></div>
    <div class="form-group">Middle text: <InputTextArea @bind-Value="model.Middle" class="form-control"></InputTextArea></div>
    <div class="form-group">Bottom text: <InputTextArea @bind-Value="model.Bottom" class="form-control"></InputTextArea></div>
    <div class="col-1" style="min-width:75px">Format:
        <InputSelect @bind-Value="model.Format" class="form-control">
            @foreach (string format in model.FormatMappingDictionary.Select(item => item.Key))
            {
                <option value="@format">@format</option>
            }
        </InputSelect>
    </div>
    <button class="btn btn-primary mt-2" type="submit">Create</button>
</EditForm>

@code {
    private CardModel model = new();

    private async Task CreatePresentation()
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load template presentation.
        var presentation = PresentationDocument.Load("CardWithPlaceholderElements.pptx");

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

        // Get placeholder elements.
        var placeholders = slide.Content.Drawings
            .OfType<Shape>()
            .Where(s => s.Placeholder != null && s.Placeholder.PlaceholderType == PlaceholderType.Text);

        // Set text on placeholders.
        var top = placeholders.First(p => p.Name == "Top Placeholder");
        top.TextContent.LoadText(this.model.Top);
        var middle = placeholders.First(p => p.Name == "Middle Placeholder");
        middle.TextContent.LoadText(this.model.Middle);
        var bottom = placeholders.First(p => p.Name == "Bottom Placeholder");
        bottom.TextContent.LoadText(this.model.Bottom);

        // Save presentation in specified file format.
        var stream = new MemoryStream();
        presentation.Save(stream, this.model.Options);

        // Download file.
        using var streamRef = new DotNetStreamReference(stream);
        await JS.InvokeVoidAsync("downloadFileFromStream", $"BlazorServerOutput.{this.model.Format.ToLower()}", streamRef);
    }
}
using System.Collections.Generic;
using GemBox.Presentation;

namespace BlazorServerApp.Data
{
    public class CardModel
    {
        public string Top { get; set; } = "Happy Birthday Jane!";
        public string Middle { get; set; } = "May this be your best birthday ever.\nMay your joy never end.";
        public string Bottom { get; set; } = "from John 😊";
        public string Format { get; set; } = "PPTX";
        public SaveOptions Options => this.FormatMappingDictionary[this.Format];
        public IDictionary<string, SaveOptions> FormatMappingDictionary => new Dictionary<string, SaveOptions>()
        {
            ["PPTX"] = new PptxSaveOptions(),
            ["PDF"] = new PdfSaveOptions(),
            ["XPS"] = new XpsSaveOptions(),
            ["PNG"] = new ImageSaveOptions(ImageSaveFormat.Png),
            ["JPG"] = new ImageSaveOptions(ImageSaveFormat.Jpeg),
            ["BMP"] = new ImageSaveOptions(ImageSaveFormat.Bmp),
            ["GIF"] = new ImageSaveOptions(ImageSaveFormat.Gif),
            ["TIF"] = new ImageSaveOptions(ImageSaveFormat.Tiff)
        };
    }
}

Note that saving to XPS and image formats (like PNG and JPG) currently works only on Blazor Server applications that are hosted on Windows Server. To export an XPS or image file, you need to use a Windows-specific TFM in the project file. Besides the .NET Core Runtime, you also need a .NET Windows Desktop Runtime installed on the server.

Create PowerPoint files in Blazor WebAssembly App

The following example shows how you can create a Blazor WebAssembly application that:

  1. Creates a PowerPoint card by importing the web form data into a template presentation using Find and Replace operations.
  2. Saves the card in a specified file format like PPTX or PDF.
  3. Downloads the generated file with a downloadFileFromStream JS function (see ASP.NET Core Blazor file downloads).
Generating a PDF card from Blazor WebAssembly app
Screenshot of submitted web form and created PDF card
@page "/"
@inject IJSRuntime JS
@using BlazorWebAssemblyApp.Data
@using System.IO
@using System.Reflection
@using GemBox.Presentation

<h1>Card generator [Blazor WebAssembly App]</h1>

<EditForm Model="model" OnSubmit="CreatePresentation">
    <div class="form-group">Top text: <InputTextArea @bind-Value="model.Top" class="form-control"></InputTextArea></div>
    <div class="form-group">Middle text: <InputTextArea @bind-Value="model.Middle" class="form-control"></InputTextArea></div>
    <div class="form-group">Bottom text: <InputTextArea @bind-Value="model.Bottom" class="form-control"></InputTextArea></div>
    <div class="col-1" style="min-width:75px">Format:
        <InputSelect @bind-Value="model.Format" class="form-control">
            @foreach (string format in model.FormatMappingDictionary.Select(item => item.Key))
            {
                <option value="@format">@format</option>
            }
        </InputSelect>
    </div>
    <button class="btn btn-primary mt-2" type="submit">Create</button>
</EditForm>

@code {
    private CardModel model = new();

    private async Task CreatePresentation()
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Add embedded resource fonts, required for saving to PDF.
        FontSettings.FontsBaseResourceLocation = "/Fonts/";

        // Load template presentation.
        using var inputStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BlazorWebAssemblyApp.CardWithPlaceholderTexts.pptx")!;
        var presentation = PresentationDocument.Load(inputStream, LoadOptions.Pptx);

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

        // Execute find and replace operations.
        slide.TextContent.Replace("{{Top Text}}", this.model.Top);
        slide.TextContent.Replace("{{Middle Text}}", this.model.Middle);
        slide.TextContent.Replace("{{Bottom Text}}", this.model.Bottom);

        // Save presentation in specified file format.
        using var outputStream = new MemoryStream();
        presentation.Save(outputStream, this.model.Options);

        // Download file.
        using var streamRef = new DotNetStreamReference(outputStream);
        await JS.InvokeVoidAsync("downloadFileFromStream", $"BlazorWebAssemblyOutput.{this.model.Format.ToLower()}", streamRef);
    }
}
using System.Collections.Generic;
using GemBox.Presentation;

namespace BlazorWebAssemblyApp.Data
{
    public class CardModel
    {
        public string Top { get; set; } = "Happy Birthday Jane!";
        public string Middle { get; set; } = "May this be your best birthday ever.\nMay your joy never end.";
        public string Bottom { get; set; } = "from John 😊";
        public string Format { get; set; } = "PDF";
        public SaveOptions Options => this.FormatMappingDictionary[this.Format];
        public IDictionary<string, SaveOptions> FormatMappingDictionary => new Dictionary<string, SaveOptions>()
        {
            ["PPTX"] = new PptxSaveOptions(),
            ["PDF"] = new PdfSaveOptions()
        };
    }
}

Besides installing GemBox.Presentation, for Blazor WebAssembly you'll also need to include its native dependencies. If you are going to create PDF files, the font files need to be present, so you'll need to embed the required font files inside the application itself and specify the FontSettings.FontsBaseResourceLocation property.

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <WasmBuildNative>true</WasmBuildNative>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Presentation" Version="*" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.3" PrivateAssets="all" />
  </ItemGroup>

  <!-- Add HarfBuzzSharp and SkiaSharp native assets. -->
  <ItemGroup>
    <PackageReference Include="HarfBuzzSharp.NativeAssets.WebAssembly" Version="2.8.2.3" />
    <NativeFileReference Include="$(HarfBuzzSharpStaticLibraryPath)\3.1.12\*.a" />
    <PackageReference Include="SkiaSharp.NativeAssets.WebAssembly" Version="2.88.3" />
    <NativeFileReference Include="$(SkiaSharpStaticLibraryPath)\3.1.12\*.a" />
  </ItemGroup>

  <!-- Add Calibri, Georgia, and Segoe UI Emoji embedded fonts. -->
  <ItemGroup>
    <EmbeddedResource Include="Fonts\calibri.ttf" />
    <EmbeddedResource Include="Fonts\georgia.ttf" />
    <EmbeddedResource Include="Fonts\seguiemj.ttf" />
  </ItemGroup>

</Project>

Host and deploy Blazor

GemBox.Presentation is licensed per individual developer, and the licenses include a royalty-free deployment. You can feel free to build an unlimited number of applications and deploy or distribute them to an unlimited number of services, servers, or end-user machines with no extra cost.

GemBox.Presentation licenses are compatible with SaaS or PaaS solutions, as long as they don't offer similar or competing functionality to our component, or expose our features through an API for use by an unlicensed third party. For more information, please check the EULA.

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