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 perform Word to PDF conversion in Azure Functions v1

Platform: WinForms |
Control: DocIO |
Published Date: December 24, 2018 |
Last Revised Date: August 27, 2021

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 Word to PDF conversion in Azure functions

Steps to perform Word to PDF conversion in Azure functions:

  1. Create a new Azure functions project.

Create a Azure Functions project

  1. Select framework Azure Functions v1 (.NET Framework) and select HTTP trigger as follows.

Select Azure Functions v1 (.NET Framework) and select HTTP trigger

  1. Install the Syncfusion.DocToPDFConverter.AspNet NuGet package as a reference to your project from NuGet.org.

Install the Syncfusion.DocToPDFConverter.AspNet NuGet package

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

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocToPDFConverter;
using Syncfusion.Pdf;
  1. Add the following code snippet in Run method of Function1 class to perform Word to PDF conversion in Azure functions and return the resultant PDF 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
WordDocument document = new WordDocument(stream);
//Creates an instance of the DocToPDFConverter
DocToPDFConverter converter = new DocToPDFConverter();
//Converts Word document into PDF document
PdfDocument pdfDocument = converter.ConvertToPDF(document);
//Releases the resources occupied by DocToPDFConverter instance
converter.Dispose();
//Closes the Word document
document.Close();
MemoryStream memoryStream = new MemoryStream();
//Saves the PDF file 
pdfDocument.Save(memoryStream);
//Closes the PDF document
pdfDocument.Close();
//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 = "DocToPDF.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;
  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
  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 Word to PDF conversion using the template Word document). You will get the output PDF document as follows. Generated PDF document

A complete sample to perform Word to PDF conversion in Azure Functions can be downloaded from Word to PDF conversion 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 PDF document.

C#

//Reads the template Word document.
FileStream fs = new FileStream(@"../../Data/DoctoPDF.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 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");
    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 WordToPDF.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.

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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
rfazley
Jul 01, 2019

Getting an Unhandled Exception: System.Net.WebException: The remote server returned an error: (500) Internal Server Error. at System.Net.HttpWebRequest.GetResponse() at WordToPDF.Program.Main(String[] args) in C:\Users\User\Source\Repos\WordToPDF\Program.cs:line 53

Reply

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