Skip to content

Commit 17911a3

Browse files
Merge pull request #540 from SyncfusionExamples/1012802-Font-manager
1012802- Added Dedicated font sample in DocIO
2 parents ee81222 + 53a02b6 commit 17911a3

76 files changed

Lines changed: 74970 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
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.37012.4 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dedicated-font-manager", "Dedicated-font-manager\Dedicated-font-manager.csproj", "{419DDFEB-A2E2-4090-9E1D-0BE6D9EE80AC}"
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+
{419DDFEB-A2E2-4090-9E1D-0BE6D9EE80AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{419DDFEB-A2E2-4090-9E1D-0BE6D9EE80AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{419DDFEB-A2E2-4090-9E1D-0BE6D9EE80AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{419DDFEB-A2E2-4090-9E1D-0BE6D9EE80AC}.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 = {75E58B0E-2E19-4599-894D-D6CDD93A3D7C}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
using Dedicated_font_manager.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Syncfusion.DocIO.DLS;
4+
using Syncfusion.DocIORenderer;
5+
using Syncfusion.Pdf;
6+
using Syncfusion.Presentation;
7+
using Syncfusion.PresentationRenderer;
8+
using Syncfusion.XlsIO;
9+
using Syncfusion.XlsIORenderer;
10+
using System.Diagnostics;
11+
12+
namespace Dedicated_font_manager.Controllers
13+
{
14+
public class HomeController : Controller
15+
{
16+
private readonly ILogger<HomeController> _logger;
17+
18+
public HomeController(ILogger<HomeController> logger)
19+
{
20+
_logger = logger;
21+
}
22+
public IActionResult OfficeToPDF(string button)
23+
{
24+
if (button == null)
25+
return View("Index");
26+
27+
if (Request.Form.Files != null)
28+
{
29+
if (Request.Form.Files.Count == 0)
30+
{
31+
ViewBag.Message = string.Format("Browse a Word document and then click the button to convert as a PDF document");
32+
return View("Index");
33+
}
34+
// Gets the extension from file.
35+
string extension = Path.GetExtension(Request.Form.Files[0].FileName).ToLower();
36+
string fileName = Path.GetFileNameWithoutExtension(Request.Form.Files[0].FileName);
37+
38+
try
39+
{
40+
PdfDocument pdfDocument = null;
41+
// Switch on file extension to determine conversion method
42+
switch (extension.ToLower())
43+
{
44+
// Word document formats
45+
case ".doc":
46+
case ".docx":
47+
case ".dot":
48+
case ".dotx":
49+
case ".dotm":
50+
case ".docm":
51+
case ".xml":
52+
case ".rtf":
53+
using (MemoryStream inputStream = new MemoryStream())
54+
{
55+
// Copy uploaded file to memory stream
56+
Request.Form.Files[0].CopyTo(inputStream);
57+
inputStream.Position = 0;
58+
// Open and load the Word document
59+
using (WordDocument wordDocument = new WordDocument())
60+
{
61+
// Convert Word document to PDF format
62+
wordDocument.Open(inputStream, Syncfusion.DocIO.FormatType.Automatic);
63+
using (DocIORenderer renderer = new DocIORenderer())
64+
{
65+
pdfDocument = renderer.ConvertToPDF(wordDocument);
66+
}
67+
}
68+
}
69+
break;
70+
// Excel format
71+
case ".xlsx":
72+
case ".xls":
73+
case ".xltx":
74+
case ".xlsm":
75+
case ".csv":
76+
case ".xlsb":
77+
case ".xltm":
78+
using (MemoryStream inputStream = new MemoryStream())
79+
{
80+
// Copy uploaded file to memory stream
81+
Request.Form.Files[0].CopyTo(inputStream);
82+
inputStream.Position = 0;
83+
// Create Excel engine and load workbook
84+
using (ExcelEngine excelEngine = new ExcelEngine())
85+
{
86+
IApplication application = excelEngine.Excel;
87+
application.DefaultVersion = ExcelVersion.Xlsx;
88+
IWorkbook workbook = application.Workbooks.Open(inputStream);
89+
// Convert Excel workbook to PDF format
90+
XlsIORenderer renderer = new XlsIORenderer();
91+
pdfDocument = renderer.ConvertToPDF(workbook);
92+
}
93+
}
94+
break;
95+
// PowerPoint format
96+
case ".pptx":
97+
using (MemoryStream inputStream = new MemoryStream())
98+
{
99+
// Copy uploaded file to memory stream
100+
Request.Form.Files[0].CopyTo(inputStream);
101+
inputStream.Position = 0;
102+
// Open PowerPoint presentation and convert to PDF
103+
using (IPresentation pptxDoc = Presentation.Open(inputStream))
104+
{
105+
pdfDocument = PresentationToPdfConverter.Convert(pptxDoc);
106+
}
107+
}
108+
break;
109+
// Invalid file format
110+
default:
111+
ViewBag.Message = "Please choose Word, Excel or PowerPoint document to convert to PDF";
112+
return null;
113+
}
114+
// Save converted PDF and return as downloadable file
115+
if (pdfDocument != null)
116+
{
117+
using (pdfDocument)
118+
{
119+
// Create memory stream to hold the PDF data
120+
MemoryStream pdfStream = new MemoryStream();
121+
// Save the converted PDF to memory stream
122+
pdfDocument.Save(pdfStream);
123+
// Reset stream position to beginning for reading
124+
pdfStream.Position = 0;
125+
// Return PDF as downloadable file to browser
126+
return File(pdfStream, "application/pdf", fileName + ".pdf");
127+
}
128+
}
129+
}
130+
catch (Exception ex)
131+
{
132+
ViewBag.Message = string.Format(ex.Message);
133+
}
134+
}
135+
else
136+
{
137+
ViewBag.Message = string.Format("Browse a Word,Excel or PowerPoint document and then click the button to convert as a PDF document");
138+
}
139+
return View("Index");
140+
}
141+
142+
public IActionResult Index()
143+
{
144+
return View();
145+
}
146+
147+
public IActionResult Privacy()
148+
{
149+
return View();
150+
}
151+
152+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
153+
public IActionResult Error()
154+
{
155+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
156+
}
157+
}
158+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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>Dedicated_font_manager</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
12+
<PackageReference Include="Syncfusion.PresentationRenderer.Net.Core" Version="*" />
13+
<PackageReference Include="Syncfusion.XlsIORenderer.Net.Core" Version="*" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Dedicated_font_manager.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
builder.Services.AddControllersWithViews();
5+
6+
var app = builder.Build();
7+
8+
// Set FontManager delay at startup (before any conversions happen)
9+
// Default is 30000ms. Adjust based on your conversion workload.
10+
Syncfusion.Drawing.Fonts.FontManager.Delay = 50000;
11+
12+
// Access the application lifetime service
13+
var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
14+
15+
// Register a callback to run when the app is shutting down
16+
lifetime.ApplicationStopping.Register(() =>
17+
{
18+
Syncfusion.Drawing.Fonts.FontManager.ClearCache();
19+
});
20+
21+
// Configure the HTTP request pipeline.
22+
if (!app.Environment.IsDevelopment())
23+
{
24+
app.UseExceptionHandler("/Home/Error");
25+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
26+
app.UseHsts();
27+
}
28+
29+
app.UseHttpsRedirection();
30+
app.UseStaticFiles();
31+
32+
app.UseRouting();
33+
34+
app.UseAuthorization();
35+
36+
app.MapControllerRoute(
37+
name: "default",
38+
pattern: "{controller=Home}/{action=Index}/{id?}");
39+
40+
app.Run();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:62691",
8+
"sslPort": 44377
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5072",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7105;http://localhost:5072",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
@using (Html.BeginForm("OfficeToPDF", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
6+
{
7+
@Html.AntiForgeryToken()
8+
<div class="Common">
9+
<div class="tablediv">
10+
<div class="rowdiv">
11+
<p>This sample illustrates how to convert Word document, Excel and PowerPoint file to PDF using Document SDK.</p>
12+
13+
<p><strong>Font Manager:</strong> A unified font-caching system that optimizes memory usage across all document conversion libraries (Word-to-PDF, Excel-to-PDF, PowerPoint-to-PDF, and PDF processing).
14+
This centralized font management significantly optimizes memory usage in multi-threaded conversions by preventing redundant font loading.
15+
</p>
16+
</div>
17+
18+
<div class="rowdiv" style="border-width: 0.5px; border-style: solid; border-color: lightgray; padding: 1px 5px 7px 5px; margin-top: 8px;">
19+
Click the button to view the resultant PDF document being converted using Document SDK.
20+
Please note that a PDF viewer is required to view the resultant PDF.
21+
22+
<div class="rowdiv" style="margin-top: 10px">
23+
<div class="celldiv">
24+
Select Document :
25+
@Html.TextBox("file", null, new { type = "file", accept = ".doc,.docx,.rtf,.dot,.dotm,.dotx,.docm,.xml,.xlsx,.xls,.xltx,.xlsm,.csv,.xlsb,.xltm,.pptx" })
26+
<br />
27+
</div>
28+
29+
<div class="rowdiv" style="margin-top: 8px">
30+
<input class="buttonStyle" type="submit" value="Convert to PDF" name="button" style="width:150px;height:27px" />
31+
<br />
32+
<div class="text-danger">
33+
@ViewBag.Message
34+
</div>
35+
</div>
36+
</div>
37+
</div>
38+
</div>
39+
</div>
40+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>

0 commit comments

Comments
 (0)