Skip to content

Commit 98a2c99

Browse files
committed
Added Word to PDF sample
1 parent ae43eb7 commit 98a2c99

File tree

15 files changed

+294
-0
lines changed

15 files changed

+294
-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", "{24433651-BB7C-44F5-B681-9470139C9B8F}"
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+
{24433651-BB7C-44F5-B681-9470139C9B8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{24433651-BB7C-44F5-B681-9470139C9B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{24433651-BB7C-44F5-B681-9470139C9B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{24433651-BB7C-44F5-B681-9470139C9B8F}.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 = {F7A92FBC-6A35-4EFA-BC94-5CFA4E20B55A}
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+

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:7240/api/Values/api/ConvertWordToPdf");
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.pdf");
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-PDF", "Convert-Word-Document-to-PDF\Convert-Word-Document-to-PDF.csproj", "{F7EF181A-0400-45DE-8180-73D24F29EF7C}"
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+
{F7EF181A-0400-45DE-8180-73D24F29EF7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F7EF181A-0400-45DE-8180-73D24F29EF7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F7EF181A-0400-45DE-8180-73D24F29EF7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F7EF181A-0400-45DE-8180-73D24F29EF7C}.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 = {1546EB77-7CED-45D1-B862-3A735EB98926}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.DocIORenderer;
4+
using Syncfusion.Pdf;
5+
6+
namespace Convert_Word_Document_to_PDF.Controllers
7+
{
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class ValuesController : ControllerBase
11+
{
12+
[HttpGet]
13+
[Route("api/ConvertWordToPdf")]
14+
public IActionResult ConvertWordToPdf()
15+
{
16+
try
17+
{
18+
var fileDownloadName = "Output.pdf";
19+
const string contentType = "application/pdf";
20+
var stream = ConvertWordDocumentToPdf();
21+
stream.Position = 0;
22+
return File(stream, contentType, fileDownloadName);
23+
}
24+
catch (Exception ex)
25+
{
26+
return BadRequest("Error occurred while converting Word to PDF: " + ex.Message);
27+
}
28+
}
29+
30+
public static MemoryStream ConvertWordDocumentToPdf()
31+
{
32+
//Open the existing PowerPoint presentation with loaded stream.
33+
using (WordDocument wordDocument = new WordDocument(Path.GetFullPath("Data/Template.docx")))
34+
{
35+
using (DocIORenderer render = new DocIORenderer())
36+
{
37+
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
38+
//Create the MemoryStream to save the converted PDF.
39+
MemoryStream pdfStream = new MemoryStream();
40+
//Save the converted PDF document to MemoryStream.
41+
pdfDocument.Save(pdfStream);
42+
pdfStream.Position = 0;
43+
//Download PDF document in the browser.
44+
return pdfStream;
45+
}
46+
}
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace Convert_Word_Document_to_PDF.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_PDF</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_PDF_HostAddress = http://localhost:5122
2+
3+
GET {{Convert_Word_Document_to_PDF_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###

0 commit comments

Comments
 (0)