Create Word (DOCX) or PDF file in ASP.NET Core

GemBox.Document provides support for .NET Standard 2.0 which is why it can be used on various .NET platforms such as .NET Framework, .NET Core, Xamarin, and Universal Windows Platform (UWP).

Save to Word or PDF in .NET Core

To create a Word document or PDF file with an application that targets .NET Core 2.0 or above on Linux, macOS, or Windows using GemBox.Document, simply add the following package references in the project file.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Document" Version="*" />
    <PackageReference Include="System.Drawing.Common" Version="*" />
  </ItemGroup>

</Project>

GemBox.Document is ideal for web applications (like ASP.NET Core, ASP.NET MVC, and ASP.NET Web Forms) because of its fast performance and thread safety when working with multiple DocumentModel objects.

The following example shows how you can create a simple ASP.NET Core application that imports data from a web form to a template document using mail merge process, creates a Word document of a specified format and downloads it to the client's browser.

Sending form data from Razor view in ASP.NET Core demo application
Screenshot of submitted web form Razor view in ASP.NET Core
Generated DOCX file from template Word document in ASP.NET Core demo application
Screenshot of created Word file of DOCX format in ASP.NET Core
@model DocumentCore.Controllers.InvoiceModel

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta charset="utf-8" />
    <title>Create a Word document or PDF file</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <style>
        body { padding: 20px; font-size: 0.9rem; }
        .col-form-label { font-weight: bold; }
        input.form-control, select.form-control { font-size: 0.9rem; }
        .form-group > label:after { content: ": "; }
    </style>
</head>
<body>
    <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row">
            <label asp-for="Number" class="col-md-2 col-sm-3 col-form-label"></label>
            <div class="col-md-5 col-sm-7">
                <input asp-for="Number" type="number" class="form-control" />
            </div>
        </div>
        <div class="form-group row">
            <label asp-for="Date" class="col-md-2 col-sm-3 col-form-label"></label>
            <div class="col-md-5 col-sm-7">
                <input asp-for="Date" type="date" class="form-control" />
            </div>
        </div>
        <div class="form-group row">
            <label asp-for="Company" class="col-md-2 col-sm-3 col-form-label"></label>
            <div class="col-md-5 col-sm-7">
                <input asp-for="Company" class="form-control" />
            </div>
        </div>
        <div class="form-group row">
            <label asp-for="Address" class="col-md-2 col-sm-3 col-form-label"></label>
            <div class="col-md-5 col-sm-7">
                <input asp-for="Address" class="form-control" />
            </div>
        </div>
        <div class="form-group row">
            <label asp-for="Country" class="col-md-2 col-sm-3 col-form-label"></label>
            <div class="col-md-5 col-sm-7">
                @Html.DropDownListFor(m => m.Country, Model.Countries, null, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group row">
            <label asp-for="FullName" class="col-md-2 col-sm-3 col-form-label"></label>
            <div class="col-md-5 col-sm-7">
                <input asp-for="FullName" class="form-control" />
            </div>
        </div>
        <hr />
        <div>
            <h4>Output format:</h4>
            <div class="form-check">
                <input id="PDF" name="SelectedFormat" type="radio" class="form-check-input" value="PDF" />
                <label for="PDF" class="form-check-label">PDF</label>
            </div>
            <div class="form-check">
                <input id="DOCX" name="SelectedFormat" type="radio" class="form-check-input" checked value="DOCX" />
                <label for="DOCX" class="form-check-label">DOCX</label>
            </div>
            <div class="form-check">
                <input id="HTML" name="SelectedFormat" type="radio" class="form-check-input" value="HTML" />
                <label for="HTML" class="form-check-label">HTML</label>
            </div>
            <div class="form-check">
                <input id="RTF" name="SelectedFormat" type="radio" class="form-check-input" value="RTF" />
                <label for="RTF" class="form-check-label">RTF</label>
            </div>
            <div class="form-check">
                <input id="XML" name="SelectedFormat" type="radio" class="form-check-input" value="XML" />
                <label for="XML" class="form-check-label">XML</label>
            </div>
            <div class="form-check">
                <input id="TXT" name="SelectedFormat" type="radio" class="form-check-input" value="TXT" />
                <label for="TXT" class="form-check-label">TXT</label>
            </div>
            <fieldset disabled>
                <div class="form-check">
                    <input id="XPS" name="SelectedFormat" type="radio" class="form-check-input" value="XPS" />
                    <label for="XPS" class="form-check-label">XPS</label>
                </div>
                <div class="form-check">
                    <input id="PNG" name="SelectedFormat" type="radio" class="form-check-input" value="PNG" />
                    <label for="PNG" class="form-check-label">PNG</label>
                </div>
                <div class="form-check">
                    <input id="JPG" name="SelectedFormat" type="radio" class="form-check-input" value="JPG" />
                    <label for="JPG" class="form-check-label">JPG</label>
                </div>
                <div class="form-check">
                    <input id="GIF" name="SelectedFormat" type="radio" class="form-check-input" value="GIF" />
                    <label for="GIF" class="form-check-label">GIF</label>
                </div>
                <div class="form-check">
                    <input id="TIF" name="SelectedFormat" type="radio" class="form-check-input" value="TIF" />
                    <label for="TIF" class="form-check-label">TIF</label>
                </div>
                <div class="form-check">
                    <input id="BMP" name="SelectedFormat" type="radio" class="form-check-input" value="BMP" />
                    <label for="BMP" class="form-check-label">BMP</label>
                </div>
                <div class="form-check">
                    <input id="WMP" name="SelectedFormat" type="radio" class="form-check-input" value="WMP" />
                    <label for="WMP" class="form-check-label">WMP</label>
                </div>
            </fieldset>
        </div>
        <hr />
        <button type="submit" class="btn btn-default">Generate</button>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using GemBox.Document;

namespace DocumentCore.Controllers
{
    public class DocumentController : Controller
    {
        private static readonly SelectListItem[] Countries = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
            .Select(c => new RegionInfo(c.LCID).EnglishName)
            .Distinct()
            .OrderBy(k => k)
            .Select(k => new SelectListItem() { Text = k, Value = k })
            .ToArray();

        private readonly IWebHostEnvironment environment;

        public DocumentController(IWebHostEnvironment environment)
        {
            this.environment = environment;
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        }

        [HttpGet]
        public IActionResult Create()
        {
            return View(new InvoiceModel()
            {
                Number = 1,
                Date = DateTime.Today,
                Company = "ACME Corp.",
                Address = "240 Old Country Road, Springfield, IL",
                Countries = Countries,
                Country = "United States",
                FullName = "Joe Smith",
                SelectedFormat = "DOCX"
            });
        }

        [HttpPost]
        public ActionResult Create(InvoiceModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            SaveOptions options = GetSaveOptions(model.SelectedFormat);
            DocumentModel document = this.Process(model);

            using (var stream = new MemoryStream())
            {
                document.Save(stream, options);
                return File(stream.ToArray(), options.ContentType, "Create." + model.SelectedFormat.ToLower());
            }
        }

        private static SaveOptions GetSaveOptions(string format)
        {
            switch (format.ToUpper())
            {
                case "DOCX":
                    return SaveOptions.DocxDefault;
                case "HTML":
                    return SaveOptions.HtmlDefault;
                case "RTF":
                    return SaveOptions.RtfDefault;
                case "XML":
                    return SaveOptions.XmlDefault;
                case "TXT":
                    return SaveOptions.TxtDefault;
                case "PDF":
                    return SaveOptions.PdfDefault;

                case "XPS":
                case "PNG":
                case "JPG":
                case "GIF":
                case "TIF":
                case "BMP":
                case "WMP":
                    throw new InvalidOperationException("To enable saving to XPS or image format, add 'Microsoft.WindowsDesktop.App' framework reference.");

                default:
                    throw new NotSupportedException();
            }
        }

        private DocumentModel Process(InvoiceModel model)
        {
            string path = Path.Combine(this.environment.ContentRootPath, "Invoice.docx");

            // Load template document.
            DocumentModel document = DocumentModel.Load(path);

            // Execute mail merge process.
            document.MailMerge.Execute(model);

            return document;
        }
    }

    public class InvoiceModel
    {
        public int Number { get; set; }
        public DateTime Date { get; set; }
        public string Company { get; set; }
        public string Address { get; set; }
        public IList<SelectListItem> Countries { get; set; }
        public string Country { get; set; }
        public string SelectedFormat { get; set; }
        public string FullName { get; set; }
    }
}

GemBox.Document is only licensed per developer and the licenses include a royalty-free deployment. There are no server or OEM licenses, there are no additional costs for anything (like building, testing and deploying).

So, you're free to build an unlimited number of applications and deploy or distribute them to an unlimited number of servers or end user machines with no extra cost.

Print and Save to XPS or image in .NET Core

The .NET Standard version of GemBox.Document has full functionality of .NET Framework version, but with few rendering limitations:

However, GemBox.Document provides a version for .NET Core 3.0 that has no rendering limitations. This version includes support for printing and saving to XPS or image formats, when used in .NET Core 3.0 or above application running on Windows.

But when used on a non-Windows platform then the same .NET Standard limitations are applied. For more information see Linux / macOS example.

To create an XPS or image file from ASP.NET Core 3.0 web application using GemBox.Document, you'll need to add Microsoft.WindowsDesktop.App framework reference in the project file.

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

 <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Document" Version="*" />
  </ItemGroup>

</Project>

To create an XPS or image file from .NET Core 3.0 desktop application using GemBox.Document, you'll need to use Microsoft.NET.Sdk.WindowsDesktop SDK and add UseWPF property in the project file.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Document" Version="*" />
  </ItemGroup>

</Project>

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.Document 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.