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