How to mail merge Word document in Docker
Mail merge is a process of merging data from a data source to Word template document. The Syncfusion Essential DocIO is a .NET Core Word library used to generate reports like invoice, payroll, letter, and more by performing mail merge faster in a batch process without Microsoft Word or interop dependencies. Using this library, you can mail merge Word document in Docker.
Steps to mail merge Word document in docker programmatically:
- Create a new C# ASP.NET Core web application project.
- Select the project template with Model-View-Controller and enable the docker support. Select the target OS (Windows or Linux) based on the requirement. In this article, Linux has been used as Target OS.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core project from NuGet.org.
- Add a new button (Create Document) in the Index.cshtml as shown below.
@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get); { <div> <input type="submit" value="Create Document" style="width:150px; height:27px" /> </div> } Html.EndForm(); }
- Include the following namespaces in the HomeController.cs file.
using System; using Syncfusion.DocIO.DLS; using Syncfusion.DocIO; using System.IO;
- Add a new action method CreateDocument in HomeController.cs and include the following code snippet to create a PDF file and download it.
//Opens the Word template document FileStream fileStreamPath = new FileStream(@"Data/Letter Formatting.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) { string[] fieldNames = { "ContactName", "CompanyName", "Address", "City", "Country", "Phone"}; string[] fieldValues = { "Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA", "(206) 555-9857-x5467" }; //Performs the mail merge document.MailMerge.Execute(fieldNames, fieldValues); //Saves the Word document to MemoryStream MemoryStream stream = new MemoryStream(); document.Save(stream, FormatType.Docx); stream.Position = 0; //Download Word document in the browser return File(stream, "application/msword", "Result.docx"); }
A complete working example of how to mail merge Word document in Docker can be downloaded from mail merge Word document.zip.
By executing the program, you will get the Word document from docker as follows.
Take a moment to peruse the documentation, where you will find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion Word Framework features.
See Also:
Mail merge Word document in ASP.NET Core
Mail merge Word document in Linux
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.