Articles in this section
Category / Section

How to mail merge Word document in Azure functions v1

3 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can perform Mail merge in Word documents in Azure functions

Steps to Mail merge Word document in Azure functions:

  1. Create a new Azure function project. Create a Azure Functions project
  2. Select framework Azure Functions v1 (.NET Framework) and select HTTP trigger as follows. Select Azure Functions v1 (.NET Framework) and select HTTP trigger
  3. Install the Syncfusion.DocIO.AspNet NuGet package as a reference to your project from NuGet.org. Install required nuget package of Syncfusion.DocIO.AspNet
  4. Include the following namespaces in the Function1.cs file.

C#

using System.IO;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. Add the following code snippet in Run method of Function1 class to perform Mail Merge in Word document in Azure functions and return the resultant Word document to client end.

C#

//Gets the input Word document as stream from request
Stream stream = req.Content.ReadAsStreamAsync().Result;
//Loads an existing Word document
using (WordDocument document = new WordDocument(stream))
{
string[] fieldNames = { "ContactName", "CompanyName", "Address", "City", "Country", "Phone" };
string[] fieldValues = { "Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "SeattleWA", "USA", "(206) 555-9857-x5467" };
//Performs the mail merge.
document.MailMerge.Execute(fieldNames, fieldValues);
MemoryStream memoryStream = new MemoryStream();
//Saves the Word document to MemoryStream
document.Save(memoryStream, FormatType.Docx);
//Reset the memory stream position
memoryStream.Position = 0;
//Create the response to return
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
//Set the Word document saved stream as content of response
response.Content = new ByteArrayContent(memoryStream.ToArray());
//Set the contentDisposition as attachment
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Result.docx"
};
//Set the content type as Word document mime type
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/msword");
//Return the response with output Word document stream
return response;
}
  1. Right-click the project and select Publish. Then, create a new profile in the Publish Window. Publish the Azure functions
  2. Create an App service using Azure subscription and select a hosting plan.Create an plan to publish your Azure functions
  3. 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
  2.  Now, go to Azure portal and select the App Services. After running the service, click Get function URL by copying it. Then, paste it in the below client sample (which will request the Azure Function, to perform Mail merge in Word document using the template Word document). You will get the output Word document as follows.

Created output Word document

A complete sample to perform Mail Merge in Word document in Azure Functions can be downloaded from Mail Merge Word document in Azure Functions.zip.

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

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

C#

//Reads the template Word document.
FileStream fs = new FileStream(@"../../Data\Letter Formatting.docx", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Position = 0;
//Saves the Word document in memory stream.
MemoryStream inputStream = new MemoryStream();
fs.CopyTo(inputStream);
inputStream.Position = 0;
try
{
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.docx");
res.GetResponseStream().CopyTo(fileStream);
//Dispose the streams
inputStream.Dispose();
fileStream.Dispose();
}
catch (Exception ex)
{
throw;
}

A console application to post the request to Azure functions can be downloaded from Generate Word file.zip

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

An online example to generate or create Word document.

See Also:

Mail Merge Word document in Azure App Service

Mail Merge Word document in Azure Cloud Service

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
Please  to leave a comment
Access denied
Access denied