using System;
using System.IO;
using System.Linq;
using System.Diagnostics;

using Telerik.Windows.Documents.Core.Fonts;
using Telerik.Documents.Core.Fonts;
using Telerik.Documents.Primitives;
using Telerik.Windows.Documents.Fixed.Model;
using Telerik.Windows.Documents.Fixed.Model.Fonts;
using Telerik.Windows.Documents.Fixed.Model.Editing;
using Telerik.Windows.Documents.Fixed.Model.ColorSpaces;
using Telerik.Windows.Documents.Fixed.Model.InteractiveForms;
using Telerik.Windows.Documents.Fixed.Model.Common;
using Telerik.Windows.Documents.Fixed.Model.Editing.Tables;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;

namespace BasicPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            AddFonts(); 
            // Adds Calibri fonts versions for: Regular, Bold, Italic, BoldItalic
            // (which is required when you want to use both Bold and Italic no the same characters)

            PdfFormatProvider pdfProvider = new PdfFormatProvider();

            RadFixedDocument radFixedDocument = new RadFixedDocument();
            RadFixedPage page = radFixedDocument.Pages.AddPage();
            FixedContentEditor editor = new FixedContentEditor(page);

            Block block = new Block();

            string custom = "Calibri Light (Headings)";
            string calibri = "Calibri";

            block.TextProperties.TrySetFont(new FontFamily(calibri), FontStyles.Normal, FontWeights.Bold);
            block.InsertText("This text should be bold.This text should be bold.");

            editor.DrawBlock(block);


            string outputPath = @"..\..\..\Export.pdf";
            if (File.Exists(outputPath)) File.Delete(outputPath);
            File.WriteAllBytes(outputPath,pdfProvider.Export(radFixedDocument));
            var p = new Process() { StartInfo = new ProcessStartInfo(outputPath) { UseShellExecute = true } }.Start();
        }

        private static void AddFonts()
        {

            //Regular
            string fontNameRegular = "Calibri";
            string fontFilePathRegular = @"..\..\..\calibri.ttf";

            Telerik.Documents.Core.Fonts.FontFamily fontFamily = new Telerik.Documents.Core.Fonts.FontFamily(fontNameRegular);
            byte[] fontData = File.ReadAllBytes(fontFilePathRegular);
            Telerik.Windows.Documents.Fixed.Model.Fonts.FontsRepository.RegisterFont(fontFamily, FontStyles.Normal, FontWeights.Normal, fontData);

            //Bold
            string fontNameBold = "Calibri";
            string fontFilePathBold = @"..\..\..\calibrib.ttf";
            Telerik.Documents.Core.Fonts.FontFamily fontFamilyBold = new Telerik.Documents.Core.Fonts.FontFamily(fontNameBold);
            byte[] fontDataBold = File.ReadAllBytes(fontFilePathBold);
            Telerik.Windows.Documents.Fixed.Model.Fonts.FontsRepository.RegisterFont(fontFamilyBold, FontStyles.Normal, FontWeights.Bold, fontDataBold);

            //Italic
            string fontNameItalic = "Calibri";
            string fontFilePathItalic = @"..\..\..\calibrii.ttf";
            Telerik.Documents.Core.Fonts.FontFamily fontFamilyItalic = new Telerik.Documents.Core.Fonts.FontFamily(fontNameItalic);
            byte[] fontDataItalic = File.ReadAllBytes(fontFilePathItalic);
            Telerik.Windows.Documents.Fixed.Model.Fonts.FontsRepository.RegisterFont(fontFamilyItalic, FontStyles.Italic, FontWeights.Normal, fontDataItalic);

            //BoldItalic
            string fontNameBoldItalic = "Calibri";
            string fontFilePathBoldItalic = @"..\..\..\calibriz.ttf";
            Telerik.Documents.Core.Fonts.FontFamily fontFamilyBoldItalic = new Telerik.Documents.Core.Fonts.FontFamily(fontNameBoldItalic);
            byte[] fontDataBoldItalic = File.ReadAllBytes(fontFilePathBoldItalic);
            Telerik.Windows.Documents.Fixed.Model.Fonts.FontsRepository.RegisterFont(fontFamilyBoldItalic, FontStyles.Italic, FontWeights.Bold, fontDataBoldItalic);
        }

        public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase
        {
            public override byte[] GetFontData(FontProperties fontProperties)
            {
                string fontFileName = fontProperties.FontFamilyName + ".ttf";
                string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
                string targetPath = Path.Combine(fontFolder, fontFileName);

                DirectoryInfo directory = new DirectoryInfo(fontFolder);
                FileInfo[] fontFiles = directory.GetFiles("*.ttf");
                if (fontFiles.Any(s => s.Name.Equals(fontFileName, StringComparison.InvariantCultureIgnoreCase)))
                {
                    using (FileStream fileStream = File.OpenRead(targetPath))
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            fileStream.CopyTo(memoryStream);
                            return memoryStream.ToArray();
                        }
                    }
                }

                return null;
            }
        }
    }
}
