2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
Syncfusion .NET Word Library generates personalized reports by mail merge on Word documents and convert it to PDF document in Azure functions using Word to PDF converter. Steps to mail merge Word documents and convert to PDF in Azure functions v2 (.NET Standard):
C# using Syncfusion.Pdf; using Syncfusion.DocIO.DLS; using Syncfusion.DocIORenderer; using Syncfusion.DocIO;
C# //JSON string string jsonDataString = "{\"Reports\":[{\"SiteName\":\"Test Site Name\",\"SiteAddress\":\"Test Site Address\",\"ClientName\":\"Compliance 365\",\"Locations\":[{\"id\":\"1\",\"Name\":\"Location 1\",\"Description\":\"Test Description\"}]}]}"; //Gets JSON object from JSON string JObject jsonObject = JObject.Parse(jsonDataString); //Converts to IDictionary data from JSON object IDictionary<string, object> data = GetData(jsonObject); Stream stream = req.Content.ReadAsStreamAsync().Result; //Opens the template document WordDocument wordDocument = new WordDocument(stream, FormatType.Docx); //Creates the mail merge data table in order to perform mail merge MailMergeDataTable dataTable = new MailMergeDataTable("Reports", (List<object>)data["Reports"]); //Performs the mail merge operation with the dynamic collection wordDocument.MailMerge.ExecuteNestedGroup(dataTable); //Create instance for DocIORenderer for Word to PDF conversion DocIORenderer render = new DocIORenderer(); //Converts Word document to PDF. PdfDocument pdfDocument = render.ConvertToPDF(wordDocument); //Release the resources used by the Word document and DocIO Renderer objects render.Dispose(); wordDocument.Dispose(); MemoryStream memoryStream = new MemoryStream(); //Saves the PDF file pdfDocument.Save(memoryStream); memoryStream.Position = 0; //Create the response to return HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); //Set the PDF document content response response.Content = new ByteArrayContent(memoryStream.ToArray()); //Set the contentDisposition as attachment response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Result.pdf" }; //Set the content type as PDF format mime type response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf"); //Return the response with output PDF stream return response;
A complete sample to mail merge Word documents and convert to PDF in Azure Functions v2 can be downloaded from Mail merge and Word to PDF conversion in Azure Functions v2. Steps to post the request to Azure functions with a template Word document:
C# //Reads the template Word document FileStream fs = new FileStream(@"../../Data/Adventure.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.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 will find the 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 conversion 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 the 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.