EmbedFonts.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 font data into a DOCX.
  15. public class EmbedFonts
  16. {
  17. public GcWordDocument CreateDocx()
  18. {
  19. // Custom names for fonts that will be embedded:
  20. const string myFontName1 = "My Font 1";
  21. const string myFontName2 = "My Font 2";
  22.  
  23. var doc = new GcWordDocument();
  24.  
  25. // Use first of the fonts to be embedded:
  26. var p = doc.Body.Paragraphs.Add();
  27. var run = p.GetRange().Runs.Add($"Text rendered using embedded font \"{myFontName1}\".");
  28. // Apply custom font to the run:
  29. run.Font.Name = myFontName1;
  30.  
  31. // Use second of the fonts to be embedded:
  32. p = doc.Body.Paragraphs.Add();
  33. run = p.GetRange().Runs.Add($"Text rendered using embedded font \"{myFontName2}\".");
  34. run.Font.Name = myFontName2;
  35.  
  36. // For reference, Panose numbers for Latin Text:
  37. // 1.Family Kind(= 2 for Latin Text)
  38. // 2.Serif Style
  39. // 3.Weight
  40. // 4.Proportion
  41. // 5.Contrast
  42. // 6.Stroke Variation
  43. // 7.Arm Style
  44. // 8.Letterform
  45. // 9.Midline
  46. // 10.X - height
  47.  
  48. // Add first font (Times New Roman) to the document:
  49. var font1 = doc.Fonts.Add(myFontName1);
  50. // Use "Times New Roman" font settings:
  51. font1.CharSet = FontCharSet.Ansi;
  52. font1.Family = GrapeCity.Documents.Word.FontFamily.Roman;
  53. font1.Pitch = FontPitch.Variable;
  54. font1.Panose = new List<byte> { 2, 2, 6, 3, 5, 4, 5, 2, 3, 4 };
  55. font1.Signature.CodePageRange1 = 0x000001ff;
  56. font1.Signature.CodePageRange2 = 0x00000000;
  57. font1.Signature.UnicodeRange1 = 0xE0002EFF;
  58. font1.Signature.UnicodeRange2 = 0xC000785B;
  59. font1.Signature.UnicodeRange3 = 0x00000009;
  60. font1.Signature.UnicodeRange4 = 0x00000000;
  61. // Load the "Times New Roman" font data:
  62. byte[] data1 = File.ReadAllBytes(Path.Combine("Resources", "Fonts", "times.ttf"));
  63. // Embed font data into the document:
  64. font1.Embedded.Add(EmbeddedFontType.Regular, FontDataType.ObfuscatedTrueTypeFont, data1);
  65.  
  66. // Add second font (Arial Bold Italic) to the document:
  67. var font2 = doc.Fonts.Add(myFontName2);
  68. // Use "Times New Roman" font settings:
  69. font2.CharSet = FontCharSet.Ansi;
  70. font2.Family = GrapeCity.Documents.Word.FontFamily.Swiss;
  71. font2.Pitch = FontPitch.Variable;
  72. font2.Panose = new List<byte> { 2, 11, 8, 3, 2, 2, 3, 2, 3, 4 };
  73. font2.Signature.CodePageRange1 = 0x000001ff;
  74. font2.Signature.CodePageRange2 = 0x00000000;
  75. font2.Signature.UnicodeRange1 = 0xE0002EFF;
  76. font2.Signature.UnicodeRange2 = 0xC000785B;
  77. font2.Signature.UnicodeRange3 = 0x00000009;
  78. font2.Signature.UnicodeRange4 = 0x00000000;
  79. // Load the "Arial Bold Italic" font data:
  80. byte[] data2 = File.ReadAllBytes(Path.Combine("Resources", "Fonts", "arialbi.ttf"));
  81. // Embed font data into the document:
  82. font2.Embedded.Add(EmbeddedFontType.BoldItalic, FontDataType.ObfuscatedTrueTypeFont, data2);
  83.  
  84. // Done:
  85. return doc;
  86. }
  87. }
  88. }
  89.