How to perform Mail merge in Word document and convert it as PDF in .NET Core?
Mail merge is a process of merging data from a data source to a Word template document. Syncfusion® Essential® DocIO Renderer is a .NET Core Word to PDF conversion library (Essential® DocIO and Essential® PDF) used to create mail merge Word documents and convert it as PDF in ASP.NET Core application [C#].
Steps to Mail merge Word document and convert it as PDF
- Create new C# .NET Core console application.
- Install the Syncfusion.DocIORenderer.NET.Core NuGet package as a reference to your ASP.NET Core application from NuGet.org.
- Include the following namespace in the Program.cs file.
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.DocIORenderer; using Syncfusion.Pdf; using System.IO;
- Use the following code snippet to create mail merge Word document and convert Word document to PDF.
//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); //Create instance for DocIORenderer for Word to PDF conversion DocIORenderer render = new DocIORenderer(); //Converts Word document to PDF. PdfDocument pdfDocument = render.ConvertToPDF(document); //Release the resources used by the Word document and DocIO Renderer objects. render.Dispose(); document.Dispose(); //Saves the PDF file. FileStream outputStream = new FileStream(@"Ouput.pdf", FileMode.CreateNew, FileAccess.Write); pdfDocument.Save(outputStream); //Closes the instance of PDF document object. pdfDocument.Close(); //Dispose the instance of FileStream. outputStream.Dispose(); }
A complete working example of how to create Mail merge Word document and convert Word document to PDF using .NET Core Word library can be downloaded from Mail-merged-Word-into-PDF.Zip.
By executing the example program, you will get the PDF document as like below.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, and most importantly PDF conversion with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
An online example to perform mail merge in Word document and convert Word document to PDF in ASP.NET Core Web application.
See Also:
Mail Merge Word document in ASP.NET Core
Convert Word document to PDF in ASP.NET Core
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 trail message.