diff --git a/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document.sln b/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document.sln new file mode 100644 index 00000000..1c1a9e7f --- /dev/null +++ b/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35707.178 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +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}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1D826977-7033-4103-A1CB-949C1F9D0461}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1D826977-7033-4103-A1CB-949C1F9D0461}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1D826977-7033-4103-A1CB-949C1F9D0461}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1D826977-7033-4103-A1CB-949C1F9D0461}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document/Data/Input.docx b/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document/Data/Input.docx new file mode 100644 index 00000000..e4b8b113 Binary files /dev/null and b/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document/Data/Input.docx differ diff --git a/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document/Find-used-fonts-in-word-document.csproj b/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document/Find-used-fonts-in-word-document.csproj new file mode 100644 index 00000000..c6622a02 --- /dev/null +++ b/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document/Find-used-fonts-in-word-document.csproj @@ -0,0 +1,21 @@ + + + + Exe + net9.0 + Find_used_fonts_in_word_document + enable + enable + + + + + + + + + Always + + + + diff --git a/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document/Program.cs b/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document/Program.cs new file mode 100644 index 00000000..350300a8 --- /dev/null +++ b/FAQs/Find-used-fonts-in-word-document/.NET/Find-used-fonts-in-word-document/Program.cs @@ -0,0 +1,118 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIO; +using Syncfusion.Drawing; + +namespace Find_used_fonts_in_word_document +{ + internal class Program + { + static void Main(string[] args) + { + //Open the template document. + WordDocument wordDocument = new WordDocument(Path.GetFullPath(@"Data/Input.docx"), FormatType.Docx); + //Get the list of fonts used in current Word document. + List usedFontsCollection = GetUsedFontsList(wordDocument); + Console.WriteLine("List of fonts used in the Word document:"); + foreach (Font usedFont in usedFontsCollection) + { + Console.WriteLine("Font name : " + usedFont.Name + "; Bold : " + usedFont.Bold + "; Italic : " + usedFont.Italic); + } + //Save word document. + wordDocument.Save("Output.docx"); + wordDocument.Close(); + Console.WriteLine("Press enter key to exit."); + Console.ReadKey(); + } + /// + /// Get the List of fonts which is used in current Word document. + /// + /// Word document + /// Used font collection + private static List GetUsedFontsList(WordDocument document) + { + List usedFonts = new List(); + Font font = null; + //Visits all document entities. + foreach (dynamic item in document.Visit()) + { + // Gets the font from CharacterFormat or BreakCharacterFormat. + font = HasProperty(item, "CharacterFormat") ? item.CharacterFormat.Font : + (HasProperty(item, "BreakCharacterFormat") ? item.BreakCharacterFormat.Font : null); + + AddToFontsCollection(font, usedFonts); + + //Gets the font from ListFormat. + GetUsedFontFromListFormat(item, usedFonts); + } + return usedFonts; + } + /// + /// Add the font to the collection. + /// + /// Font to add. + /// Collection of fonts. + private static void AddToFontsCollection(Font font, List usedFonts) + { + // Check whether the font is different in name and font style properties and add it to the collection. + if (font != null && font is Font && + !(usedFonts.Find((match) => match.Name == font.Name && match.Italic == font.Italic && match.Bold == font.Bold) is Font)) + { + usedFonts.Add(font); + } + } + /// + /// Gets the fonts used in the List Format. + /// + /// Current item. + /// Collection of fonts. + /// + private static void GetUsedFontFromListFormat(dynamic item, List usedFonts) + { + if (item is WParagraph) + { + //if item is a paragraph then get the font from list format. + if (item.ListFormat != null && item.ListFormat.CurrentListLevel != null) + { + Font font = item.ListFormat.CurrentListLevel.CharacterFormat.Font; + AddToFontsCollection(font, usedFonts); + } + } + } + /// + /// Check whether the property is available in the object. + /// + /// Current object. + /// Property to check. + /// True, if object has property. Otherwise, false. + private static bool HasProperty(dynamic obj, string name) + { + Type objType = obj.GetType(); + return objType.GetProperty(name) != null; + } + } + + #region ExtendedClass + /// + /// DocIO extension class. + /// + public static class DocIOExtensions + { + public static IEnumerable Visit(this ICompositeEntity entity) + { + var entities = new Stack(new IEntity[] { entity }); + while (entities.Count > 0) + { + var e = entities.Pop(); + yield return e; + if (e is ICompositeEntity) + { + foreach (IEntity childEntity in ((ICompositeEntity)e).ChildEntities) + { + entities.Push(childEntity); + } + } + } + } + } + #endregion +} \ No newline at end of file