using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using Telerik.Windows.Documents.Fixed.Model.Fonts;
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
using Telerik.Windows.Documents.Flow.FormatProviders.Pdf;
using Telerik.Windows.Documents.Flow.Model;

namespace ArialNarrowItalicAndBoldExportDemo
{
    class Program
    {
        static void Main()
        {
            RegisterWrongFontPaths();

            string importFileName = "Releway.docx";
            string exportFileName = "Releway.pdf";

            RadFlowDocument document = new DocxFormatProvider().Import(File.ReadAllBytes(importFileName));

            if (File.Exists(exportFileName))
            {
                File.Delete(exportFileName);
            }

            File.WriteAllBytes(exportFileName, new PdfFormatProvider().Export(document));
            Process.Start(exportFileName);
        }

        /// <summary>
        /// This method fixes bug in System.Windows.Media.Typeface.TryGetGlyphTypeface method. 
        /// The bug is similar to the one mentioned in the following forum post:
        /// https://social.msdn.microsoft.com/Forums/vstudio/en-US/7dbab379-b723-4687-aed4-2a8eb2c07c4c/typefacetrygetglyphtypeface-doesnt-return-the-correct-font?forum=wpf
        /// </summary>
        private static void RegisterWrongFontPaths()
        {
            /*   FontsRepository.RegisterFont(new System.Windows.Media.FontFamily("Arial Narrow"), FontStyles.Normal, FontWeights.Bold,
                   File.ReadAllBytes(@"C:\WINDOWS\FONTS\ARIALNB.TTF"));
               FontsRepository.RegisterFont(new System.Windows.Media.FontFamily("Arial Narrow"), FontStyles.Italic, FontWeights.Normal,
                   File.ReadAllBytes(@"C:\WINDOWS\FONTS\ARIALNI.TTF"));
               FontsRepository.RegisterFont(new System.Windows.Media.FontFamily("Arial Narrow"), FontStyles.Italic, FontWeights.Bold,
                   File.ReadAllBytes(@"C:\WINDOWS\FONTS\ARIALNBI.TTF"));
            */
            FontsRepository.RegisterFont(new System.Windows.Media.FontFamily("Releway"), FontStyles.Normal, FontWeights.Bold,
                   File.ReadAllBytes(@"Raleway-Bold.ttf"));
            FontsRepository.RegisterFont(new System.Windows.Media.FontFamily("Releway"), FontStyles.Italic, FontWeights.Bold,
                   File.ReadAllBytes(@"Raleway-BoldItalic.ttf"));
            FontsRepository.RegisterFont(new System.Windows.Media.FontFamily("Releway"), FontStyles.Italic, FontWeights.Normal,
                   File.ReadAllBytes(@"Raleway-Italic.ttf"));
            FontsRepository.RegisterFont(new System.Windows.Media.FontFamily("Releway"), FontStyles.Normal, FontWeights.Normal,
                File.ReadAllBytes(@"Raleway-Regular.ttf"));
            FontsRepository.RegisterFont(new System.Windows.Media.FontFamily("Releway"), FontStyles.Normal, FontWeights.ExtraBold,
                File.ReadAllBytes(@"Raleway-ExtraBold.ttf"));

        }
    }
}
