EmbedFontFile.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 embed a True Type font from a .ttf font file,
  15. // and add a paragraph run to a DOCX file using that font.
  16. // The font in this sample is not very common, so without embedding it
  17. // the document will not display correctly on many systems.
  18. public class EmbedFontFile
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. var doc = new GcWordDocument();
  23. // Add a paragraph and a run with some text to the document:
  24. var p = doc.Body.Paragraphs.Add();
  25. var run = p.GetRange().Runs.Add("the quick brown fox jumps over the lazy dog");
  26. run.Font.Size = 24;
  27. // Load font from a .ttf file and set it as the font of the sample text:
  28. var font = GrapeCity.Documents.Text.Font.FromFile(Path.Combine("Resources", "Fonts", "SEGA.ttf"));
  29. // Assign the run's font to the font loaded from the file:
  30. run.Font.Name = font.FontFamilyName;
  31. // This embeds the font into the document, without this line the document won't display correctly
  32. // on a system that does not have the SEGA.ttf font installed:
  33. doc.Fonts.Add(font, true);
  34.  
  35. // Done:
  36. return doc;
  37. }
  38. }
  39. }
  40.