Create Word files in Blazor
With GemBox.Document you can build Blazor applications that read, write, edit, process, and convert Word documents. The following live examples show how you can create Blazor apps that generate Word files and download them to your browser. The following example shows how you can create a Blazor Server application that: 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. The following example shows how you can create a Blazor WebAssembly application that: Besides installing GemBox.Document, 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 GemBox.Document 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.Document 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.Create Word files in Blazor Server App
downloadFileFromStream
JS function (see ASP.NET Core Blazor file downloads).@page "/"
@inject IJSRuntime JS
@using BlazorServerApp.Data
@using System.IO
@using GemBox.Document
<h1>Invoice generator [Blazor Server App]</h1>
<EditForm Model="model" OnSubmit="CreateDocument">
<p><label>Number: <InputNumber @bind-Value="model.Number" /></label></p>
<p><label>Date: <InputDate @bind-Value="model.Date" /></label></p>
<p><label>Company: <InputText @bind-Value="model.Company" /></label></p>
<p><label>Address: <InputText @bind-Value="model.Address" /></label></p>
<p><label>Name: <InputText @bind-Value="model.Name" /></label></p>
<p><label>Format:
<InputSelect @bind-Value="model.Format">
@foreach (string format in model.FormatMappingDictionary.Select(item => item.Key))
{
<option value="@format">@format</option>
}
</InputSelect>
</label></p>
<button class="btn btn-primary" type="submit">Create</button>
</EditForm>
@code {
private InvoiceModel model = new();
private async Task CreateDocument()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Load template document.
var document = DocumentModel.Load("InvoiceWithFields.docx");
// Execute mail merge process.
document.MailMerge.Execute(this.model);
// Save document in specified file format.
var stream = new MemoryStream();
document.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;
using System.Collections.Generic;
using GemBox.Document;
namespace BlazorServerApp.Data
{
public class InvoiceModel
{
public int Number { get; set; } = 1;
public DateTime Date { get; set; } = DateTime.Today;
public string Company { get; set; } = "ACME Corp.";
public string Address { get; set; } = "240 Old Country Road, Springfield, United States";
public string Name { get; set; } = "Joe Smith";
public string Format { get; set; } = "DOCX";
public SaveOptions Options => this.FormatMappingDictionary[this.Format];
public IDictionary<string, SaveOptions> FormatMappingDictionary => new Dictionary<string, SaveOptions>()
{
["PDF"] = new PdfSaveOptions(),
["DOCX"] = new DocxSaveOptions(),
["ODT"] = new OdtSaveOptions(),
["HTML"] = new HtmlSaveOptions() { EmbedImages = true },
["MHTML"] = new HtmlSaveOptions() { HtmlType = HtmlType.Mhtml },
["RTF"] = new RtfSaveOptions(),
["XML"] = new XmlSaveOptions(),
["TXT"] = new TxtSaveOptions(),
["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)
};
}
}
Create Word files in Blazor WebAssembly App
downloadFileFromStream
JS function (see ASP.NET Core Blazor file downloads).@page "/"
@inject IJSRuntime JS
@using BlazorWebAssemblyApp.Data
@using System.IO
@using System.Reflection
@using GemBox.Document
<h1>Invoice generator [Blazor WebAssembly App]</h1>
<EditForm Model="model" OnSubmit="CreateDocument">
<p><label>Number: <InputNumber @bind-Value="model.Number" /></label></p>
<p><label>Date: <InputDate @bind-Value="model.Date" /></label></p>
<p><label>Company: <InputText @bind-Value="model.Company" /></label></p>
<p><label>Address: <InputText @bind-Value="model.Address" /></label></p>
<p><label>Name: <InputText @bind-Value="model.Name" /></label></p>
<p><label>Format:
<InputSelect @bind-Value="model.Format">
@foreach (string format in model.FormatMappingDictionary.Select(item => item.Key))
{
<option value="@format">@format</option>
}
</InputSelect>
</label></p>
<button class="btn btn-primary" type="submit">Create</button>
</EditForm>
@code {
private InvoiceModel model = new();
private async Task CreateDocument()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Add embedded resource fonts, required for saving to PDF.
FontSettings.FontsBaseResourceLocation = "/Fonts/";
// Load template document.
using var inputStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BlazorWebAssemblyApp.InvoiceWithPlaceholders.docx")!;
var document = DocumentModel.Load(inputStream, LoadOptions.DocxDefault);
// Execute find and replace operations.
document.Content.Replace("{{Number}}", this.model.Number.ToString("0000"));
document.Content.Replace("{{Date}}", this.model.Date.ToString("d MMM yyyy HH:mm"));
document.Content.Replace("{{Company}}", this.model.Company);
document.Content.Replace("{{Address}}", this.model.Address);
document.Content.Replace("{{Name}}", this.model.Name);
// Save document in specified file format.
var outputStream = new MemoryStream();
document.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;
using System.Collections.Generic;
using GemBox.Document;
namespace BlazorWebAssemblyApp.Data
{
public class InvoiceModel
{
public int Number { get; set; } = 1;
public DateTime Date { get; set; } = DateTime.Today;
public string Company { get; set; } = "ACME Corp.";
public string Address { get; set; } = "240 Old Country Road, Springfield, United States";
public string Name { get; set; } = "Joe Smith";
public string Format { get; set; } = "PDF";
public SaveOptions Options => this.FormatMappingDictionary[this.Format];
public IDictionary<string, SaveOptions> FormatMappingDictionary => new Dictionary<string, SaveOptions>()
{
["PDF"] = new PdfSaveOptions(),
["DOCX"] = new DocxSaveOptions(),
["ODT"] = new OdtSaveOptions(),
["HTML"] = new HtmlSaveOptions() { EmbedImages = true },
["MHTML"] = new HtmlSaveOptions() { HtmlType = HtmlType.Mhtml },
["RTF"] = new RtfSaveOptions(),
["XML"] = new XmlSaveOptions(),
["TXT"] = new TxtSaveOptions(),
};
}
}
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.Document" 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 and Calibri Bold embedded fonts. -->
<ItemGroup>
<EmbeddedResource Include="Fonts\calibri.ttf" />
<EmbeddedResource Include="Fonts\calibrib.ttf" />
</ItemGroup>
</Project>
Host and deploy Blazor