Skip to content

ES-893200- Add sample for Font-substitution in Word to Image conversion #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{847FCA42-187F-427D-980F-6F7E1D27A767}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{847FCA42-187F-427D-980F-6F7E1D27A767}.Debug|Any CPU.Build.0 = Debug|Any CPU
{847FCA42-187F-427D-980F-6F7E1D27A767}.Release|Any CPU.ActiveCfg = Release|Any CPU
{847FCA42-187F-427D-980F-6F7E1D27A767}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {48159659-795E-4CE5-BEBF-42EA1C1F9B78}
EndGlobalSection
EndGlobal
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.IO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Drawing;

namespace Use_alternate_font_without_installing
{
class Program
{
static void Main(string[] args)
{
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open))
{
//Loads an existing Word document.
using (WordDocument wordDocument = new WordDocument(fileStream, Syncfusion.DocIO.FormatType.Automatic))
{
//Hooks the font substitution event.
wordDocument.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
//Creates an instance of DocIORenderer.
using (DocIORenderer renderer = new DocIORenderer())
{
//Convert the entire Word document to images.
Stream[] imageStreams = wordDocument.RenderAsImages();
int i = 0;
foreach (Stream stream in imageStreams)
{
//Reset the stream position.
stream.Position = 0;
//Save the stream as file.
using (FileStream fileStreamOutput = File.Create("Output/WordToImage_" + i + ".jpeg"))
{
stream.CopyTo(fileStreamOutput);
}
i++;
}
}
//Unhooks the font substitution event after converting to Image.
wordDocument.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;
}
}
}
private static void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
//Sets the alternate font when a specified font is not installed in the production environment.
if (args.OrignalFontName == "Arial Unicode MS")
{
switch (args.FontStyle)
{
case FontStyle.Italic:
args.AlternateFontStream = new FileStream(Path.GetFullPath(@"Data/Arial_italic.TTF"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
break;
case FontStyle.Bold:
args.AlternateFontStream = new FileStream(Path.GetFullPath(@"Data/Arial_bold.TTF"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
break;
default:
args.AlternateFontStream = new FileStream(Path.GetFullPath(@"Data/Arial.TTF"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Use_alternate_font_without_installing</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Arial.TTF">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Arial_bold.TTF">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Arial_italic.TTF">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Use-alternate-installed-font", "Use-alternate-installed-font/Use-alternate-installed-font.csproj", "{3725A863-9E8B-4E8D-9838-C25075B80804}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3725A863-9E8B-4E8D-9838-C25075B80804}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3725A863-9E8B-4E8D-9838-C25075B80804}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3725A863-9E8B-4E8D-9838-C25075B80804}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3725A863-9E8B-4E8D-9838-C25075B80804}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BAC6BA07-F38B-44D0-A557-0AA30B0893CE}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.IO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

namespace Use_alternate_installed_font
{
class Program
{
static void Main(string[] args)
{
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open))
{
//Loads an existing Word document.
using (WordDocument wordDocument = new WordDocument(fileStream, Syncfusion.DocIO.FormatType.Automatic))
{
//Hooks the font substitution event.
wordDocument.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
//Creates an instance of DocIORenderer.
using (DocIORenderer renderer = new DocIORenderer())
{
//Convert the entire Word document to images.
Stream[] imageStreams = wordDocument.RenderAsImages();
int i = 0;
foreach (Stream stream in imageStreams)
{
//Reset the stream position.
stream.Position = 0;
//Save the stream as file.
using (FileStream fileStreamOutput = File.Create("Output/WordToImage_" + i + ".jpeg"))
{
stream.CopyTo(fileStreamOutput);
}
i++;
}
}
//Unhooks the font substitution event after converting to Image.
wordDocument.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;
}
}
}
private static void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
//Sets the alternate font when a specified font is not installed in the production environment.
//If "Arial Unicode MS" font is not installed, then it uses the "Arial" font.
//For other missing fonts, uses the "Times New Roman".
if (args.OriginalFontName == "Arial Unicode MS")
args.AlternateFontName = "Arial";
else
args.AlternateFontName = "Times New Roman";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Use_alternate_installed_font</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading