2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
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:
C# using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.DocToPDFConverter; using Syncfusion.Pdf;
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;
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:
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.
This page will automatically be redirected to the sign-in page in 10 seconds.
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