Skip to content

Commit 9c70e01

Browse files
committed
Added the Word to image sample
1 parent 98a2c99 commit 9c70e01

File tree

15 files changed

+290
-0
lines changed

15 files changed

+290
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36518.9 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client-Application", "Client-Application\Client-Application.csproj", "{ECB9DA0B-5045-48CD-BD48-59D43107A210}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{ECB9DA0B-5045-48CD-BD48-59D43107A210}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{ECB9DA0B-5045-48CD-BD48-59D43107A210}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{ECB9DA0B-5045-48CD-BD48-59D43107A210}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{ECB9DA0B-5045-48CD-BD48-59D43107A210}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {95D2CDFC-5BF6-42E8-9C97-53AF84B04938}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Client_Application</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Program
2+
{
3+
static async Task Main(string[] args)
4+
{
5+
// Create an HttpClient instance
6+
using (HttpClient client = new HttpClient())
7+
{
8+
try
9+
{
10+
// Send a GET request to a URL
11+
HttpResponseMessage response = await client.GetAsync("https://localhost:7112/api/Values/api/ConvertWordToImage");
12+
13+
// Check if the response is successful
14+
if (response.IsSuccessStatusCode)
15+
{
16+
// Read the content as a string
17+
Stream responseBody = await response.Content.ReadAsStreamAsync();
18+
FileStream fileStream = File.Create("../../../Output/Output.jpeg");
19+
responseBody.CopyTo(fileStream);
20+
fileStream.Close();
21+
}
22+
else
23+
{
24+
Console.WriteLine("HTTP error status code: " + response.StatusCode);
25+
}
26+
}
27+
catch (HttpRequestException e)
28+
{
29+
Console.WriteLine("Request exception: " + e.Message);
30+
}
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36518.9 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-Word-Document-to-Image", "Convert-Word-Document-to-Image\Convert-Word-Document-to-Image.csproj", "{9AF519F5-E49C-41C4-8897-19C28E9FD735}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{9AF519F5-E49C-41C4-8897-19C28E9FD735}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9AF519F5-E49C-41C4-8897-19C28E9FD735}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9AF519F5-E49C-41C4-8897-19C28E9FD735}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9AF519F5-E49C-41C4-8897-19C28E9FD735}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {4893225B-0DC6-41EB-B2B1-A22EBC1E0BE2}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Syncfusion.DocIO;
4+
using Syncfusion.DocIO.DLS;
5+
using Syncfusion.DocIORenderer;
6+
7+
namespace Convert_Word_Document_to_Image.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
public class ValuesController : ControllerBase
12+
{
13+
[HttpGet]
14+
[Route("api/ConvertWordToImage")]
15+
public IActionResult ConvertWordToImage()
16+
{
17+
try
18+
{
19+
var fileDownloadName = "Output.jpeg";
20+
const string contentType = "image/jpeg";
21+
var stream = ConvertWordDocumentToImage();
22+
stream.Position = 0;
23+
return File(stream, contentType, fileDownloadName);
24+
}
25+
catch (Exception ex)
26+
{
27+
return BadRequest("Error occurred while converting Word to Image: " + ex.Message);
28+
}
29+
}
30+
public static Stream ConvertWordDocumentToImage()
31+
{
32+
//Loads the input Word document
33+
WordDocument wordDocument = new WordDocument(Path.GetFullPath("Data/Input.docx"), FormatType.Docx);
34+
DocIORenderer render = new DocIORenderer();
35+
//Convert the first page of the Word document into an image.
36+
Stream imageStream = wordDocument.RenderAsImages(0, ExportImageFormat.Jpeg);
37+
//close the word document.
38+
wordDocument.Close();
39+
//Reset the stream position.
40+
imageStream.Position = 0;
41+
//Save the image file.
42+
return imageStream;
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace Convert_Word_Document_to_Image.Controllers
4+
{
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class WeatherForecastController : ControllerBase
8+
{
9+
private static readonly string[] Summaries = new[]
10+
{
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
};
13+
14+
private readonly ILogger<WeatherForecastController> _logger;
15+
16+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
[HttpGet(Name = "GetWeatherForecast")]
22+
public IEnumerable<WeatherForecast> Get()
23+
{
24+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
25+
{
26+
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
27+
TemperatureC = Random.Shared.Next(-20, 55),
28+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
29+
})
30+
.ToArray();
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RootNamespace>Convert_Word_Document_to_Image</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@Convert_Word_Document_to_Image_HostAddress = http://localhost:5217
2+
3+
GET {{Convert_Word_Document_to_Image_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###

0 commit comments

Comments
 (0)