Skip to content

Commit b9d6dbe

Browse files
Merge pull request #1606 from syncfusion-content/973380-Prepare-WebAPI-Page-Hotfix
973380 - Added Web-API page in DocIO UG
2 parents f31c461 + 74100aa commit b9d6dbe

21 files changed

+769
-0
lines changed

Document-Processing-toc.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,6 +3549,9 @@
35493549
<li>
35503550
<a href="/document-processing/word/word-library/net/Create-Word-document-in-Console-application">Console</a>
35513551
</li>
3552+
<li>
3553+
<a href="/document-processing/word/word-library/net/Create-Word-Document-in-ASP-NET-Core-WEB-API">ASP .NET Core Web API</a>
3554+
</li>
35523555
<li>
35533556
<a href="/document-processing/word/word-library/net/Create-Word-Document-in-Azure">Azure</a>
35543557
<ul>
@@ -3680,6 +3683,9 @@
36803683
<li>
36813684
<a href="/document-processing/word/word-library/net/Open-and-save-Word-document-in-Console-application">Console</a>
36823685
</li>
3686+
<li>
3687+
<a href="/document-processing/word/word-library/net/Open-and-Save-Word-Document-in-ASP-NET-Core-WEB-API">ASP .NET Core Web API</a>
3688+
</li>
36833689
<li>
36843690
<a href="/document-processing/word/word-library/net/Open-and-save-Word-document-in-Azure">Azure</a>
36853691
<ul>
@@ -3937,6 +3943,9 @@
39373943
<li>
39383944
<a href="/document-processing/word/conversions/word-to-pdf/net/Convert-Word-document-to-PDF-in-Console-application">Console</a>
39393945
</li>
3946+
<li>
3947+
<a href="/document-processing/word/conversions/word-to-pdf/net/Convert-Word-Document-to-PDF-in-ASP-NET-Core-WEB-API">ASP.NET Core Web API</a>
3948+
</li>
39403949
<li>
39413950
<a href="/document-processing/word/conversions/word-to-pdf/net/Convert-Word-Document-to-PDF-in-Azure">Azure</a>
39423951
<ul>
@@ -4020,6 +4029,9 @@
40204029
<li>
40214030
<a href="/document-processing/word/conversions/word-to-image/net/Convert-Word-document-to-Image-in-Console-application">Console</a>
40224031
</li>
4032+
<li>
4033+
<a href="/document-processing/word/conversions/word-to-image/net/Convert-Word-Document-to-Image-in-ASP-NET-Core-WEB-API">ASP .NET Core Web API</a>
4034+
</li>
40234035
<li>
40244036
<a href="/document-processing/word/conversions/word-to-image/net/Convert-Word-Document-to-Image-Azure">Azure</a>
40254037
<ul>
@@ -4177,6 +4189,9 @@
41774189
<li>
41784190
<a href="/document-processing/word/conversions/word-to-pdf/net/Convert-Word-document-to-PDF-in-Console-application">Console</a>
41794191
</li>
4192+
<li>
4193+
<a href="/document-processing/word/conversions/word-to-pdf/net/Convert-Word-Document-to-PDF-in-ASP-NET-Core-WEB-API">ASP.NET Core Web API</a>
4194+
</li>
41804195
<li>
41814196
<a href="/document-processing/word/conversions/word-to-pdf/net/Convert-Word-Document-to-PDF-in-Azure">Azure</a>
41824197
<ul>
@@ -4298,6 +4313,9 @@
42984313
<li>
42994314
<a href="/document-processing/word/conversions/word-to-image/net/Convert-Word-document-to-Image-in-Console-application">Console</a>
43004315
</li>
4316+
<li>
4317+
<a href="/document-processing/word/conversions/word-to-image/net/Convert-Word-Document-to-Image-in-ASP-NET-Core-WEB-API">ASP.NET Core Web API</a>
4318+
</li>
43014319
<li>
43024320
<a href="/document-processing/word/conversions/word-to-image/net/Convert-Word-Document-to-Image-Azure">Azure</a>
43034321
<ul>
70.1 KB
Loading
33.4 KB
Loading
67.1 KB
Loading
54 KB
Loading
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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>&reg;</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+
![Create ASP.NET Core Web API project in Visual Studio](ASP-NET-Core-WEB-API-images/ASP-NET-Core-Web-API-template.png)
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+
![Install Syncfusion.DocIORenderer.Net.Core NuGet Package](ASP-NET-Core-WEB-API-images/Word-to-Image-Nuget-package.png)
24+
25+
N> Starting with v16.2.0.x, if you reference Syncfusion<sup>&reg;</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>&reg;</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+
![Add empty API controller to the project](ASP-NET-Core-WEB-API-images/Word-to-Image-new-controller.png)
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+
![Create a Console application in Visual Studio](ASP-NET-Core-WEB-API-images/Console-Template-Net-Core.png)
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+
![ASP .NET Core WEB API output Word document](WordToPDF_images/Output-WordtoImage.png)
169+
170+
Click [here](https://www.syncfusion.com/document-processing/word-framework/net) to explore the rich set of Syncfusion<sup>&reg;</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.
44.8 KB
Loading
70.1 KB
Loading
33.4 KB
Loading
66.9 KB
Loading

0 commit comments

Comments
 (0)