Skip to content

Commit 07a6ef3

Browse files
committed
Added Web API Page in DocIO
1 parent c986553 commit 07a6ef3

16 files changed

+530
-2
lines changed
70.1 KB
Loading
33.4 KB
Loading
67.1 KB
Loading
54 KB
Loading
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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> 1. If you're deploying the application in a Linux environment, refer to the [documentation](https://help.syncfusion.com/document-processing/word/conversions/word-to-image/net/nuget-packages-required-word-to-image#additional-nuget-packages-required-for-linux) for the required additional NuGet packages.
26+
N> 2. 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.
27+
28+
Step 3: Add a new API controller empty file in the project.
29+
30+
![Add empty API controller to the project](ASP-NET-Core-WEB-API-images/Word-to-Image-new-controller.png)
31+
32+
Step 4: Include the following namespaces in the **ValuesController.cs** file.
33+
34+
{% tabs %}
35+
36+
{% highlight c# tabtitle="C#" %}
37+
38+
using Microsoft.AspNetCore.Mvc;
39+
using Syncfusion.DocIO;
40+
using Syncfusion.DocIO.DLS;
41+
using Syncfusion.DocIORenderer;
42+
43+
{% endhighlight %}
44+
45+
{% endtabs %}
46+
47+
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.
48+
49+
{% tabs %}
50+
51+
{% highlight c# tabtitle="C#" %}
52+
53+
[HttpGet]
54+
[Route("api/ConvertWordToImage")]
55+
public IActionResult ConvertWordToImage()
56+
{
57+
try
58+
{
59+
var fileDownloadName = "Output.jpeg";
60+
const string contentType = "image/jpeg";
61+
var stream = ConvertWordDocumentToImage();
62+
stream.Position = 0;
63+
return File(stream, contentType, fileDownloadName);
64+
}
65+
catch (Exception ex)
66+
{
67+
return BadRequest("Error occurred while converting Word to Image: " + ex.Message);
68+
}
69+
}
70+
71+
{% endhighlight %}
72+
73+
{% endtabs %}
74+
75+
Step 6: Implement the `ConvertWordDocumentToImage` method in `ValuesController.cs`.
76+
77+
{% tabs %}
78+
79+
{% highlight c# tabtitle="C#" %}
80+
81+
public static Stream ConvertWordDocumentToImage()
82+
{
83+
// Loads the input Word document
84+
WordDocument wordDocument = new WordDocument(Path.GetFullPath("Data/Input.docx"), FormatType.Docx);
85+
DocIORenderer render = new DocIORenderer();
86+
// Convert the first page of the Word document into an image.
87+
Stream imageStream = wordDocument.RenderAsImages(0, ExportImageFormat.Jpeg);
88+
// Close the Word document.
89+
wordDocument.Close();
90+
// Reset the stream position.
91+
imageStream.Position = 0;
92+
// Save the image file.
93+
return imageStream;
94+
}
95+
96+
{% endhighlight %}
97+
98+
{% endtabs %}
99+
100+
Step 7: Build the project.
101+
102+
Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project.
103+
104+
Step 8: Run the project.
105+
106+
Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app.
107+
108+
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).
109+
110+
## Steps for accessing the Web API using HTTP requests
111+
112+
Step 1: Create a console application.
113+
![Create a Console application in Visual Studio](ASP-NET-Core-WEB-API-images/Console-Template-Net-Core.png)
114+
115+
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).
116+
117+
Step 2: Add the below code snippet in the **Program.cs** file for accessing the Web API using HTTP requests.
118+
119+
This method sends a GET request to the Web API endpoint to retrieve and save the generated image.
120+
121+
{% tabs %}
122+
123+
{% highlight c# tabtitle="C#" %}
124+
125+
// Create an HttpClient instance
126+
using (HttpClient client = new HttpClient())
127+
{
128+
try
129+
{
130+
// Send a GET request to a URL
131+
HttpResponseMessage response = await client.GetAsync("https://localhost:7112/api/Values/api/ConvertWordToImage");
132+
133+
// Check if the response is successful
134+
if (response.IsSuccessStatusCode)
135+
{
136+
// Read the content as a string
137+
Stream responseBody = await response.Content.ReadAsStreamAsync();
138+
FileStream fileStream = File.Create("../../../Output/Output.jpeg");
139+
responseBody.CopyTo(fileStream);
140+
fileStream.Close();
141+
}
142+
else
143+
{
144+
Console.WriteLine("HTTP error status code: " + response.StatusCode);
145+
}
146+
}
147+
catch (HttpRequestException e)
148+
{
149+
Console.WriteLine("Request exception: " + e.Message);
150+
}
151+
}
152+
153+
{% endhighlight %}
154+
155+
{% endtabs %}
156+
157+
Step 3: Build the project.
158+
159+
Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project.
160+
161+
Step 4: Run the project.
162+
163+
Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app.
164+
165+
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).
166+
167+
Upon executing the program, the **image** will be generated as follows.
168+
169+
![ASP .NET Core WEB API output Word document](WordToPDF_images/Output-WordtoImage.png)
170+
171+
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.
172+
173+
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
66.9 KB
Loading
55.6 KB
Loading
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
title: Convert Word to PDF in ASP.NET Core Web API | Syncfusion
3+
description: Convert Word to PDF 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 PDF 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 PDF in ASP.NET Core Web API**.
12+
13+
## Steps to convert Word document to PDF in programmatically:
14+
15+
The below steps illustrate convert a simple Word document to PDF 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-PDF-Nuget-Package.png)
24+
25+
N> 1. If you're deploying the application in a Linux environment, refer to the [documentation](https://help.syncfusion.com/document-processing/word/conversions/word-to-image/net/nuget-packages-required-word-to-image#additional-nuget-packages-required-for-linux) for the required additional NuGet packages.
26+
N> 2. 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.
27+
28+
Step 3: Add a new API controller empty file in the project.
29+
30+
![Add empty API controller to the project](ASP-NET-Core-WEB-API-images/Word-to-PDF-new-controller.png)
31+
32+
Step 4: Include the following namespaces in the **ValuesController.cs** file.
33+
34+
{% tabs %}
35+
36+
{% highlight c# tabtitle="C#" %}
37+
38+
using Microsoft.AspNetCore.Mvc;
39+
using Syncfusion.DocIO;
40+
using Syncfusion.DocIO.DLS;
41+
using Syncfusion.DocIORenderer;
42+
using Syncfusion.Pdf;
43+
44+
{% endhighlight %}
45+
46+
{% endtabs %}
47+
48+
Step 5: Add a new action method ConvertWordToPdf in **ValuesController.cs** and include the below code snippet to Convert Word document to PDF and download it.
49+
50+
{% tabs %}
51+
52+
{% highlight c# tabtitle="C#" %}
53+
54+
[HttpGet]
55+
[Route("api/ConvertWordToPdf")]
56+
public IActionResult ConvertWordToPdf()
57+
{
58+
try
59+
{
60+
var fileDownloadName = "Output.pdf";
61+
const string contentType = "application/pdf";
62+
var stream = ConvertWordDocumentToPdf();
63+
stream.Position = 0;
64+
return File(stream, contentType, fileDownloadName);
65+
}
66+
catch (Exception ex)
67+
{
68+
return BadRequest("Error occurred while converting Word to PDF: " + ex.Message);
69+
}
70+
}
71+
72+
{% endhighlight %}
73+
74+
{% endtabs %}
75+
76+
Step 6: Implement the `ConvertWordDocumentToPdf` method in `ValuesController.cs`.
77+
78+
{% tabs %}
79+
80+
{% highlight c# tabtitle="C#" %}
81+
82+
public static MemoryStream ConvertWordDocumentToPdf()
83+
{
84+
//Open the existing PowerPoint presentation with loaded stream.
85+
using (WordDocument wordDocument = new WordDocument(Path.GetFullPath("Data/Template.docx")))
86+
{
87+
using (DocIORenderer render = new DocIORenderer())
88+
{
89+
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
90+
//Create the MemoryStream to save the converted PDF.
91+
MemoryStream pdfStream = new MemoryStream();
92+
//Save the converted PDF document to MemoryStream.
93+
pdfDocument.Save(pdfStream);
94+
pdfStream.Position = 0;
95+
//Download PDF document in the browser.
96+
return pdfStream;
97+
}
98+
}
99+
}
100+
101+
{% endhighlight %}
102+
103+
{% endtabs %}
104+
105+
Step 7: Build the project.
106+
107+
Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project.
108+
109+
Step 8: Run the project.
110+
111+
Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app.
112+
113+
A complete working sample is available on [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/ASP.NET-Core-Web-API/Convert-Word-Document-to-PDF).
114+
115+
## Steps for accessing the Web API using HTTP requests
116+
117+
Step 1: Create a console application.
118+
![Create a Console application in Visual Studio](ASP-NET-Core-WEB-API-images/Console-Template-Net-Core.png)
119+
120+
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).
121+
122+
Step 2: Add the below code snippet in the **Program.cs** file for accessing the Web API using HTTP requests.
123+
124+
This method sends a GET request to the Web API endpoint to retrieve and save the generated PDF document.
125+
126+
{% tabs %}
127+
128+
{% highlight c# tabtitle="C#" %}
129+
130+
// Create an HttpClient instance
131+
using (HttpClient client = new HttpClient())
132+
{
133+
try
134+
{
135+
// Send a GET request to a URL
136+
HttpResponseMessage response = await client.GetAsync("https://localhost:7240/api/Values/api/ConvertWordToPdf");
137+
138+
// Check if the response is successful
139+
if (response.IsSuccessStatusCode)
140+
{
141+
// Read the content as a string
142+
Stream responseBody = await response.Content.ReadAsStreamAsync();
143+
FileStream fileStream = File.Create("../../../Output/Output.pdf");
144+
responseBody.CopyTo(fileStream);
145+
fileStream.Close();
146+
}
147+
else
148+
{
149+
Console.WriteLine("HTTP error status code: " + response.StatusCode);
150+
}
151+
}
152+
catch (HttpRequestException e)
153+
{
154+
Console.WriteLine("Request exception: " + e.Message);
155+
}
156+
}
157+
158+
{% endhighlight %}
159+
160+
{% endtabs %}
161+
162+
Step 3: Build the project.
163+
164+
Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project.
165+
166+
Step 4: Run the project.
167+
168+
Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app.
169+
170+
A complete working sample is available on [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/ASP.NET-Core-Web-API/Client-Application).
171+
172+
Upon executing the program, the **PDF** will be generated as follows.
173+
174+
![ASP .NET Core WEB API output Word document](ASP-NET-Core-WEB-API-images/ASP-NET-Core-Web-API-Output.png)
175+
176+
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.
177+
178+
An online sample link to [convert Word document to PDF](https://document.syncfusion.com/demos/word/wordtopdf#/tailwind) in ASP.NET Core.
-6.82 KB
Loading

0 commit comments

Comments
 (0)