Skip to content

Commit fa2f65a

Browse files
Merge pull request #482 from SyncfusionExamples/Find-Used-Fonts-in-Word-Document-in-Portable
Added sample for Find Used Fonts in Word Document
2 parents 8d466ff + e187af0 commit fa2f65a

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-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}") = "Find-used-fonts-in-word-document", "Find-used-fonts-in-word-document\Find-used-fonts-in-word-document.csproj", "{1D826977-7033-4103-A1CB-949C1F9D0461}"
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+
{1D826977-7033-4103-A1CB-949C1F9D0461}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{1D826977-7033-4103-A1CB-949C1F9D0461}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{1D826977-7033-4103-A1CB-949C1F9D0461}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{1D826977-7033-4103-A1CB-949C1F9D0461}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<RootNamespace>Find_used_fonts_in_word_document</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Input.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.Drawing;
4+
5+
namespace Find_used_fonts_in_word_document
6+
{
7+
internal class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
//Open the template document.
12+
WordDocument wordDocument = new WordDocument(Path.GetFullPath(@"Data/Input.docx"), FormatType.Docx);
13+
//Get the list of fonts used in current Word document.
14+
List<Font> usedFontsCollection = GetUsedFontsList(wordDocument);
15+
Console.WriteLine("List of fonts used in the Word document:");
16+
foreach (Font usedFont in usedFontsCollection)
17+
{
18+
Console.WriteLine("Font name : " + usedFont.Name + "; Bold : " + usedFont.Bold + "; Italic : " + usedFont.Italic);
19+
}
20+
//Save word document.
21+
wordDocument.Save("Output.docx");
22+
wordDocument.Close();
23+
Console.WriteLine("Press enter key to exit.");
24+
Console.ReadKey();
25+
}
26+
/// <summary>
27+
/// Get the List of fonts which is used in current Word document.
28+
/// </summary>
29+
/// <param name="document">Word document</param>
30+
/// <returns>Used font collection</returns>
31+
private static List<Font> GetUsedFontsList(WordDocument document)
32+
{
33+
List<Font> usedFonts = new List<Font>();
34+
Font font = null;
35+
//Visits all document entities.
36+
foreach (dynamic item in document.Visit())
37+
{
38+
// Gets the font from CharacterFormat or BreakCharacterFormat.
39+
font = HasProperty(item, "CharacterFormat") ? item.CharacterFormat.Font :
40+
(HasProperty(item, "BreakCharacterFormat") ? item.BreakCharacterFormat.Font : null);
41+
42+
AddToFontsCollection(font, usedFonts);
43+
44+
//Gets the font from ListFormat.
45+
GetUsedFontFromListFormat(item, usedFonts);
46+
}
47+
return usedFonts;
48+
}
49+
/// <summary>
50+
/// Add the font to the collection.
51+
/// </summary>
52+
/// <param name="font">Font to add.</param>
53+
/// <param name="usedFonts">Collection of fonts.</param>
54+
private static void AddToFontsCollection(Font font, List<Font> usedFonts)
55+
{
56+
// Check whether the font is different in name and font style properties and add it to the collection.
57+
if (font != null && font is Font &&
58+
!(usedFonts.Find((match) => match.Name == font.Name && match.Italic == font.Italic && match.Bold == font.Bold) is Font))
59+
{
60+
usedFonts.Add(font);
61+
}
62+
}
63+
/// <summary>
64+
/// Gets the fonts used in the List Format.
65+
/// </summary>
66+
/// <param name="item">Current item.</param>
67+
/// <param name="usedFonts">Collection of fonts.</param>
68+
/// <returns></returns>
69+
private static void GetUsedFontFromListFormat(dynamic item, List<Font> usedFonts)
70+
{
71+
if (item is WParagraph)
72+
{
73+
//if item is a paragraph then get the font from list format.
74+
if (item.ListFormat != null && item.ListFormat.CurrentListLevel != null)
75+
{
76+
Font font = item.ListFormat.CurrentListLevel.CharacterFormat.Font;
77+
AddToFontsCollection(font, usedFonts);
78+
}
79+
}
80+
}
81+
/// <summary>
82+
/// Check whether the property is available in the object.
83+
/// </summary>
84+
/// <param name="obj">Current object.</param>
85+
/// <param name="name">Property to check.</param>
86+
/// <returns>True, if object has property. Otherwise, false.</returns>
87+
private static bool HasProperty(dynamic obj, string name)
88+
{
89+
Type objType = obj.GetType();
90+
return objType.GetProperty(name) != null;
91+
}
92+
}
93+
94+
#region ExtendedClass
95+
/// <summary>
96+
/// DocIO extension class.
97+
/// </summary>
98+
public static class DocIOExtensions
99+
{
100+
public static IEnumerable<IEntity> Visit(this ICompositeEntity entity)
101+
{
102+
var entities = new Stack<IEntity>(new IEntity[] { entity });
103+
while (entities.Count > 0)
104+
{
105+
var e = entities.Pop();
106+
yield return e;
107+
if (e is ICompositeEntity)
108+
{
109+
foreach (IEntity childEntity in ((ICompositeEntity)e).ChildEntities)
110+
{
111+
entities.Push(childEntity);
112+
}
113+
}
114+
}
115+
}
116+
}
117+
#endregion
118+
}

0 commit comments

Comments
 (0)