Skip to content

Commit ae43eb7

Browse files
committed
Add Open and Save Web API Sample
1 parent 8596b31 commit ae43eb7

File tree

15 files changed

+302
-0
lines changed

15 files changed

+302
-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", "{BADAD8BB-DD47-4F82-8E74-302D7CD76E61}"
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+
{BADAD8BB-DD47-4F82-8E74-302D7CD76E61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{BADAD8BB-DD47-4F82-8E74-302D7CD76E61}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{BADAD8BB-DD47-4F82-8E74-302D7CD76E61}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{BADAD8BB-DD47-4F82-8E74-302D7CD76E61}.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 = {11406F87-9ABB-4E04-86C0-5DD6338AC9F0}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
<ItemGroup>
12+
<Folder Include="Output\" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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:7125/api/Values/api/Word");
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.docx");
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+
34+
}
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}") = "Open_and_save_Word_document", "Open_and_save_Word_document\Open_and_save_Word_document.csproj", "{F85A0851-CA1C-418D-BF88-97A9B31CED41}"
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+
{F85A0851-CA1C-418D-BF88-97A9B31CED41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F85A0851-CA1C-418D-BF88-97A9B31CED41}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F85A0851-CA1C-418D-BF88-97A9B31CED41}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F85A0851-CA1C-418D-BF88-97A9B31CED41}.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 = {11B82ED7-A31F-495F-B18A-4BC1E92A7574}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Syncfusion.DocIO;
4+
using Syncfusion.DocIO.DLS;
5+
using System.Reflection.Metadata;
6+
7+
namespace Open_and_save_Word_document.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
public class ValuesController : ControllerBase
12+
{
13+
[HttpGet]
14+
[Route("api/Word")]
15+
public IActionResult DownloadWordDocument()
16+
{
17+
try
18+
{
19+
var fileDownloadName = "Output.docx";
20+
const string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
21+
var stream = OpenandSaveDocument();
22+
stream.Position = 0;
23+
return File(stream, contentType, fileDownloadName);
24+
}
25+
catch (Exception ex)
26+
{
27+
// Log or handle the exception
28+
return BadRequest("Error occurred while creating Word file: " + ex.Message);
29+
}
30+
}
31+
public static MemoryStream OpenandSaveDocument()
32+
{
33+
//Open an existing Word document.
34+
WordDocument document = new WordDocument(Path.GetFullPath("Data/Input.docx"));
35+
//Access the section in a Word document.
36+
IWSection section = document.Sections[0];
37+
//Add a new paragraph to the section.
38+
IWParagraph paragraph = section.AddParagraph();
39+
paragraph.ParagraphFormat.FirstLineIndent = 36;
40+
paragraph.BreakCharacterFormat.FontSize = 12f;
41+
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.");
42+
text.CharacterFormat.FontSize = 12f;
43+
44+
//Saving the Word document to the MemoryStream
45+
MemoryStream stream = new MemoryStream();
46+
document.Save(stream, FormatType.Docx);
47+
document.Close();
48+
//Set the position as '0'.
49+
stream.Position = 0;
50+
return stream;
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace Open_and_save_Word_document.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,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
11+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@Open_and_save_Word_document_HostAddress = http://localhost:5239
2+
3+
GET {{Open_and_save_Word_document_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###

0 commit comments

Comments
 (0)