Skip to content

Commit 8dd71eb

Browse files
authored
Merge pull request #164 from SyncfusionExamples/956797
956797 Added the TOC with Unicode Font while converting HTML to PDF.
2 parents fe02e2d + 40d19e3 commit 8dd71eb

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlToPdfWithUnicodeTOC", "HtmlToPdfWithUnicodeTOC\HtmlToPdfWithUnicodeTOC.csproj", "{4AD65462-EECF-4D8A-82C1-8E4127CE70A3}"
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+
{4AD65462-EECF-4D8A-82C1-8E4127CE70A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4AD65462-EECF-4D8A-82C1-8E4127CE70A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4AD65462-EECF-4D8A-82C1-8E4127CE70A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4AD65462-EECF-4D8A-82C1-8E4127CE70A3}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Multilingual Sample Document</title>
6+
<style>
7+
body {
8+
font-family: Arial, sans-serif;
9+
line-height: 1.6;
10+
padding: 20px;
11+
}
12+
h1, h2 {
13+
color: #2c3e50;
14+
}
15+
ul.toc {
16+
list-style-type: none;
17+
padding-left: 0;
18+
}
19+
ul.toc li {
20+
margin: 5px 0;
21+
}
22+
ul.toc a {
23+
text-decoration: none;
24+
color: #007BFF;
25+
}
26+
ul.toc a:hover {
27+
text-decoration: underline;
28+
}
29+
</style>
30+
</head>
31+
<body>
32+
33+
<h1>Sample Document – Beispiel – Пример документа</h1>
34+
35+
<h2>Table of Contents – Inhaltsverzeichnis – Содержание</h2>
36+
<ul class="toc">
37+
<li><a href="#english">English</a></li>
38+
<li><a href="#german">Deutsch (German)</a></li>
39+
<li><a href="#russian">Русский (Russian)</a></li>
40+
</ul>
41+
42+
<hr>
43+
44+
<h2 id="english">English Section</h2>
45+
<p>This document demonstrates multilingual content in HTML using UTF-8 encoding. You can mix English, German, and Russian in the same page.</p>
46+
47+
<h2 id="german">Deutscher Abschnitt</h2>
48+
<p>Dieses Dokument zeigt mehrsprachige Inhalte in HTML mit UTF-8-Kodierung. Sie können Englisch, Deutsch und Russisch auf derselben Seite verwenden.</p>
49+
<ul>
50+
<li>Zeichen wie ä, ö, ü, ß werden korrekt angezeigt.</li>
51+
<li>Unterstützt auch deutsche Umlaute.</li>
52+
</ul>
53+
54+
<h2 id="russian">Русский раздел</h2>
55+
<p>Этот документ демонстрирует мультиязычное содержимое в HTML с использованием кодировки UTF-8. Вы можете использовать английский, немецкий и русский языки на одной странице.</p>
56+
<ul>
57+
<li>Кириллические символы отображаются правильно.</li>
58+
<li>Полная поддержка русского алфавита.</li>
59+
</ul>
60+
61+
</body>
62+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="*" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.HtmlConverter;
3+
using Syncfusion.Pdf.Graphics;
4+
using Syncfusion.Pdf.HtmlToPdf;
5+
using Syncfusion.Pdf;
6+
7+
// Initialize the HTML to PDF converter with Blink rendering engine
8+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
9+
10+
// Create Blink converter settings
11+
BlinkConverterSettings settings = new BlinkConverterSettings
12+
{
13+
EnableToc = true
14+
};
15+
using (FileStream fontStream = new FileStream(Path.GetFullPath(@"Data/ARIALUNI.TTF"), FileMode.Open, FileAccess.Read))
16+
{
17+
// Load a Unicode TrueType font from system or custom path
18+
PdfTrueTypeFont unicodeFont = new PdfTrueTypeFont(fontStream, 14);
19+
20+
// Set the style for level 1 (H1) items in table of contents
21+
HtmlToPdfTocStyle tocStyleH1 = new HtmlToPdfTocStyle
22+
{
23+
Font = unicodeFont,
24+
BackgroundColor = new PdfSolidBrush(new PdfColor(Color.FromArgb(68, 114, 196))),
25+
ForeColor = PdfBrushes.White,
26+
Padding = new PdfPaddings(5, 5, 3, 3)
27+
};
28+
29+
// Apply the style to TOC level 1
30+
settings.Toc.SetItemStyle(1, tocStyleH1);
31+
32+
// Assign Blink converter settings to HTML converter
33+
htmlConverter.ConverterSettings = settings;
34+
35+
// Convert HTML to PDF
36+
PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/Input.html"));
37+
38+
// Save and close the PDF document
39+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
40+
{
41+
document.Save(fileStream);
42+
}
43+
//Close the PDF document
44+
document.Close(true);
45+
}

0 commit comments

Comments
 (0)