We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to mail merge Word documents and convert to PDF in Azure Functions v2

Platform: WinForms |
Control: DocIO

Syncfusion .NET Word Library generates personalized reports by mail merge on Word documents and convert it to PDF document in Azure functions using Word to PDF converter.

Steps to mail merge Word documents and convert to PDF in Azure functions v2 (.NET Standard):

  1. Create a new Azure functions project.

Azure Functions project

  1. Select framework Azure Functions v2 (.NET Standard) and select HTTP trigger as follows.

Selecting Framework Azure Functions v2

  1. Install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to your project from the NuGet.org.

Installing NuGet from nuget.org

  1. Include the following namespaces in the Function1.cs file.



using Syncfusion.Pdf;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.DocIO;
  1. Add the following code snippet in Run method of Function1 class to mail merge Word documents and convert to PDF in Azure functions and return the resultant PDF document to client end.



//JSON string
string jsonDataString = "{\"Reports\":[{\"SiteName\":\"Test Site Name\",\"SiteAddress\":\"Test Site Address\",\"ClientName\":\"Compliance 365\",\"Locations\":[{\"id\":\"1\",\"Name\":\"Location 1\",\"Description\":\"Test Description\"}]}]}";
//Gets JSON object from JSON string
JObject jsonObject = JObject.Parse(jsonDataString);
//Converts to IDictionary data from JSON object
IDictionary<string, object> data = GetData(jsonObject);
Stream stream = req.Content.ReadAsStreamAsync().Result;
//Opens the template document
WordDocument wordDocument = new WordDocument(stream, FormatType.Docx);
//Creates the mail merge data table in order to perform mail merge
MailMergeDataTable dataTable = new MailMergeDataTable("Reports", (List<object>)data["Reports"]);
//Performs the mail merge operation with the dynamic collection
//Create instance for DocIORenderer for Word to PDF conversion
DocIORenderer render = new DocIORenderer();
//Converts Word document to PDF.
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
//Release the resources used by the Word document and DocIO Renderer objects
MemoryStream memoryStream = new MemoryStream();
//Saves the PDF file
memoryStream.Position = 0;
//Create the response to return
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
//Set the PDF document content response
response.Content = new ByteArrayContent(memoryStream.ToArray());
//Set the contentDisposition as attachment
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{    FileName = "Result.pdf"
//Set the content type as PDF format mime type
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
//Return the response with output PDF stream
return response;


  1. Right-click the project and select Publish. Then, create a new profile in the Publish Window.

Publish the Azure functions

  1. Create an App service using Azure subscription and select a hosting plan.

Create an plan to publish your Azure functions

  1. The Syncfusion DocIO library works from basic hosting plan (B1). So, select the required hosting plan.

Configure hosting plan

  1. After creating the profile, click the Publish button.

Publish screenshot

  1. Now, go to Azure portal and select the Function Apps. After running the service, click the Get function URL and then click Copy.
  2. Paste the function URL in the client sample (which will request the Azure Function to mail merge Word documents and convert to PDF using the template Word document). You will get the output PDF document as follows.

Generated PDF

A complete sample to mail merge Word documents and convert to PDF in Azure Functions v2 can be downloaded from Mail merge and Word to PDF conversion in Azure Functions v2.

Steps to post the request to Azure functions with a template Word document:

  1. Create a console application to request the Azure functions API.
  2. Add the following code snippet into the Main method to post the request to Azure functions with a template Word document to mail merge and get the resultant PDF document.


//Reads the template Word document
FileStream fs = new FileStream(@"../../Data/Adventure.docx", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Position = 0;
//Saves the Word document in memory stream
MemoryStream inputStream = new MemoryStream();
inputStream.Position = 0;
    Console.WriteLine("Please enter your Azure Functions URL :");
    string functionURL = Console.ReadLine();
    //Create HttpWebRequest with hosted azure function URL               
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(functionURL);
    //Set request method as POST
    req.Method = "POST";
    //Get the request stream to save the Word document stream
    Stream stream = req.GetRequestStream();
    //Write the Word document stream into request stream
    stream.Write(inputStream.ToArray(), 0, inputStream.ToArray().Length);
    //Gets the responce from the Azure Function
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    //Saves the Word document stream
    FileStream fileStream = File.Create("Result.pdf");
    //Dispose the streams
catch (Exception ex)

A console application to post the request to Azure functions can be downloaded from WordToPDF.zip

Take a moment to peruse the documentation, where you will find the 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 conversion with code examples.

Explore more about the rich set of Syncfusion Word Framework features.


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 the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.


2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile