Convert Word document to PDF in Azure Functions v4

29 May 20236 minutes to read

Syncfusion DocIO is a .NET Core Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert a Word document to PDF in Azure Functions v4

Steps to convert a Word document to PDF in Azure Functions v4:

Step 1: Create a new Azure Functions project.
Create a Azure functions project

Step 2: Create a project name and select the location.
Create a project name

Step 3: Select function worker as .NET 6.0(Long-term support)
Select function worker

Step 4: Install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to your project from
Install Syncfusion.DocIORenderer.Net.Core NuGet package

Step 5: Include the following namespaces in the Function1.cs file.

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

Step 5: Add the following code snippet in Run method of Function1 class to perform convert Word to PDF in Azure Functions and return the resultant PDF document to client end.

//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, FormatType.Docx))
    //Creates an instance of the DocToPDFConverter
    using (DocIORenderer render = new DocIORenderer())
        //Converts Word document into PDF document
        using (PdfDocument pdfDocument = render.ConvertToPDF(document))
            MemoryStream memoryStream = new MemoryStream();
            //Saves the PDF file 
            //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 = "Sample.Pdf"
            //Set the content type as Word document mime type
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
            //Return the response with output Word document stream
            return response;

Step 6: Right click the project and select Publish. Then, create a new profile in the Publish Window.
Create a new profile in the Publish Window

Step 7: Select the target as Azure and click Next button
Select the target as Azure

Step 8: Select the Create new button
Configure Hosting Plan

Step 9: Click Create button
Select the plan type

Step 10: After creating app service then click Finish button
Creating app service

Step 11: Click the Publish button
Click Publish Button

Step 12: Publish has been succeed.
Publish succeeded

Step 13: 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 Functions, to perform Word to PDF conversion using the template Word document). You will get the output PDF document as follows.

Output image PDF document

Steps to post the request to Azure Functions:

Step 1: Create a console application to request the Azure Functions API.

Step 2: Add the following code snippet into Main method to post the request to Azure Functions with template Word document and get the resultant PDF document.

//Reads the template Word document.
FileStream fs = new FileStream(@"../../Data/Input.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 functions 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 Functions.
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    //Saves the PDF document stream.
    FileStream fileStream = File.Create("DocToPDF.pdf");
    //Dispose the streams
catch (Exception ex)

From GitHub, you can download the console application and Azure Functions v4.