using Telerik.Documents.Core.Fonts;
using Telerik.Windows.Documents.Core.Fonts;
using Telerik.Windows.Documents.Flow.FormatProviders.Doc;
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
using Telerik.Windows.Documents.Flow.Model;

Telerik.Windows.Documents.Extensibility.JpegImageConverterBase jpegImageConverter = new Telerik.Documents.ImageUtils.JpegImageConverter();
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = jpegImageConverter;
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.PredefinedCMapsProvider = new Telerik.Documents.CMapUtils.PredefinedCMapsProvider();
Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new TelerikFontsProvider();
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider;


var docxFile = File.ReadAllBytes("..\\..\\..\\..\\Problem_with_footer.docx");
DocxFormatProvider docxProvider = new DocxFormatProvider();
RadFlowDocument docxDocument = docxProvider.Import(docxFile);
var pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
var newFile = pdfProvider.Export(docxDocument);
using (FileStream stream = new FileStream("..\\..\\..\\..\\footer.pdf", FileMode.Create, FileAccess.Write, FileShare.Read))
{
    stream.Write(newFile, 0, newFile.Length);
}

var docFile = File.ReadAllBytes("..\\..\\..\\..\\Changes_Signature_position.doc");
DocFormatProvider docProvider = new DocFormatProvider();
RadFlowDocument document = docProvider.Import(docFile);
newFile = pdfProvider.Export(document);

using (FileStream stream = new FileStream("..\\..\\..\\..\\signature.pdf", FileMode.Create, FileAccess.Write, FileShare.Read))
{
    stream.Write(newFile, 0, newFile.Length);
}

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

        if (fontProperties.FontFamilyName == "Calibri")
        {
            if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold)
            {
                fontFileName = $"{fontProperties.FontFamilyName}z.ttf";
            }
            else if (fontProperties.FontStyle == FontStyles.Italic)
            {
                fontFileName = $"{fontProperties.FontFamilyName}i.ttf";
            }
            else if (fontProperties.FontWeight == FontWeights.Normal)
            {
                fontFileName = fontProperties.FontFamilyName + ".ttf";
            }
            else if (fontProperties.FontWeight == FontWeights.Bold)
            {
                fontFileName = $"{fontProperties.FontFamilyName}b.ttf";
            }
        }

        DirectoryInfo directory = new DirectoryInfo(fontFolder);
        FileInfo[] fontFiles = directory.GetFiles("*.ttf");

        var fontFile = fontFiles.FirstOrDefault(f => f.Name.Equals(fontFileName, StringComparison.InvariantCultureIgnoreCase));
        if (fontFile != null)
        {
            var targetPath = fontFile.FullName;
            using (FileStream fileStream = File.OpenRead(targetPath))
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    fileStream.CopyTo(memoryStream);
                    return memoryStream.ToArray();
                }
            }
        }

        return null;
    }
}