diff --git a/Document-Processing-toc.html b/Document-Processing-toc.html index eeb1d387d3..aa667a853e 100644 --- a/Document-Processing-toc.html +++ b/Document-Processing-toc.html @@ -4526,6 +4526,9 @@
  • Azure Functions v4
  • +
  • + Azure Functions Flex Consumption +
  • @@ -4655,6 +4658,9 @@
  • Azure Functions v4
  • +
  • + Azure Functions Flex Consumption +
  • @@ -4917,6 +4923,9 @@
  • Azure Functions v4
  • +
  • + Azure Functions Flex Consumption +
  • @@ -5003,6 +5012,9 @@
  • Azure Functions v4
  • +
  • + Azure Functions Flex Consumption +
  • @@ -5168,6 +5180,9 @@
  • Azure Functions v4
  • +
  • + Azure Functions Flex Consumption +
  • @@ -5294,6 +5309,9 @@
  • Azure Functions v4
  • +
  • + Azure Functions Flex Consumption +
  • diff --git a/Document-Processing/PowerPoint/Conversions/PowerPoint-To-Image/NET/Performance-metrics.md b/Document-Processing/PowerPoint/Conversions/PowerPoint-To-Image/NET/Performance-metrics.md index 6d19a8c92d..7281d90843 100644 --- a/Document-Processing/PowerPoint/Conversions/PowerPoint-To-Image/NET/Performance-metrics.md +++ b/Document-Processing/PowerPoint/Conversions/PowerPoint-To-Image/NET/Performance-metrics.md @@ -18,7 +18,7 @@ The following system configurations were used for benchmarking: * **Processor:** 12th Gen Intel(R) Core(TM) i5-1235U (1.30 GHz) * **RAM:** 24GB * **.NET Version:** .NET 8.0 -* **Syncfusion® Version:** [Syncfusion.PresentationRenderer.Net.Core v32.1.19](https://www.nuget.org/packages/Syncfusion.PresentationRenderer.Net.Core/32.1.19) +* **Syncfusion® Version:** [Syncfusion.PresentationRenderer.Net.Core v33.1.44](https://www.nuget.org/packages/Syncfusion.PresentationRenderer.Net.Core/33.1.44) ## PowerPoint to image conversion @@ -46,8 +46,10 @@ The following system configurations were used for benchmarking: 500 {{'[PowerPoint-500.pptx](https://github.com/SyncfusionExamples/PowerPoint-Examples/tree/master/Performance-metrices/PPTX-to-Image/.NET/Convert-PowerPoint-slide-to-Image/Data/PowerPoint-500.pptx)'| markdownify }} - 23.5 + 23.4 You can find the sample used for this performance evaluation on [GitHub](https://github.com/SyncfusionExamples/PowerPoint-Examples/tree/master/Performance-metrices/PPTX-to-Image/). + +N> Execution times are based on the sample documents and may vary with different content or environments. \ No newline at end of file diff --git a/Document-Processing/PowerPoint/Conversions/PowerPoint-To-PDF/NET/Performance-metrics.md b/Document-Processing/PowerPoint/Conversions/PowerPoint-To-PDF/NET/Performance-metrics.md index 5a6d3ef270..f8ba52d31b 100644 --- a/Document-Processing/PowerPoint/Conversions/PowerPoint-To-PDF/NET/Performance-metrics.md +++ b/Document-Processing/PowerPoint/Conversions/PowerPoint-To-PDF/NET/Performance-metrics.md @@ -18,7 +18,7 @@ The following system configurations were used for benchmarking: * **Processor:** 12th Gen Intel(R) Core(TM) i5-1235U (1.30 GHz) * **RAM:** 24GB * **.NET Version:** .NET 8.0 -* **Syncfusion® Version:** [Syncfusion.PresentationRenderer.Net.Core v32.1.19](https://www.nuget.org/packages/Syncfusion.PresentationRenderer.Net.Core/32.1.19) +* **Syncfusion® Version:** [Syncfusion.PresentationRenderer.Net.Core v33.1.44](https://www.nuget.org/packages/Syncfusion.PresentationRenderer.Net.Core/33.1.44) ## PowerPoint to PDF conversion @@ -46,8 +46,10 @@ The following system configurations were used for benchmarking: 500 {{'[PowerPoint-500.pptx](https://github.com/SyncfusionExamples/PowerPoint-Examples/tree/master/Performance-metrices/PPTX-to-PDF/.NET/PPTX-to-PDF/Data/PowerPoint-500.pptx)'| markdownify }} - 13.5 + 13.4 You can find the sample used for this performance evaluation on [GitHub](https://github.com/SyncfusionExamples/PowerPoint-Examples/tree/master/Performance-metrices/PPTX-to-PDF/). + +N> Execution times are based on the sample documents and may vary with different content or environments. \ No newline at end of file diff --git a/Document-Processing/PowerPoint/PowerPoint-Library/NET/Performance-metrics.md b/Document-Processing/PowerPoint/PowerPoint-Library/NET/Performance-metrics.md index 2f74883cb8..e0fe37993c 100644 --- a/Document-Processing/PowerPoint/PowerPoint-Library/NET/Performance-metrics.md +++ b/Document-Processing/PowerPoint/PowerPoint-Library/NET/Performance-metrics.md @@ -18,7 +18,7 @@ The following system configurations were used for benchmarking: * **Processor:** 12th Gen Intel(R) Core(TM) i5-1235U (1.30 GHz) * **RAM:** 24GB * **.NET Version:** .NET 8.0 -* **Syncfusion® Version:** [Syncfusion.Presentation.Net.Core v32.1.19](https://www.nuget.org/packages/Syncfusion.Presentation.Net.Core/32.1.19) +* **Syncfusion® Version:** [Syncfusion.Presentation.Net.Core v33.1.44](https://www.nuget.org/packages/Syncfusion.Presentation.Net.Core/33.1.44) ## Open and save Presentation @@ -46,7 +46,7 @@ The following system configurations were used for benchmarking: 500 {{'[PowerPoint-500.pptx](https://github.com/SyncfusionExamples/PowerPoint-Examples/tree/master/Performance-metrices/Open-and-save/.NET/Open-and-save-PowerPoint/Data/PowerPoint-500.pptx)'| markdownify }} - 1.06 + 1.04 @@ -84,4 +84,6 @@ You can find the sample used for this performance evaluation on [GitHub](https:/ You can find the sample used for this performance evaluation on [GitHub](https://github.com/SyncfusionExamples/PowerPoint-Examples/tree/master/Performance-metrices/Clone-and-merge-slides/). -You can find the performance benchmark report for [PowerPoint to PDF](https://help.syncfusion.com/document-processing/powerpoint/conversions/powerpoint-to-pdf/net/performance-metrics) and [PowerPoint to Image](https://help.syncfusion.com/document-processing/powerpoint/conversions/powerpoint-to-image/net/performance-metrics) conversion. \ No newline at end of file +You can find the performance benchmark report for [PowerPoint to PDF](https://help.syncfusion.com/document-processing/powerpoint/conversions/powerpoint-to-pdf/net/performance-metrics) and [PowerPoint to Image](https://help.syncfusion.com/document-processing/powerpoint/conversions/powerpoint-to-image/net/performance-metrics) conversion. + +N> Execution times are based on the sample documents and may vary with different content or environments. \ No newline at end of file diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_to_Image.png new file mode 100644 index 0000000000..3707ae8583 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/After_Publish_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/After_Publish_Word_to_Image.png new file mode 100644 index 0000000000..9459379b7d Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/After_Publish_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Azure_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Azure_Word_to_Image.png new file mode 100644 index 0000000000..f41c848328 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Azure_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_to_Image.png new file mode 100644 index 0000000000..edc7b81c15 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Configure_PowerPoint_Presentation_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Configure_PowerPoint_Presentation_to_PDF.png new file mode 100644 index 0000000000..d5064e5571 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Configure_PowerPoint_Presentation_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Configure_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Configure_Word_to_Image.png new file mode 100644 index 0000000000..aa79345d03 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Configure_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Finish_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Finish_Word_to_Image.png new file mode 100644 index 0000000000..ee7902a017 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Finish_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_to_Image.png new file mode 100644 index 0000000000..cd27196a89 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Hosting_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Hosting_Word_to_Image.png new file mode 100644 index 0000000000..aacf4acae0 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Hosting_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_SkiaSharp_Native_Linux_NoDependencies.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_SkiaSharp_Native_Linux_NoDependencies.png new file mode 100644 index 0000000000..d37396eb81 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_SkiaSharp_Native_Linux_NoDependencies.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_Word_to_Image.png new file mode 100644 index 0000000000..c06b39d00b Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Output_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Output_Word_to_Image.png new file mode 100644 index 0000000000..8ff379e4ae Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Output_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Publish_Wordto_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Publish_Wordto_Image.png new file mode 100644 index 0000000000..7ecde99304 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Publish_Wordto_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_to_Image.png new file mode 100644 index 0000000000..1db0017891 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Target_Word_to_Image.png b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Target_Word_to_Image.png new file mode 100644 index 0000000000..0d5f43da1b Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-Image/NET/Azure-Images/Functions-Flex-Consumption/Target_Word_to_Image.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Convert-Word-Document-to-Image-in-Azure-Functions-Flex-Consumption.md b/Document-Processing/Word/Conversions/Word-To-Image/NET/Convert-Word-Document-to-Image-in-Azure-Functions-Flex-Consumption.md new file mode 100644 index 0000000000..e3f073bfc3 --- /dev/null +++ b/Document-Processing/Word/Conversions/Word-To-Image/NET/Convert-Word-Document-to-Image-in-Azure-Functions-Flex-Consumption.md @@ -0,0 +1,197 @@ +--- +title: Convert Word to Image in Azure Functions Flex Consumption | Syncfusion +description: Convert Word to Image in Azure Functions Flex Consumption using .NET Core Word (DocIO) library without Microsoft Word or interop dependencies. +platform: document-processing +control: DocIO +documentation: UG +--- + +# Convert Word document to Image in Azure Functions (Flex Consumption) + +Syncfusion® DocIO is a [.NET Core Word library](https://www.syncfusion.com/document-processing/word-framework/net/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 image in Azure Functions deployed on Flex (Consumption) plan**. + +## Steps to convert a Word document to Image in Azure Functions (Flex Consumption) + +Step 1: Create a new Azure Functions project. +![Create a Azure Functions project](Azure-Images/Functions-Flex-Consumption/Azure_Word_to_Image.png) + +Step 2: Create a project name and select the location. +![Create a project name](Azure-Images/Functions-Flex-Consumption/Configure_Word_to_Image.png) + +Step 3: Select function worker as **.NET 8.0 (Long Term Support)** (isolated worker) and target Flex/Consumption hosting suitable for isolated worker. +![Select function worker](Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_to_Image.png) + +Step 4: Install the [Syncfusion.DocIORenderer.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIORenderer.Net.Core) and [SkiaSharp.NativeAssets.Linux.NoDependencies v3.119.1](https://www.nuget.org/packages/SkiaSharp.NativeAssets.Linux.NoDependencies/3.119.1) NuGet packages as references to your project from [NuGet.org](https://www.nuget.org/). +![Install NuGet packages](Azure-Images/Functions-Flex-Consumption/Nuget_Package_Word_to_Image.png) +![Install NuGet packages](Azure-Images/Functions-Flex-Consumption/Nuget_Package_SkiaSharp_Native_Linux_NoDependencies.png) + +N> Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add "Syncfusion.Licensing" assembly reference and include a license key in your projects. Please refer to this [link](https://help.syncfusion.com/common/essential-studio/licensing/overview) to know about registering Syncfusion® license key in your application to use our components. + +Step 5: Include the following namespaces in the **Function1.cs** file. + +{% tabs %} + +{% highlight c# tabtitle="C#" %} +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIORenderer; +{% endhighlight %} + +{% endtabs %} + +Step 6: Add the following code snippet in **Run** method of **Function1** class to perform **Word document to image conversion** in Azure Functions and return the resultant **image** to client end. + +{% tabs %} +{% highlight c# tabtitle="C#" %} + +public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req) + { + try + { + // Create a memory stream to hold the incoming request body (Word document bytes) + await using MemoryStream inputStream = new MemoryStream(); + // Copy the request body into the memory stream + await req.Body.CopyToAsync(inputStream); + // Check if the stream is empty (no file content received) + if (inputStream.Length == 0) + return new BadRequestObjectResult("No file content received in request body."); + // Reset stream position to the beginning for reading + inputStream.Position = 0; + // Load the Word document from the stream (auto-detects format type) + using WordDocument document = new WordDocument(inputStream, Syncfusion.DocIO.FormatType.Automatic); + // Attach font substitution handler to manage missing fonts + document.FontSettings.SubstituteFont += FontSettings_SubstituteFont; + // Initialize the DocIORenderer to perform image conversion. + DocIORenderer render = new DocIORenderer(); + // Convert Word document to image as stream. + Stream imageStream = document.RenderAsImages(0, ExportImageFormat.Png); + // Reset the stream position. + imageStream.Position = 0; + // Create a memory stream to hold the Image output + await using MemoryStream outputStream = new MemoryStream(); + // Copy the contents of the image stream to the memory stream. + await imageStream.CopyToAsync(outputStream); + // Convert the Image stream to a byte array + var imageBytes = outputStream.ToArray(); + //Reset the stream position. + imageStream.Position = 0; + // Reset stream position to the beginning for reading + outputStream.Position = 0; + // Create a file result to return the PNG as a downloadable file + return new FileContentResult(imageBytes, "image/png") + { + FileDownloadName = "document-1.png" + }; + } + catch (Exception ex) + { + // Log the error with details for troubleshooting + _logger.LogError(ex, "Error converting Word document to Image."); + // Prepare error message including exception details + var msg = $"Exception: {ex.Message}\n\n{ex}"; + // Return a 500 Internal Server Error response with the message + return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" }; + } + } + /// + /// Event handler for font substitution during Image conversion + /// + /// + /// + private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args) + { + // Define the path to the Fonts folder in the application base directory + string fontsFolder = Path.Combine(AppContext.BaseDirectory, "Fonts"); + // If the original font is Calibri, substitute with calibri-regular.ttf + if (args.OriginalFontName == "Calibri") + { + args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "calibri-regular.ttf")); + } + // Otherwise, substitute with Times New Roman + else + { + args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "Times New Roman.ttf")); + } + } + +{% endhighlight %} +{% endtabs %} + +Step 7: Right click the project and select **Publish**. Then, create a new profile in the Publish Window. +![Create a new profile in the Publish Window](Azure-Images/Functions-Flex-Consumption/Publish_Wordto_Image.png) + +Step 8: Select the target as **Azure** and click **Next** button. +![Select the target as Azure](Azure-Images/Functions-Flex-Consumption/Target_Word_to_Image.png) + +Step 9: Select the specific target as **Azure Function App** and click **Next** button. +![Select the target as Azure](Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_to_Image.png) + +Step 10: Select the **Create new** button. +![Configure Hosting Plan](Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_to_Image.png) + +Step 11: Click **Create** button. +![Select the plan type](Azure-Images/Functions-Flex-Consumption/Hosting_Word_to_Image.png) + +Step 12: After creating app service then click **Finish** button. +![Creating app service](Azure-Images/Functions-Flex-Consumption/Finish_Word_to_Image.png) + +Step 13: Click the **Publish** button. +![Click Publish Button](Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_to_Image.png) + +Step 14: Publish has been succeed. +![Publish succeeded](Azure-Images/Functions-Flex-Consumption/After_Publish_Word_to_Image.png) + +Step 15: 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 document to image conversion** using the template Word document). You will get the output **image** as follows. + +![Word to Image in Azure Functions Flex Consumption](Azure-Images/Functions-Flex-Consumption/Output_Word_to_Image.png) + +## 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 image. + +{% tabs %} +{% highlight c# tabtitle="C#" %} +static async Task Main() + { + try + { + Console.Write("Please enter your Azure Functions URL : "); + string url = Console.ReadLine(); + if (string.IsNullOrEmpty(url)) return; + // Create a new HttpClient instance for sending HTTP requests + using var http = new HttpClient(); + // Read all bytes from the input Word document + byte[] bytes = await File.ReadAllBytesAsync(@"Data/Input.docx"); + // Wrap the file bytes into a ByteArrayContent object for HTTP transmission + using var content = new ByteArrayContent(bytes); + // Set the content type header to indicate binary data + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); + // Send a POST request to the provided Azure Functions URL with the file content + using var res = await http.PostAsync(url, content); + // Read the response body as a byte array + var resBytes = await res.Content.ReadAsByteArrayAsync(); + // Extract the media type from the response headers (e.g., "image/png") + string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty; + // Decide the output file path the response is an image or txt + string outputPath = mediaType.Contains("image", StringComparison.OrdinalIgnoreCase) + ? Path.GetFullPath(@"../../../Output/image-1.png") + : Path.GetFullPath(@"../../../function-error.txt"); + // Write the response bytes to the output file + await File.WriteAllBytesAsync(outputPath, resBytes); + Console.WriteLine($"Saved: {outputPath}"); + } + catch (Exception ex) + { + throw; + } + } +{% endhighlight %} +{% endtabs %} + +From GitHub, you can download the [console application](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption) and [Azure Functions Flex Consumption](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption). + +Click [here](https://www.syncfusion.com/document-processing/word-framework/net-core) to explore the rich set of Syncfusion® Word library (DocIO) features. + +An online sample link to [convert Word document to image](https://document.syncfusion.com/demos/word/wordtoimage#/tailwind) in ASP.NET Core. \ No newline at end of file diff --git a/Document-Processing/Word/Conversions/Word-To-Image/NET/Performance-metrics.md b/Document-Processing/Word/Conversions/Word-To-Image/NET/Performance-metrics.md index 6e6cdce42c..25f112f978 100644 --- a/Document-Processing/Word/Conversions/Word-To-Image/NET/Performance-metrics.md +++ b/Document-Processing/Word/Conversions/Word-To-Image/NET/Performance-metrics.md @@ -18,7 +18,7 @@ The following system configurations were used for benchmarking: * **Processor:** AMD Ryzen 5 7520U with Radeon Graphics * **RAM:** 16GB * **.NET Version:** .NET 8.0 -* **Syncfusion® Version:** [Syncfusion.DocIORenderer.Net.Core v32.2.3](https://www.nuget.org/packages/Syncfusion.DocIORenderer.Net.Core/32.2.3) +* **Syncfusion® Version:** [Syncfusion.DocIORenderer.Net.Core v33.1.44](https://www.nuget.org/packages/Syncfusion.DocIORenderer.Net.Core/33.1.44) ## Benchmark Results @@ -34,7 +34,7 @@ The table below shows the performance results of various Word document operation {{'[Word to Image](https://help.syncfusion.com/document-processing/word/conversions/word-to-image/net/word-to-image)'| markdownify }} 100 pages - 7.77 + 7.35 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Word-to-Image/)'| markdownify }} @@ -46,7 +46,7 @@ The table below shows the performance results of various Word document operation {{'[Font-Substitution](https://help.syncfusion.com/document-processing/word/conversions/word-to-image/net/font-substituion-word-to-image)'| markdownify }} 2 pages - 0.79 + 0.86 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Font-Substitution-Image/)'| markdownify }} diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_to_PDF.png new file mode 100644 index 0000000000..3707ae8583 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/After_Publish_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/After_Publish_Word_to_PDF.png new file mode 100644 index 0000000000..9459379b7d Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/After_Publish_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Azure_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Azure_Word_to_PDF.png new file mode 100644 index 0000000000..f41c848328 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Azure_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_to_PDF.png new file mode 100644 index 0000000000..edc7b81c15 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Configuration_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Configuration_Word_to_PDF.png new file mode 100644 index 0000000000..32d90145bf Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Configuration_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Finish_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Finish_Word_to_PDF.png new file mode 100644 index 0000000000..ee7902a017 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Finish_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_to_PDF.png new file mode 100644 index 0000000000..cd27196a89 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Hosting_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Hosting_Word_to_PDF.png new file mode 100644 index 0000000000..aacf4acae0 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Hosting_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_SkiaSharp_Native_Linux_NoDependencies.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_SkiaSharp_Native_Linux_NoDependencies.png new file mode 100644 index 0000000000..707e14bf01 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_SkiaSharp_Native_Linux_NoDependencies.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_Word_to_PDF.png new file mode 100644 index 0000000000..c39ce44523 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Nuget_Package_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Publish_WordtoPDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Publish_WordtoPDF.png new file mode 100644 index 0000000000..f0bdcca998 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Publish_WordtoPDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_to_PDF.png new file mode 100644 index 0000000000..1db0017891 Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Target_Word_to_PDF.png b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Target_Word_to_PDF.png new file mode 100644 index 0000000000..0d5f43da1b Binary files /dev/null and b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Azure-Images/Functions-Flex-Consumption/Target_Word_to_PDF.png differ diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Convert-Word-Document-to-PDF-in-Azure-Functions-Flex-Consumption.md b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Convert-Word-Document-to-PDF-in-Azure-Functions-Flex-Consumption.md new file mode 100644 index 0000000000..d5dc0b7e20 --- /dev/null +++ b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Convert-Word-Document-to-PDF-in-Azure-Functions-Flex-Consumption.md @@ -0,0 +1,193 @@ +--- +title: Convert Word to PDF in Azure Functions Flex Consumption | Syncfusion +description: Convert Word to PDF in Azure Functions Flex Consumption using .NET Core Word (DocIO) library without Microsoft Word or interop dependencies. +platform: document-processing +control: DocIO +documentation: UG +--- + +# Convert Word document to PDF in Azure Functions (Flex Consumption) + +Syncfusion® DocIO is a [.NET Core Word library](https://www.syncfusion.com/document-processing/word-framework/net/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 deployed on Flex (Consumption) plan**. + +## Steps to convert a Word document to PDF in Azure Functions (Flex Consumption) + +Step 1: Create a new Azure Functions project. +![Create a Azure Functions project](Azure-Images/Functions-Flex-Consumption/Azure_Word_to_PDF.png) + +Step 2: Create a project name and select the location. +![Create a project name](Azure-Images/Functions-Flex-Consumption/Configuration_Word_to_PDF.png) + +Step 3: Select function worker as **.NET 8.0 (Long Term Support)** (isolated worker) and target Flex/Consumption hosting suitable for isolated worker. +![Select function worker](Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_to_PDF.png) + +Step 4: Install the [Syncfusion.DocIORenderer.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIORenderer.Net.Core) and [SkiaSharp.NativeAssets.Linux.NoDependencies v3.119.1](https://www.nuget.org/packages/SkiaSharp.NativeAssets.Linux.NoDependencies/3.119.1) NuGet packages as references to your project from [NuGet.org](https://www.nuget.org/). +![Install NuGet packages](Azure-Images/Functions-Flex-Consumption/Nuget_Package_Word_to_PDF.png) +![Install NuGet packages](Azure-Images/Functions-Flex-Consumption/Nuget_Package_SkiaSharp_Native_Linux_NoDependencies.png) + +N> Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add "Syncfusion.Licensing" assembly reference and include a license key in your projects. Please refer to this [link](https://help.syncfusion.com/common/essential-studio/licensing/overview) to know about registering Syncfusion® license key in your application to use our components. + +Step 5: Include the following namespaces in the **Function1.cs** file. + +{% tabs %} + +{% highlight c# tabtitle="C#" %} +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIORenderer; +using Syncfusion.Pdf; +{% endhighlight %} + +{% endtabs %} + +Step 6: Add the following code snippet in **Run** method of **Function1** class to perform **Word document to PDF conversion** in Azure Functions and return the resultant **PDF** to client end. + +{% tabs %} +{% highlight c# tabtitle="C#" %} + +public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req) + { + try + { + // Create a memory stream to hold the incoming request body (Word document bytes) + await using MemoryStream inputStream = new MemoryStream(); + // Copy the request body into the memory stream + await req.Body.CopyToAsync(inputStream); + // Check if the stream is empty (no file content received) + if (inputStream.Length == 0) + return new BadRequestObjectResult("No file content received in request body."); + // Reset stream position to the beginning for reading + inputStream.Position = 0; + // Load the Word document from the stream (auto-detects format type) + using WordDocument document = new WordDocument(inputStream, FormatType.Automatic); + // Attach font substitution handler to manage missing fonts + document.FontSettings.SubstituteFont += FontSettings_SubstituteFont; + // Initialize DocIORenderer to convert Word document to PDF + using DocIORenderer renderer = new DocIORenderer(); + // Convert the Word document to a PDF document + using PdfDocument pdfDoc = renderer.ConvertToPDF(document); + // Create a memory stream to hold the PDF output + await using MemoryStream outputStream = new MemoryStream(); + // Save the PDF into the output stream + pdfDoc.Save(outputStream); + // Close the PDF document and release resources + pdfDoc.Close(true); + // Reset stream position to the beginning for reading + outputStream.Position = 0; + // Convert the PDF stream to a byte array + var pdfBytes = outputStream.ToArray(); + + // Create a file result to return the PDF as a downloadable file + var fileResult = new FileContentResult(pdfBytes, "application/pdf") + { + FileDownloadName = "converted.pdf" + }; + // Return the PDF file result to the client + return fileResult; + } + catch (Exception ex) + { + // Log the error with details for troubleshooting + _logger.LogError(ex, "Error converting DOCX to PDF."); + // Prepare error message including exception details + var msg = $"Exception: {ex.Message}\n\n{ex}"; + // Return a 500 Internal Server Error response with the message + return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" }; + } + } + /// + /// Event handler for font substitution during PDF conversion + /// + /// + /// + private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args) + { + // Define the path to the Fonts folder in the application base directory + string fontsFolder = Path.Combine(AppContext.BaseDirectory, "Fonts"); + // If the original font is Calibri, substitute with calibri-regular.ttf + if (args.OriginalFontName == "Calibri") + { + args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "calibri-regular.ttf")); + } + // Otherwise, substitute with Times New Roman + else + { + args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "Times New Roman.ttf")); + } + } + +{% endhighlight %} +{% endtabs %} + +Step 7: Right click the project and select **Publish**. Then, create a new profile in the Publish Window. +![Create a new profile in the Publish Window](Azure-Images/Functions-Flex-Consumption/Publish_WordtoPDF.png) + +Step 8: Select the target as **Azure** and click **Next** button. +![Select the target as Azure](Azure-Images/Functions-Flex-Consumption/Target_Word_to_PDF.png) + +Step 9: Select the specific target as **Azure Function App** and click **Next** button. +![Select the target as Azure](Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_to_PDF.png) + +Step 10: Select the **Create new** button. +![Configure Hosting Plan](Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_to_PDF.png) + +Step 11: Click **Create** button. +![Select the plan type](Azure-Images/Functions-Flex-Consumption/Hosting_Word_to_PDF.png) + +Step 12: After creating app service then click **Finish** button. +![Creating app service](Azure-Images/Functions-Flex-Consumption/Finish_Word_to_PDF.png) + +Step 13: Click the **Publish** button. +![Click Publish Button](Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_to_PDF.png) + +Step 14: Publish has been succeed. +![Publish succeeded](Azure-Images/Functions-Flex-Consumption/After_Publish_Word_to_PDF.png) + +Step 15: 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 document to PDF conversion** using the template Word document). You will get the output **PDF** as follows. + +![Word to PDF in Azure Functions v1](WordToPDF_images/WordToPDF_Output_Cloud.png) + +## 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. + +{% tabs %} +{% highlight c# tabtitle="C#" %} +static async Task Main() + { + Console.Write("Please enter your Azure Functions URL : "); + // Read the URL entered by the user and trim whitespace + string url = Console.ReadLine()?.Trim(); + // If no URL was entered, exit the program + if (string.IsNullOrEmpty(url)) return; + // Create a new HttpClient instance for sending requests + using var http = new HttpClient(); + // Read all bytes from the input Word document file + var bytes = await File.ReadAllBytesAsync(@"Data/Input.docx"); + // Create HTTP content from the document bytes + using var content = new ByteArrayContent(bytes); + // Set the content type header to application/octet-stream (binary data) + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); + // Send a POST request to the Azure Function with the document content + using var res = await http.PostAsync(url, content); + // Read the response content as a byte array + var resBytes = await res.Content.ReadAsByteArrayAsync(); + // Get the media type (e.g., application/pdf or text/plain) from the response headers + string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty; + string outFile = mediaType.Contains("pdf", StringComparison.OrdinalIgnoreCase) + ? Path.GetFullPath(@"../../../Output/Output.pdf") + : Path.GetFullPath(@"../../../Output/function-error.txt"); + // Write the response bytes to the chosen output file + await File.WriteAllBytesAsync(outFile, resBytes); + Console.WriteLine($"Saved: {outFile} "); + } +{% endhighlight %} +{% endtabs %} + +From GitHub, you can download the [console application](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption) and [Azure Functions Flex Consumption](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption). + +Click [here](https://www.syncfusion.com/document-processing/word-framework/net) to explore the rich set of Syncfusion® Word library (DocIO) features. + +An online sample link to [convert Word document to PDF](https://document.syncfusion.com/demos/word/wordtopdf#/tailwind) in ASP.NET Core. \ No newline at end of file diff --git a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Performance-metrics.md b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Performance-metrics.md index df96637176..9a366019c9 100644 --- a/Document-Processing/Word/Conversions/Word-To-PDF/NET/Performance-metrics.md +++ b/Document-Processing/Word/Conversions/Word-To-PDF/NET/Performance-metrics.md @@ -18,7 +18,7 @@ The following system configurations were used for benchmarking: * **Processor:** AMD Ryzen 5 7520U with Radeon Graphics * **RAM:** 16GB * **.NET Version:** .NET 8.0 -* **Syncfusion® Version:** [Syncfusion.DocIORenderer.Net.Core v32.2.3](https://www.nuget.org/packages/Syncfusion.DocIORenderer.Net.Core/32.2.3) +* **Syncfusion® Version:** [Syncfusion.DocIORenderer.Net.Core v33.1.44](https://www.nuget.org/packages/Syncfusion.DocIORenderer.Net.Core/33.1.44) ## Benchmark Results @@ -34,13 +34,13 @@ The table below shows the performance results of various Word document operation {{'[Word to PDF](https://help.syncfusion.com/document-processing/word/conversions/word-to-pdf/net/word-to-pdf)'| markdownify }} 100 pages - 5.45 + 5.24 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Word-to-PDF/)'| markdownify }} {{'[Accessible PDF](https://help.syncfusion.com/document-processing/word/conversions/word-to-pdf/net/word-to-pdf-settings#accessible-pdf-document)'| markdownify }} 2 pages - 1.1 + 1.03 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Accessible-PDF/)'| markdownify }} @@ -52,49 +52,49 @@ The table below shows the performance results of various Word document operation {{'[Embed fonts in PDF](https://help.syncfusion.com/document-processing/word/conversions/word-to-pdf/net/word-to-pdf#embedding-fonts)'| markdownify }} 2 pages - 0.98 + 1.06 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Embed-fonts-in-PDF/)'| markdownify }} {{'[Export bookmarks](https://help.syncfusion.com/document-processing/word/conversions/word-to-pdf/net/word-to-pdf-settings#word-document-headings-to-pdf-bookmarks)'| markdownify }} 2 pages - 0.92 + 0.98 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Export-Bookmarks/)'| markdownify }} {{'[Fallback font](https://help.syncfusion.com/document-processing/word/conversions/word-to-pdf/net/fallback-fonts-word-to-pdf)'| markdownify }} 1 page - 0.79 + 0.84 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Fallback-Font-PDF/)'| markdownify }} {{'[Font-Substitution](https://help.syncfusion.com/document-processing/word/conversions/word-to-pdf/net/font-substituion-word-to-pdf)'| markdownify }} 2 pages - 0.89 + 0.95 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Font-substitution-PDF/)'| markdownify }} {{'[PDF Conformance Level](https://help.syncfusion.com/document-processing/word/conversions/word-to-pdf/net/word-to-pdf-settings#pdf-conformance-level)'| markdownify }} 2 pages - 0.92 + 0.99 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/PDF-Conformance-Level/)'| markdownify }} {{'[Preserve Form Fields](https://help.syncfusion.com/document-processing/word/conversions/word-to-pdf/net/word-to-pdf-settings#word-document-form-field-to-pdf-form-field)'| markdownify }} 1 page - 0.79 + 0.85 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Preserve-Form-Fields/)'| markdownify }} {{'[Track changes](https://help.syncfusion.com/document-processing/word/conversions/word-to-pdf/net/word-to-pdf-settings#track-changes-in-word-to-pdf-conversion)'| markdownify }} 1 page - 0.91 + 0.93 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Track%20changes/)'| markdownify }} {{'[Use embedded word fonts](https://support.syncfusion.com/kb/article/13969/how-to-resolve-font-problems-during-word-to-pdf-or-image-conversion#suggestion-3:-embed-fonts-in-docx)'| markdownify }} 2 pages - 1.16 + 1.13 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Use-embeded-word-font-PDF/)'| markdownify }} diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_Document.png new file mode 100644 index 0000000000..3707ae8583 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/After_Publish_Word_Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/After_Publish_Word_Document.png new file mode 100644 index 0000000000..9459379b7d Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/After_Publish_Word_Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Azure_Word_Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Azure_Word_Document.png new file mode 100644 index 0000000000..f41c848328 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Azure_Word_Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_Document.png new file mode 100644 index 0000000000..edc7b81c15 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Configuration-Create-Word-Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Configuration-Create-Word-Document.png new file mode 100644 index 0000000000..c4d881c0a7 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Configuration-Create-Word-Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Configuration-Open-and-Save-Word-Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Configuration-Open-and-Save-Word-Document.png new file mode 100644 index 0000000000..ee233581a2 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Configuration-Open-and-Save-Word-Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Finish_Word_Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Finish_Word_Document.png new file mode 100644 index 0000000000..ee7902a017 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Finish_Word_Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_Document.png new file mode 100644 index 0000000000..cd27196a89 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Hosting_Word_Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Hosting_Word_Document.png new file mode 100644 index 0000000000..aacf4acae0 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Hosting_Word_Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Nuget-Package-Create-Word-Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Nuget-Package-Create-Word-Document.png new file mode 100644 index 0000000000..6ac2700c31 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Nuget-Package-Create-Word-Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Nuget-Package-Open-and-Save-Word-Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Nuget-Package-Open-and-Save-Word-Document.png new file mode 100644 index 0000000000..82f94e3c8c Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Nuget-Package-Open-and-Save-Word-Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Publish-Create-Word-Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Publish-Create-Word-Document.png new file mode 100644 index 0000000000..6f6736edbc Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Publish-Create-Word-Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Publish-Open-and-Save-Word-Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Publish-Open-and-Save-Word-Document.png new file mode 100644 index 0000000000..57358cf544 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Publish-Open-and-Save-Word-Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_Document.png new file mode 100644 index 0000000000..1db0017891 Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Target_Word_Document.png b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Target_Word_Document.png new file mode 100644 index 0000000000..0d5f43da1b Binary files /dev/null and b/Document-Processing/Word/Word-Library/NET/Azure-Images/Functions-Flex-Consumption/Target_Word_Document.png differ diff --git a/Document-Processing/Word/Word-Library/NET/Create-Word-Document-in-Azure-Functions-Flex-Consumption.md b/Document-Processing/Word/Word-Library/NET/Create-Word-Document-in-Azure-Functions-Flex-Consumption.md new file mode 100644 index 0000000000..f01809acb7 --- /dev/null +++ b/Document-Processing/Word/Word-Library/NET/Create-Word-Document-in-Azure-Functions-Flex-Consumption.md @@ -0,0 +1,364 @@ +--- +title: Create Word document in Azure Functions Flex Consumption | Syncfusion +description: Create Word document in Azure Functions Flex Consumption using .NET Core Word (DocIO) library without Microsoft Word or interop dependencies. +platform: document-processing +control: DocIO +documentation: UG +--- + +# Create Word document in Azure Functions (Flex Consumption) + +Syncfusion® DocIO is a [.NET Core Word library](https://www.syncfusion.com/document-processing/word-framework/net/word-library) used to create, read, edit, and convert Word documents programmatically without **Microsoft Word** or interop dependencies. Using this library, you can **Create Word document in Azure Functions deployed on Flex (Consumption) plan**. + +## Steps to Create Word document in Azure Functions (Flex Consumption) + +Step 1: Create a new Azure Functions project. +![Create a Azure Functions project](Azure-Images/Functions-Flex-Consumption/Azure_Word_Document.png) + +Step 2: Create a project name and select the location. +![Create a project name](Azure-Images/Functions-Flex-Consumption/Configuration-Create-Word-Document.png) + +Step 3: Select function worker as **.NET 8.0 (Long Term Support)** (isolated worker) and target Flex/Consumption hosting suitable for isolated worker. +![Select function worker](Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_Document.png) + +Step 4: Install the [Syncfusion.DocIO.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core) NuGet package as a reference to your project from [NuGet.org](https://www.nuget.org/). +![Install Syncfusion.DocIO.Net.Core NuGet package](Azure-Images/Functions-Flex-Consumption/Nuget-Package-Create-Word-Document.png) + +N> Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add "Syncfusion.Licensing" assembly reference and include a license key in your projects. Please refer to this [link](https://help.syncfusion.com/common/essential-studio/licensing/overview) to know about registering Syncfusion® license key in your application to use our components. + +Step 5: Include the following namespaces in the **Function1.cs** file. + +{% tabs %} + +{% highlight c# tabtitle="C#" %} +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; + +{% endhighlight %} + +{% endtabs %} + +Step 6: Add the following code snippet in **Run** method of **Function1** class to perform **Create Word document** in Azure Functions and return the resultant **Word document** to client end. + +{% tabs %} +{% highlight c# tabtitle="C#" %} + +public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req) + { + try + { + // Creating a new document. + WordDocument document = new WordDocument(); + //Adding a new section to the document. + WSection section = document.AddSection() as WSection; + //Set Margin of the section + section.PageSetup.Margins.All = 72; + //Set page size of the section + section.PageSetup.PageSize = new Syncfusion.Drawing.SizeF(612, 792); + + //Create Paragraph styles + WParagraphStyle style = document.AddParagraphStyle("Normal") as WParagraphStyle; + style.CharacterFormat.FontName = "Calibri"; + style.CharacterFormat.FontSize = 11f; + style.ParagraphFormat.BeforeSpacing = 0; + style.ParagraphFormat.AfterSpacing = 8; + style.ParagraphFormat.LineSpacing = 13.8f; + + style = document.AddParagraphStyle("Heading 1") as WParagraphStyle; + style.ApplyBaseStyle("Normal"); + style.CharacterFormat.FontName = "Calibri Light"; + style.CharacterFormat.FontSize = 16f; + style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(46, 116, 181); + style.ParagraphFormat.BeforeSpacing = 12; + style.ParagraphFormat.AfterSpacing = 0; + style.ParagraphFormat.Keep = true; + style.ParagraphFormat.KeepFollow = true; + style.ParagraphFormat.OutlineLevel = OutlineLevel.Level1; + + IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph(); + // Gets the image stream. + var assembly = Assembly.GetExecutingAssembly(); + var stream = assembly.GetManifestResourceStream("Create_Word_Document.Data.AdventureCycle.jpg"); + IWPicture picture = paragraph.AppendPicture(stream); + picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText; + picture.VerticalOrigin = VerticalOrigin.Margin; + picture.VerticalPosition = -45; + picture.HorizontalOrigin = HorizontalOrigin.Column; + picture.HorizontalPosition = 263.5f; + picture.WidthScale = 20; + picture.HeightScale = 15; + + paragraph.ApplyStyle("Normal"); + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; + WTextRange textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Calibri"; + textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red; + + //Appends paragraph. + paragraph = section.AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; + textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange; + textRange.CharacterFormat.FontSize = 18f; + textRange.CharacterFormat.FontName = "Calibri"; + + //Appends paragraph. + paragraph = section.AddParagraph(); + paragraph.ParagraphFormat.FirstLineIndent = 36; + paragraph.BreakCharacterFormat.FontSize = 12f; + textRange = paragraph.AppendText("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + + //Appends paragraph. + paragraph = section.AddParagraph(); + paragraph.ParagraphFormat.FirstLineIndent = 36; + paragraph.BreakCharacterFormat.FontSize = 12f; + textRange = paragraph.AppendText("In 2000, AdventureWorks Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the AdventureWorks Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + + paragraph = section.AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; + textRange = paragraph.AppendText("Product Overview") as WTextRange; + textRange.CharacterFormat.FontSize = 16f; + textRange.CharacterFormat.FontName = "Calibri"; + + //Appends table. + IWTable table = section.AddTable(); + table.ResetCells(3, 2); + table.TableFormat.Borders.BorderType = BorderStyle.None; + table.TableFormat.IsAutoResized = true; + //Appends paragraph. + paragraph = table[0, 0].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.BreakCharacterFormat.FontSize = 12f; + //Appends picture to the paragraph. + var assembly1 = Assembly.GetExecutingAssembly(); + using (var stream1 = assembly1.GetManifestResourceStream("Create_Word_Document.Data.Mountain-200.jpg")) + picture = paragraph.AppendPicture(stream1); + picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; + picture.VerticalOrigin = VerticalOrigin.Paragraph; + picture.VerticalPosition = 4.5f; + picture.HorizontalOrigin = HorizontalOrigin.Column; + picture.HorizontalPosition = -2.15f; + picture.WidthScale = 79; + picture.HeightScale = 79; + + //Appends paragraph. + paragraph = table[0, 1].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.AppendText("Mountain-200"); + //Appends paragraph. + paragraph = table[0, 1].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + paragraph.BreakCharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Product No: BK-M68B-38\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Size: 38\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Weight: 25\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Price: $2,294.99\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + //Appends paragraph. + paragraph = table[0, 1].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + + //Appends paragraph. + paragraph = table[1, 0].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.AppendText("Mountain-300 "); + //Appends paragraph. + paragraph = table[1, 0].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + paragraph.BreakCharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Product No: BK-M47B-38\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Size: 35\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Weight: 22\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Price: $1,079.99\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + //Appends paragraph. + paragraph = table[1, 0].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + + //Appends paragraph. + paragraph = table[1, 1].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.LineSpacing = 12f; + //Appends picture to the paragraph. + var assembly2 = Assembly.GetExecutingAssembly(); + using (var stream2 = assembly2.GetManifestResourceStream("Create_Word_Document.Data.Mountain-300.jpg")) + picture = paragraph.AppendPicture(stream2); + picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; + picture.VerticalOrigin = VerticalOrigin.Paragraph; + picture.VerticalPosition = 8.2f; + picture.HorizontalOrigin = HorizontalOrigin.Column; + picture.HorizontalPosition = -14.95f; + picture.WidthScale = 75; + picture.HeightScale = 75; + + //Appends paragraph. + paragraph = table[2, 0].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.LineSpacing = 12f; + //Appends picture to the paragraph. + var assembly3 = Assembly.GetExecutingAssembly(); + using (var stream3 = assembly3.GetManifestResourceStream("Create_Word_Document.Data.Road-550-W.jpg")) + picture = paragraph.AppendPicture(stream3); + picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; + picture.VerticalOrigin = VerticalOrigin.Paragraph; + picture.VerticalPosition = 3.75f; + picture.HorizontalOrigin = HorizontalOrigin.Column; + picture.HorizontalPosition = -5f; + picture.WidthScale = 92; + picture.HeightScale = 92; + + //Appends paragraph. + paragraph = table[2, 1].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.AppendText("Road-150 "); + //Appends paragraph. + paragraph = table[2, 1].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + paragraph.BreakCharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Product No: BK-R93R-44\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Size: 44\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Weight: 14\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Price: $3,578.27\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + //Appends paragraph. + section.AddParagraph(); + + MemoryStream memoryStream = new MemoryStream(); + //Saves the Word document file. + document.Save(memoryStream, FormatType.Docx); + memoryStream.Position = 0; + var bytes = memoryStream.ToArray(); + return new FileContentResult(bytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document") + { + FileDownloadName = "document.docx" + }; + } + catch (Exception ex) + { + // Log the error with details for troubleshooting + _logger.LogError(ex, "Error converting Word document to Image."); + // Prepare error message including exception details + var msg = $"Exception: {ex.Message}\n\n{ex}"; + // Return a 500 Internal Server Error response with the message + return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" }; + } + } + +{% endhighlight %} +{% endtabs %} + +Step 7: Right click the project and select **Publish**. Then, create a new profile in the Publish Window. +![Create a new profile in the Publish Window](Azure-Images/Functions-Flex-Consumption/Publish-Create-Word-Document.png) + +Step 8: Select the target as **Azure** and click **Next** button. +![Select the target as Azure](Azure-Images/Functions-Flex-Consumption/Target_Word_Document.png) + +Step 9: Select the specific target as **Azure Function App** and click **Next** button. +![Select the target as Azure](Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_Document.png) + +Step 10: Select the **Create new** button. +![Configure Hosting Plan](Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_Document.png) + +Step 11: Click **Create** button. +![Select the plan type](Azure-Images/Functions-Flex-Consumption/Hosting_Word_Document.png) + +Step 12: After creating app service then click **Finish** button. +![Creating app service](Azure-Images/Functions-Flex-Consumption/Finish_Word_Document.png) + +Step 13: Click the **Publish** button. +![Click Publish Button](Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_Document.png) + +Step 14: Publish has been succeed. +![Publish succeeded](Azure-Images/Functions-Flex-Consumption/After_Publish_Word_Document.png) + +Step 15: 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 **create a Word document** using the template Word document). You will get the output Word document as follows. + +![Create a Word document in Azure Functions Flex Consumption](ASP-NET-Core_images/GettingStartedOutput.jpg) + +## 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 Word document. + +{% tabs %} +{% highlight c# tabtitle="C#" %} +static async Task Main() + { + try + { + Console.Write("Please enter your Azure Function URL: "); + string url = Console.ReadLine(); + if (string.IsNullOrWhiteSpace(url)) return; + // Create a new HttpClient instance for sending HTTP requests + using var http = new HttpClient(); + using var content = new StringContent(string.Empty); + using var res = await http.PostAsync(url, content); + // Read the response body as a byte array + var resBytes = await res.Content.ReadAsByteArrayAsync(); + // Extract the media type from the response headers + string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty; + // Decide the output file path the response is an docx or txt + string outputPath = mediaType.Contains("word", StringComparison.OrdinalIgnoreCase) + || mediaType.Contains("officedocument", StringComparison.OrdinalIgnoreCase) + || mediaType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", StringComparison.OrdinalIgnoreCase) + ? Path.GetFullPath("../../../Output/Output.docx") + : Path.GetFullPath("../../../Output/function-error.txt"); + // Write the response bytes to the output file + await File.WriteAllBytesAsync(outputPath, resBytes); + Console.WriteLine($"Saved: {outputPath}"); + } + catch (Exception ex) + { + throw; + } + } +{% endhighlight %} +{% endtabs %} + +From GitHub, you can download the [console application](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption) and [Azure Functions Flex Consumption](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption). + +Click [here](https://www.syncfusion.com/document-processing/word-framework/net-core) to explore the rich set of Syncfusion® Word library (DocIO) features. + +An online sample link to [create a Word document](https://document.syncfusion.com/demos/word/helloworld#/tailwind) in ASP.NET Core. \ No newline at end of file diff --git a/Document-Processing/Word/Word-Library/NET/Open-and-save-Word-document-in-Azure-Functions-Flex-Consumption.md b/Document-Processing/Word/Word-Library/NET/Open-and-save-Word-document-in-Azure-Functions-Flex-Consumption.md new file mode 100644 index 0000000000..f1fea737cd --- /dev/null +++ b/Document-Processing/Word/Word-Library/NET/Open-and-save-Word-document-in-Azure-Functions-Flex-Consumption.md @@ -0,0 +1,172 @@ +--- +title: Open and save Word document in Azure Functions Flex Consumption | Syncfusion +description: Open and save Word document in Azure Functions Flex Consumption using .NET Core Word (DocIO) library without Microsoft Word or interop dependencies. +platform: document-processing +control: DocIO +documentation: UG +--- + +# Open and save Word document in Azure Functions (Flex Consumption) + +Syncfusion® DocIO is a [.NET Core Word library](https://www.syncfusion.com/document-processing/word-framework/net/word-library) used to create, read, edit, and convert Word documents programmatically without **Microsoft Word** or interop dependencies. Using this library, you can **Open and save Word document in Azure Functions deployed on Flex (Consumption) plan**. + +## Steps to Open and save Word document in Azure Functions (Flex Consumption) + +Step 1: Create a new Azure Functions project. +![Create a Azure Functions project](Azure-Images/Functions-Flex-Consumption/Azure_Word_Document.png) + +Step 2: Create a project name and select the location. +![Create a project name](Azure-Images/Functions-Flex-Consumption/Configuration-Open-and-Save-Word-Document.png) + +Step 3: Select function worker as **.NET 8.0 (Long Term Support)** (isolated worker) and target Flex/Consumption hosting suitable for isolated worker. +![Select function worker](Azure-Images/Functions-Flex-Consumption/Additional_Information_Word_Document.png) + +Step 4: Install the [Syncfusion.DocIO.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core) NuGet package as a reference to your project from [NuGet.org](https://www.nuget.org/). +![Install Syncfusion.DocIO.Net.Core NuGet package](Azure-Images/Functions-Flex-Consumption/Nuget-Package-Open-and-Save-Word-Document.png) + +N> Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add "Syncfusion.Licensing" assembly reference and include a license key in your projects. Please refer to this [link](https://help.syncfusion.com/common/essential-studio/licensing/overview) to know about registering Syncfusion® license key in your application to use our components. + +Step 5: Include the following namespaces in the **Function1.cs** file. + +{% tabs %} + +{% highlight c# tabtitle="C#" %} +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; + +{% endhighlight %} + +{% endtabs %} + +Step 6: Add the following code snippet in **Run** method of **Function1** class to perform **Open and save Word document** in Azure Functions and return the resultant **Word document** to client end. + +{% tabs %} +{% highlight c# tabtitle="C#" %} + +public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req) + { + try + { + // Create a memory stream to hold the incoming request body (Word document bytes) + await using MemoryStream inputStream = new MemoryStream(); + // Copy the request body into the memory stream + await req.Body.CopyToAsync(inputStream); + // Check if the stream is empty (no file content received) + if (inputStream.Length == 0) + return new BadRequestObjectResult("No file content received in request body."); + // Reset stream position to the beginning for reading + inputStream.Position = 0; + // Load the Word document from the stream (auto-detects format type) + using WordDocument document = new WordDocument(inputStream, Syncfusion.DocIO.FormatType.Automatic); + //Access the section in a Word document. + IWSection section = document.Sections[0]; + //Add a new paragraph to the section. + IWParagraph paragraph = section.AddParagraph(); + paragraph.ParagraphFormat.FirstLineIndent = 36; + paragraph.BreakCharacterFormat.FontSize = 12f; + IWTextRange text = paragraph.AppendText("In 2000, Adventure Works Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the Adventure Works Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group."); + text.CharacterFormat.FontSize = 12f; + MemoryStream memoryStream = new MemoryStream(); + //Saves the Word document file. + document.Save(memoryStream, FormatType.Docx); + memoryStream.Position = 0; + var bytes = memoryStream.ToArray(); + return new FileContentResult(bytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document") + { + FileDownloadName = "document.docx" + }; + } + catch (Exception ex) + { + // Log the error with details for troubleshooting + _logger.LogError(ex, "Error Open and Save document."); + // Prepare error message including exception details + var msg = $"Exception: {ex.Message}\n\n{ex}"; + // Return a 500 Internal Server Error response with the message + return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" }; + } + } + +{% endhighlight %} +{% endtabs %} + +Step 7: Right click the project and select **Publish**. Then, create a new profile in the Publish Window. +![Create a new profile in the Publish Window](Azure-Images/Functions-Flex-Consumption/Publish-Open-and-Save-Word-Document.png) + +Step 8: Select the target as **Azure** and click **Next** button. +![Select the target as Azure](Azure-Images/Functions-Flex-Consumption/Target_Word_Document.png) + +Step 9: Select the specific target as **Azure Function App** and click **Next** button. +![Select the target as Azure](Azure-Images/Functions-Flex-Consumption/Specific_Target_Word_Document.png) + +Step 10: Select the **Create new** button. +![Configure Hosting Plan](Azure-Images/Functions-Flex-Consumption/Function_Instance_Word_Document.png) + +Step 11: Click **Create** button. +![Select the plan type](Azure-Images/Functions-Flex-Consumption/Hosting_Word_Document.png) + +Step 12: After creating app service then click **Finish** button. +![Creating app service](Azure-Images/Functions-Flex-Consumption/Finish_Word_Document.png) + +Step 13: Click the **Publish** button. +![Click Publish Button](Azure-Images/Functions-Flex-Consumption/Before_Publish_Word_Document.png) + +Step 14: Publish has been succeed. +![Publish succeeded](Azure-Images/Functions-Flex-Consumption/After_Publish_Word_Document.png) + +Step 15: 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 **Open and save a Word document** using the template Word document). You will get the output Word document as follows. + +![Open and Save in Azure Functions v4](ASP-NET-Core_images/OpenAndSaveOutput.png) + +## 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 Word document. + +{% tabs %} +{% highlight c# tabtitle="C#" %} +static async Task Main() + { + try + { + Console.Write("Please enter your Azure Functions URL : "); + string url = Console.ReadLine(); + if (string.IsNullOrEmpty(url)) return; + // Create a new HttpClient instance for sending HTTP requests + using var http = new HttpClient(); + // Read all bytes from the input Word document + byte[] bytes = await File.ReadAllBytesAsync(@"Data/Input.docx"); + // Wrap the file bytes into a ByteArrayContent object for HTTP transmission + using var content = new ByteArrayContent(bytes); + // Set the content type header to indicate binary data + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); + // Send a POST request to the provided Azure Functions URL with the file content + using var res = await http.PostAsync(url, content); + // Read the response body as a byte array + var resBytes = await res.Content.ReadAsByteArrayAsync(); + // Extract the media type from the response headers + string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty; + // Decide the output file path the response is an image or txt + string outputPath = mediaType.Contains("word", StringComparison.OrdinalIgnoreCase) + || mediaType.Contains("officedocument", StringComparison.OrdinalIgnoreCase) + || mediaType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", StringComparison.OrdinalIgnoreCase) + ? Path.GetFullPath(@"../../../Output/Output.docx") + : Path.GetFullPath(@"../../../function-error.txt"); + // Write the response bytes to the output file + await File.WriteAllBytesAsync(outputPath, resBytes); + Console.WriteLine($"Saved: {outputPath}"); + } + catch (Exception ex) + { + throw; + } + } +{% endhighlight %} +{% endtabs %} + +From GitHub, you can download the [console application](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption) and [Azure Functions Flex Consumption](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption). + +Click [here](https://www.syncfusion.com/document-processing/word-framework/net-core) to explore the rich set of Syncfusion® Word library (DocIO) features. + +An online sample link to [create a Word document](https://document.syncfusion.com/demos/word/helloworld#/tailwind) in ASP.NET Core. \ No newline at end of file diff --git a/Document-Processing/Word/Word-Library/NET/Performance-metrics.md b/Document-Processing/Word/Word-Library/NET/Performance-metrics.md index f3e232a93a..a8d6122110 100644 --- a/Document-Processing/Word/Word-Library/NET/Performance-metrics.md +++ b/Document-Processing/Word/Word-Library/NET/Performance-metrics.md @@ -18,7 +18,7 @@ The following system configurations were used for benchmarking: * **Processor:** AMD Ryzen 5 7520U with Radeon Graphics * **RAM:** 16GB * **.NET Version:** .NET 8.0 -* **Syncfusion® Version:** [Syncfusion.DocIO.Net.Core v32.2.3](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core/32.2.3) +* **Syncfusion® Version:** [Syncfusion.DocIO.Net.Core v33.1.44](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core/33.1.44) ## Benchmark Results @@ -34,25 +34,25 @@ The table below shows the performance results of various Word document operation {{'[DOCX to DOCX](https://help.syncfusion.com/document-processing/word/word-library/net/loading-and-saving-document)'| markdownify }} 100 pages - 1.68 + 1.5 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Open-and-save/.NET/Open-and-Save-Word-document)'| markdownify }} {{'[RTF to RTF](https://help.syncfusion.com/document-processing/word/word-library/net/rtf)'| markdownify }} 100 pages - 5.53 + 4.3 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Open-and-save/.NET/Open-and-Save-RTF-document)'| markdownify }} {{'[HTML to HTML](https://help.syncfusion.com/document-processing/word/word-library/net/html)'| markdownify }} 100 pages - 7.7 + 6.5 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Open-and-save/.NET/Open-and-Save-HTML-document)'| markdownify }} {{'[Clone and merge](https://help.syncfusion.com/document-processing/word/word-library/net/working-with-word-document#cloning-a-word-document)'| markdownify }} 100 pages - 3.85 + 0.8 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Clone-and-merge/)'| markdownify }} @@ -64,13 +64,13 @@ The table below shows the performance results of various Word document operation {{'[Mail merge](https://help.syncfusion.com/document-processing/word/word-library/net/working-with-mail-merge)'| markdownify }} 1000 records - 1.72 + 1.5 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Mail-Merge/)'| markdownify }} {{'[Word Compare](https://help.syncfusion.com/document-processing/word/word-library/net/word-document/compare-word-documents)'| markdownify }} 100 pages - 3.52 + 3.84 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Word-Compare/)'| markdownify }} @@ -82,19 +82,19 @@ The table below shows the performance results of various Word document operation {{'[Reject All](https://help.syncfusion.com/document-processing/word/word-library/net/accepting-or-rejecting-track-changes#reject-all-changes)'| markdownify }} 100 revisions - 0.009 + 0.008 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Reject-All/)'| markdownify }} {{'[Update TOC](https://help.syncfusion.com/document-processing/word/word-library/net/working-with-table-of-contents)'| markdownify }} 100 pages - 4.53 + 4.3 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/Update-TOC/)'| markdownify }} {{'[Update Document Fields](https://help.syncfusion.com/document-processing/word/word-library/net/working-with-fields)'| markdownify }} 100 pages - 0.18 + 0.14 {{'[GitHub-Example](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Performance-metrices/UpdateDocumentFields/)'| markdownify }}