ListEmbedFonts.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Word;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // This sample shows how to list fonts embedded in a DOCX.
  15. public class ListEmbedFonts
  16. {
  17. public GcWordDocument CreateDocx()
  18. {
  19. var doc = new GcWordDocument();
  20.  
  21. // Load a DOCX that contains embedded font:
  22. doc.Load(Path.Combine("Resources", "WordDocs", "EmbedFonts.docx"));
  23.  
  24. var embeddedFont = doc.Fonts.FirstOrDefault(fi_ => fi_.Embedded.Count > 1)?.Embedded[0];
  25.  
  26. doc.Body.Paragraphs.Add("Embedded fonts found in this document:", doc.Styles[BuiltInStyleId.Heading1]);
  27. var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
  28.  
  29. // Enumerate all embedded fonts:
  30. foreach (var fi in doc.Fonts)
  31. {
  32. foreach (var ef in fi.Embedded)
  33. {
  34. var p = doc.Body.Paragraphs.Add(doc.Styles[BuiltInStyleId.ListParagraph]);
  35. p.ListFormat.Template = myListTemplate;
  36. var run = p.GetRange().Runs.Add($"Found embedded font of type {ef.Type} in font \"{fi.Name}\".");
  37. run.Font.Name = fi.Name;
  38. if (ef.Type == EmbeddedFontType.Bold)
  39. run.Font.Bold = true;
  40. else if (ef.Type == EmbeddedFontType.Italic)
  41. run.Font.Italic = true;
  42. else if (ef.Type == EmbeddedFontType.BoldItalic)
  43. run.Font.Bold = run.Font.Italic = true;
  44. }
  45. }
  46.  
  47. // Done:
  48. return doc;
  49. }
  50. }
  51. }
  52.