ListEmbedFonts.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This sample shows how to list fonts embedded in a DOCX.
    public class ListEmbedFonts
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();

            // Load a DOCX that contains embedded font:            
            doc.Load(Path.Combine("Resources", "WordDocs", "EmbedFonts.docx"));

            var embeddedFont = doc.Fonts.FirstOrDefault(fi_ => fi_.Embedded.Count > 1)?.Embedded[0];

            doc.Body.Paragraphs.Add("Embedded fonts found in this document:", doc.Styles[BuiltInStyleId.Heading1]);
            var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");

            // Enumerate all embedded fonts:
            foreach (var fi in doc.Fonts)
            {
                foreach (var ef in fi.Embedded)
                {
                    var p = doc.Body.Paragraphs.Add(doc.Styles[BuiltInStyleId.ListParagraph]);
                    p.ListFormat.Template = myListTemplate;
                    var run = p.GetRange().Runs.Add($"Found embedded font of type {ef.Type} in font \"{fi.Name}\".");
                    run.Font.Name = fi.Name;
                    if (ef.Type == EmbeddedFontType.Bold)
                        run.Font.Bold = true;
                    else if (ef.Type == EmbeddedFontType.Italic)
                        run.Font.Italic = true;
                    else if (ef.Type == EmbeddedFontType.BoldItalic)
                        run.Font.Bold = run.Font.Italic = true;
                }
            }

            // Done:
            return doc;
        }
    }
}