|
| 1 | +--- |
| 2 | +title: Convert Word to Image in ASP.NET Core Web API | Syncfusion |
| 3 | +description: Convert Word to image in ASP.NET Core Web API using .NET Core Word (DocIO) library without Microsoft Word or interop dependencies. |
| 4 | +platform: document-processing |
| 5 | +control: DocIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# Convert Word document to Image in ASP.NET Core Web API |
| 10 | + |
| 11 | +Syncfusion<sup>®</sup> DocIO is a [.NET Core Word library](https://www.syncfusion.com/document-processing/word-framework/net-core/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 ASP.NET Core Web API**. |
| 12 | + |
| 13 | +## Steps to convert Word document to Image in programmatically: |
| 14 | + |
| 15 | +The below steps illustrate convert a simple Word document to Image in ASP.NET Core Web API. |
| 16 | + |
| 17 | +Step 1: Create a new C# ASP.NET Core Web API project. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Step 2: Install the [Syncfusion.DocIORenderer.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIORenderer.Net.Core) NuGet package as a reference to your project from [NuGet.org](https://www.nuget.org). |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +N> Starting with v16.2.0.x, if you reference Syncfusion<sup>®</sup> 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<sup>®</sup> license key in your application to use our components. |
| 26 | + |
| 27 | +Step 3: Add a new API controller empty file in the project. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +Step 4: Include the following namespaces in the **ValuesController.cs** file. |
| 32 | + |
| 33 | +{% tabs %} |
| 34 | + |
| 35 | +{% highlight c# tabtitle="C#" %} |
| 36 | + |
| 37 | +using Microsoft.AspNetCore.Mvc; |
| 38 | +using Syncfusion.DocIO; |
| 39 | +using Syncfusion.DocIO.DLS; |
| 40 | +using Syncfusion.DocIORenderer; |
| 41 | + |
| 42 | +{% endhighlight %} |
| 43 | + |
| 44 | +{% endtabs %} |
| 45 | + |
| 46 | +Step 5: Add a new action method ConvertWordToImage in **ValuesController.cs** and include the below code snippet to Convert Word document to image and download it. |
| 47 | + |
| 48 | +{% tabs %} |
| 49 | + |
| 50 | +{% highlight c# tabtitle="C#" %} |
| 51 | + |
| 52 | +[HttpGet] |
| 53 | +[Route("api/ConvertWordToImage")] |
| 54 | +public IActionResult ConvertWordToImage() |
| 55 | +{ |
| 56 | + try |
| 57 | + { |
| 58 | + var fileDownloadName = "Output.jpeg"; |
| 59 | + const string contentType = "image/jpeg"; |
| 60 | + var stream = ConvertWordDocumentToImage(); |
| 61 | + stream.Position = 0; |
| 62 | + return File(stream, contentType, fileDownloadName); |
| 63 | + } |
| 64 | + catch (Exception ex) |
| 65 | + { |
| 66 | + return BadRequest("Error occurred while converting Word to Image: " + ex.Message); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +{% endhighlight %} |
| 71 | + |
| 72 | +{% endtabs %} |
| 73 | + |
| 74 | +Step 6: Implement the `ConvertWordDocumentToImage` method in `ValuesController.cs`. |
| 75 | + |
| 76 | +{% tabs %} |
| 77 | + |
| 78 | +{% highlight c# tabtitle="C#" %} |
| 79 | + |
| 80 | +public static Stream ConvertWordDocumentToImage() |
| 81 | +{ |
| 82 | + // Loads the input Word document |
| 83 | + WordDocument wordDocument = new WordDocument(Path.GetFullPath("Data/Input.docx"), FormatType.Docx); |
| 84 | + DocIORenderer render = new DocIORenderer(); |
| 85 | + // Convert the first page of the Word document into an image. |
| 86 | + Stream imageStream = wordDocument.RenderAsImages(0, ExportImageFormat.Jpeg); |
| 87 | + // Close the Word document. |
| 88 | + wordDocument.Close(); |
| 89 | + // Reset the stream position. |
| 90 | + imageStream.Position = 0; |
| 91 | + // Save the image file. |
| 92 | + return imageStream; |
| 93 | +} |
| 94 | + |
| 95 | +{% endhighlight %} |
| 96 | + |
| 97 | +{% endtabs %} |
| 98 | + |
| 99 | +Step 7: Build the project. |
| 100 | + |
| 101 | +Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project. |
| 102 | + |
| 103 | +Step 8: Run the project. |
| 104 | + |
| 105 | +Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app. |
| 106 | + |
| 107 | +A complete working sample is available on [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-Image-conversion/Convert-Word-to-image/ASP.NET-Core-Web-API/Convert-Word-Document-to-Image). |
| 108 | + |
| 109 | +## Steps for accessing the Web API using HTTP requests |
| 110 | + |
| 111 | +Step 1: Create a console application. |
| 112 | + |
| 113 | + |
| 114 | +N> Ensure your ASP.NET Core Web API is running on the specified port before running this client. Adjust the port number if your Web API runs on a different port (check the ASP.NET Core app's launch settings). |
| 115 | + |
| 116 | +Step 2: Add the below code snippet in the **Program.cs** file for accessing the Web API using HTTP requests. |
| 117 | + |
| 118 | +This method sends a GET request to the Web API endpoint to retrieve and save the generated image. |
| 119 | + |
| 120 | +{% tabs %} |
| 121 | + |
| 122 | +{% highlight c# tabtitle="C#" %} |
| 123 | + |
| 124 | +// Create an HttpClient instance |
| 125 | +using (HttpClient client = new HttpClient()) |
| 126 | +{ |
| 127 | + try |
| 128 | + { |
| 129 | + // Send a GET request to a URL |
| 130 | + HttpResponseMessage response = await client.GetAsync("https://localhost:7112/api/Values/api/ConvertWordToImage"); |
| 131 | + |
| 132 | + // Check if the response is successful |
| 133 | + if (response.IsSuccessStatusCode) |
| 134 | + { |
| 135 | + // Read the content as a string |
| 136 | + Stream responseBody = await response.Content.ReadAsStreamAsync(); |
| 137 | + FileStream fileStream = File.Create("../../../Output/Output.jpeg"); |
| 138 | + responseBody.CopyTo(fileStream); |
| 139 | + fileStream.Close(); |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + Console.WriteLine("HTTP error status code: " + response.StatusCode); |
| 144 | + } |
| 145 | + } |
| 146 | + catch (HttpRequestException e) |
| 147 | + { |
| 148 | + Console.WriteLine("Request exception: " + e.Message); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +{% endhighlight %} |
| 153 | + |
| 154 | +{% endtabs %} |
| 155 | + |
| 156 | +Step 3: Build the project. |
| 157 | + |
| 158 | +Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project. |
| 159 | + |
| 160 | +Step 4: Run the project. |
| 161 | + |
| 162 | +Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app. |
| 163 | + |
| 164 | +A complete working sample is available on [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-Image-conversion/Convert-Word-to-image/ASP.NET-Core-Web-API/Client-Application). |
| 165 | + |
| 166 | +Upon executing the program, the **image** will be generated as follows. |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +Click [here](https://www.syncfusion.com/document-processing/word-framework/net) to explore the rich set of Syncfusion<sup>®</sup> Word library (DocIO) features. |
| 171 | + |
| 172 | +An online sample link to [convert Word document to image](https://document.syncfusion.com/demos/word/wordtoimage#/tailwind) in ASP.NET Core. |
0 commit comments