How to perform Word to PDF conversion in WinForms ?
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:
- Create a new Azure functions project.
- Select framework Azure Functions v1 (.NET Framework) and select HTTP trigger as follows.
Install the Syncfusion.DocToPDFConverter.AspNet NuGet package as a reference to your project from NuGet.org.
- Include the following namespaces in the Function1.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.DocToPDFConverter; using Syncfusion.Pdf;
- 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;
- Right-click the project and select Publish. Then, create a new profile in the Publish Window.
- Create an App service using Azure subscription and select a hosting plan.
- The Syncfusion DocIO library works from basic hosting plan (B1). So, select the required hosting plan.
- After creating the profile, click the Publish button.
- 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.
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:
- Create a console application to request the Azure functions API.
- 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.
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.
Conclusion
I hope you enjoyed learning about how to perform Word to PDF conversion in WinForms.
You can refer to our WinForms Word feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!