Skip to content

Commit 1440798

Browse files
ES-893200-Font-substitution
1 parent 6895d2e commit 1440798

File tree

13 files changed

+218
-0
lines changed

13 files changed

+218
-0
lines changed
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 16
4+
VisualStudioVersion = 16.0.30804.86
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Use-alternate-font-without-installing", "Use-alternate-font-without-installing/Use-alternate-font-without-installing.csproj", "{847FCA42-187F-427D-980F-6F7E1D27A767}"
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+
{847FCA42-187F-427D-980F-6F7E1D27A767}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{847FCA42-187F-427D-980F-6F7E1D27A767}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{847FCA42-187F-427D-980F-6F7E1D27A767}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{847FCA42-187F-427D-980F-6F7E1D27A767}.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 = {48159659-795E-4CE5-BEBF-42EA1C1F9B78}
24+
EndGlobalSection
25+
EndGlobal

Word-to-Image-conversion/Use-alternate-font-without-installing/.NET/Use-alternate-font-without-installing/Output/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.IO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.DocIORenderer;
4+
using Syncfusion.Drawing;
5+
6+
namespace Use_alternate_font_without_installing
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open))
13+
{
14+
//Loads an existing Word document.
15+
using (WordDocument wordDocument = new WordDocument(fileStream, Syncfusion.DocIO.FormatType.Automatic))
16+
{
17+
//Hooks the font substitution event.
18+
wordDocument.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
19+
//Creates an instance of DocIORenderer.
20+
using (DocIORenderer renderer = new DocIORenderer())
21+
{
22+
//Convert the entire Word document to images.
23+
Stream[] imageStreams = wordDocument.RenderAsImages();
24+
int i = 0;
25+
foreach (Stream stream in imageStreams)
26+
{
27+
//Reset the stream position.
28+
stream.Position = 0;
29+
//Save the stream as file.
30+
using (FileStream fileStreamOutput = File.Create("Output/WordToImage_" + i + ".jpeg"))
31+
{
32+
stream.CopyTo(fileStreamOutput);
33+
}
34+
i++;
35+
}
36+
}
37+
//Unhooks the font substitution event after converting to Image.
38+
wordDocument.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;
39+
}
40+
}
41+
}
42+
private static void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
43+
{
44+
//Sets the alternate font when a specified font is not installed in the production environment.
45+
if (args.OrignalFontName == "Arial Unicode MS")
46+
{
47+
switch (args.FontStyle)
48+
{
49+
case FontStyle.Italic:
50+
args.AlternateFontStream = new FileStream(Path.GetFullPath(@"Data/Arial_italic.TTF"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
51+
break;
52+
case FontStyle.Bold:
53+
args.AlternateFontStream = new FileStream(Path.GetFullPath(@"Data/Arial_bold.TTF"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
54+
break;
55+
default:
56+
args.AlternateFontStream = new FileStream(Path.GetFullPath(@"Data/Arial.TTF"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
57+
break;
58+
}
59+
}
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Use_alternate_font_without_installing</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Arial.TTF">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Arial_bold.TTF">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Data\Arial_italic.TTF">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
<None Update="Data\Template.docx">
24+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25+
</None>
26+
<None Update="Output\.gitkeep">
27+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
28+
</None>
29+
</ItemGroup>
30+
31+
</Project>
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 16
4+
VisualStudioVersion = 16.0.30804.86
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Use-alternate-installed-font", "Use-alternate-installed-font/Use-alternate-installed-font.csproj", "{3725A863-9E8B-4E8D-9838-C25075B80804}"
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+
{3725A863-9E8B-4E8D-9838-C25075B80804}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3725A863-9E8B-4E8D-9838-C25075B80804}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3725A863-9E8B-4E8D-9838-C25075B80804}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3725A863-9E8B-4E8D-9838-C25075B80804}.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 = {BAC6BA07-F38B-44D0-A557-0AA30B0893CE}
24+
EndGlobalSection
25+
EndGlobal

Word-to-Image-conversion/Use-alternate-installed-font/.NET/Use-alternate-installed-font/Output/.gitkeep

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.IO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.DocIORenderer;
4+
using Syncfusion.Pdf;
5+
6+
namespace Use_alternate_installed_font
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open))
13+
{
14+
//Loads an existing Word document.
15+
using (WordDocument wordDocument = new WordDocument(fileStream, Syncfusion.DocIO.FormatType.Automatic))
16+
{
17+
//Hooks the font substitution event.
18+
wordDocument.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
19+
//Creates an instance of DocIORenderer.
20+
using (DocIORenderer renderer = new DocIORenderer())
21+
{
22+
//Convert the entire Word document to images.
23+
Stream[] imageStreams = wordDocument.RenderAsImages();
24+
int i = 0;
25+
foreach (Stream stream in imageStreams)
26+
{
27+
//Reset the stream position.
28+
stream.Position = 0;
29+
//Save the stream as file.
30+
using (FileStream fileStreamOutput = File.Create("Output/WordToImage_" + i + ".jpeg"))
31+
{
32+
stream.CopyTo(fileStreamOutput);
33+
}
34+
i++;
35+
}
36+
}
37+
//Unhooks the font substitution event after converting to Image.
38+
wordDocument.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;
39+
}
40+
}
41+
}
42+
private static void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
43+
{
44+
//Sets the alternate font when a specified font is not installed in the production environment.
45+
//If "Arial Unicode MS" font is not installed, then it uses the "Arial" font.
46+
//For other missing fonts, uses the "Times New Roman".
47+
if (args.OriginalFontName == "Arial Unicode MS")
48+
args.AlternateFontName = "Arial";
49+
else
50+
args.AlternateFontName = "Times New Roman";
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Use_alternate_installed_font</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)