EmbedFonts.cs
//
// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;

namespace GcWordWeb.Samples
{
    // This sample shows how to embed font data into a DOCX.
    public class EmbedFonts
    {
        public GcWordDocument CreateDocx()
        {
            // Custom names for fonts that will be embedded:
            const string myFontName1 = "My Font 1";
            const string myFontName2 = "My Font 2";

            var doc = new GcWordDocument();

            // Use first of the fonts to be embedded:
            var p = doc.Body.Paragraphs.Add();
            var run = p.GetRange().Runs.Add($"Text rendered using embedded font \"{myFontName1}\".");
            // Apply custom font to the run:
            run.Font.Name = myFontName1;

            // Use second of the fonts to be embedded:
            p = doc.Body.Paragraphs.Add();
            run = p.GetRange().Runs.Add($"Text rendered using embedded font \"{myFontName2}\".");
            run.Font.Name = myFontName2;

            // For reference, Panose numbers for Latin Text:
            // 1.Family Kind(= 2 for Latin Text)
            // 2.Serif Style
            // 3.Weight
            // 4.Proportion
            // 5.Contrast
            // 6.Stroke Variation
            // 7.Arm Style
            // 8.Letterform
            // 9.Midline
            // 10.X - height

            // Add first font (Times New Roman) to the document:
            var font1 = doc.Fonts.Add(myFontName1);
            // Use "Times New Roman" font settings:
            font1.CharSet = FontCharSet.Ansi;
            font1.Family = FontFamily.Roman;
            font1.Pitch = FontPitch.Variable;
            font1.Panose = new List<byte> { 2, 2, 6, 3, 5, 4, 5, 2, 3, 4 };
            font1.Signature.CodePageRange1 = 0x000001ff;
            font1.Signature.CodePageRange2 = 0x00000000;
            font1.Signature.UnicodeRange1 = 0xE0002EFF;
            font1.Signature.UnicodeRange2 = 0xC000785B;
            font1.Signature.UnicodeRange3 = 0x00000009;
            font1.Signature.UnicodeRange4 = 0x00000000;
            // Load the "Times New Roman" font data:
            byte[] data1 = File.ReadAllBytes(Path.Combine("Resources", "Fonts", "times.ttf"));
            // Embed font data into the document:
            font1.Embedded.Add(EmbeddedFontType.Regular, FontDataType.ObfuscatedTrueTypeFont, data1);

            // Add second font (Arial Bold Italic) to the document:
            var font2 = doc.Fonts.Add(myFontName2);
            // Use "Times New Roman" font settings:
            font2.CharSet = FontCharSet.Ansi;
            font2.Family = FontFamily.Swiss;
            font2.Pitch = FontPitch.Variable;
            font2.Panose = new List<byte> { 2, 11, 8, 3, 2, 2, 3, 2, 3, 4 };
            font2.Signature.CodePageRange1 = 0x000001ff;
            font2.Signature.CodePageRange2 = 0x00000000;
            font2.Signature.UnicodeRange1 = 0xE0002EFF;
            font2.Signature.UnicodeRange2 = 0xC000785B;
            font2.Signature.UnicodeRange3 = 0x00000009;
            font2.Signature.UnicodeRange4 = 0x00000000;
            // Load the "Arial Bold Italic" font data:
            byte[] data2 = File.ReadAllBytes(Path.Combine("Resources", "Fonts", "arialbi.ttf"));
            // Embed font data into the document:
            font2.Embedded.Add(EmbeddedFontType.BoldItalic, FontDataType.ObfuscatedTrueTypeFont, data2);

            // Done:
            return doc;
        }
    }
}