Articles in this section
Category / Section

How to perform Mail merge in Word document and convert it as PDF in .NET Core?

5 mins read

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

  1. Create new C# .NET Core console application. Create new .Net Core console application in Visual studioCreate new .Net Core console application in Visual studio
  2. Install the Syncfusion.DocIORenderer.NET.Core NuGet package as a reference to your ASP.NET Core application from NuGet.org. Install "Syncfusion.DocIORenderer.Net.Core" NuGet packageInstall "Syncfusion.DocIORenderer.Net.Core" NuGet package
  3. 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;
    

 

  1. 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. Output converted mail merge pdf documentOutput converted mail merge pdf document

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

Note:

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.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied