Skip to content

Commit

Permalink
Merge pull request #130 from AkashArul26/main
Browse files Browse the repository at this point in the history
ES-263244 Sample to find list of used fonts in the Word document
  • Loading branch information
MohanaselvamJothi authored Oct 4, 2023
2 parents 202cc71 + 8e85e3f commit 3618159
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 0 deletions.
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 17
VisualStudioVersion = 17.6.33829.357
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", "{705D8B2F-A110-4DCB-B527-51ECF9786621}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{705D8B2F-A110-4DCB-B527-51ECF9786621}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{705D8B2F-A110-4DCB-B527-51ECF9786621}.Debug|Any CPU.Build.0 = Debug|Any CPU
{705D8B2F-A110-4DCB-B527-51ECF9786621}.Release|Any CPU.ActiveCfg = Release|Any CPU
{705D8B2F-A110-4DCB-B527-51ECF9786621}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1233443E-9D90-454C-93B1-4C3506EBDB21}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{705D8B2F-A110-4DCB-B527-51ECF9786621}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Find_used_fonts_in_word_document</RootNamespace>
<AssemblyName>Find-used-fonts-in-word-document</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Syncfusion.Compression.Base, Version=23.1460.38.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
<HintPath>..\packages\Syncfusion.Compression.Base.23.1.38\lib\net46\Syncfusion.Compression.Base.dll</HintPath>
</Reference>
<Reference Include="Syncfusion.DocIO.Base, Version=23.1460.38.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
<HintPath>..\packages\Syncfusion.DocIO.WinForms.23.1.38\lib\net46\Syncfusion.DocIO.Base.dll</HintPath>
</Reference>
<Reference Include="Syncfusion.Licensing, Version=23.1460.38.0, Culture=neutral, PublicKeyToken=632609b4d040f6b4, processorArchitecture=MSIL">
<HintPath>..\packages\Syncfusion.Licensing.23.1.38\lib\net46\Syncfusion.Licensing.dll</HintPath>
</Reference>
<Reference Include="Syncfusion.OfficeChart.Base, Version=23.1460.38.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
<HintPath>..\packages\Syncfusion.OfficeChart.Base.23.1.38\lib\net46\Syncfusion.OfficeChart.Base.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Find_used_fonts_in_word_document
{
internal class Program
{
static void Main(string[] args)
{
//Open the template document.
WordDocument wordDocument = new WordDocument(@"../../Data/Adventure.docx", FormatType.Docx);
//Get the list of fonts used in current Word document.
List<Font> 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();
}
/// <summary>
/// Get the List of fonts which is used in current Word document.
/// </summary>
/// <param name="document">Word document</param>
/// <returns>Used font collection</returns>
private static List<Font> GetUsedFontsList(WordDocument document)
{
List<Font> usedFonts = new List<Font>();
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;
}
/// <summary>
/// Add the font to the collection.
/// </summary>
/// <param name="font">Font to add.</param>
/// <param name="usedFonts">Collection of fonts.</param>
private static void AddToFontsCollection(Font font, List<Font> 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);
}
}
/// <summary>
/// Gets the fonts used in the List Format.
/// </summary>
/// <param name="item">Current item.</param>
/// <param name="usedFonts">Collection of fonts.</param>
/// <returns></returns>
private static void GetUsedFontFromListFormat(dynamic item, List<Font> 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);
}
}
}
/// <summary>
/// Check whether the property is available in the object.
/// </summary>
/// <param name="obj">Current object.</param>
/// <param name="name">Property to check.</param>
/// <returns>True, if object has property. Otherwise, false.</returns>
private static bool HasProperty(dynamic obj, string name)
{
Type objType = obj.GetType();
return objType.GetProperty(name) != null;
}
}

#region ExtendedClass
/// <summary>
/// DocIO extension class.
/// </summary>
public static class DocIOExtensions
{
public static IEnumerable<IEntity> Visit(this ICompositeEntity entity)
{
var entities = new Stack<IEntity>(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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Find-used-fonts-in-word-document")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Find-used-fonts-in-word-document")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("705d8b2f-a110-4dcb-b527-51ecf9786621")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Syncfusion.Compression.Base" version="23.1.38" targetFramework="net46" />
<package id="Syncfusion.DocIO.WinForms" version="23.1.38" targetFramework="net46" />
<package id="Syncfusion.Licensing" version="23.1.38" targetFramework="net46" />
<package id="Syncfusion.OfficeChart.Base" version="23.1.38" targetFramework="net46" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
How to run the examples
-----------------------

* Download this project to a location in your disk.
* Open the solution file using Visual Studio.
* Rebuild the solution to install the required NuGet packages.
* Run the application.

0 comments on commit 3618159

Please sign in to comment.