Example #1
0
        //async Task<BitmapImage> renderImageWithTemplateContextAsync()
        //{
        //    //var filename = System.IO.Path.Combine(_config.SlidesConfiguration.TemplatePath, template);
        //    if (Template == null || TemplateFields == null)
        //    {
        //        return null;
        //    }
        //    if (!File.Exists(Template) && File.Exists(Path.Combine(_config.SlidesConfiguration.TemplatePath, Template)))
        //    {
        //        Template = Path.Combine(_config.SlidesConfiguration.TemplatePath, Template);
        //    }
        //    else if (!File.Exists(Template))
        //    {
        //        return null;
        //    }

        //    var templateHtml = File.ReadAllText(Template);

        //    foreach (Match m in Regex.Matches(templateHtml, pattern))
        //    {
        //        var variable = m.Groups[1].Value;
        //        var matchedValue = m.Value;
        //        var replaceField = TemplateFields.FirstOrDefault(x => x.Title == variable);
        //        var replaceValue = "";
        //        if (replaceField != null)
        //            replaceValue = replaceField.Value;
        //        templateHtml = templateHtml.Replace(matchedValue, replaceValue);
        //    }

        //    // Replace ##index## with offseted Values
        //    const string indexpattern = "##(.*?)##";
        //    foreach (Match m in Regex.Matches(templateHtml, indexpattern))
        //    {
        //        var variable = Convert.ToInt32(m.Groups[1].Value);
        //        var matchedValue = m.Value;
        //        var replaceValue = (IndexOffset + variable).ToString();
        //        templateHtml = templateHtml.Replace(matchedValue, replaceValue);
        //    }

        //    // Background Management
        //    const string bgpattern = "\"backgroundimage\": ?\"(?<image>.*?)\"";
        //    Match mbg = Regex.Match(templateHtml, bgpattern);
        //    if (mbg.Success && !String.IsNullOrEmpty(Background))
        //    {
        //        var variable = mbg.Groups[1].Value;
        //        var filepath = Background.Replace("\\", "/"); //"file://" +
        //        templateHtml = templateHtml.Replace(variable, filepath);
        //    }

        //    // Render the image
        //    var rendered = HTMLRenderer.RenderHtml(templateHtml, _config.SlidesConfiguration.TemplatePath);

        //    return rendered;
        //}

        private string RenderImageWithTemplateContext(string generationProperties)
        {
            //var filename = System.IO.Path.Combine(_config.SlidesConfiguration.TemplatePath, template);
            if (Template == null || TemplateFields == null)
            {
                return(null);
            }
            if (!File.Exists(Template) && File.Exists(Path.Combine(Config.SlidesConfiguration.TemplatePath, Template)))
            {
                Template = Path.Combine(Config.SlidesConfiguration.TemplatePath, Template);
            }
            else if (!File.Exists(Template) && File.Exists(Path.Combine(Config.SlidesConfiguration.TemplatePath, Path.GetFileName(Template))))
            {
                Template = Path.Combine(Config.SlidesConfiguration.TemplatePath, Path.GetFileName(Template));
            }
            else if (!File.Exists(Template))
            {
                return(null);
            }

            var templateHtml = File.ReadAllText(Template);

            // Pattern used to replace template variables
            string pattern = "@@(?<variable>.*?)(=(?<default>.*))?@@";

            foreach (Match m in Regex.Matches(templateHtml, pattern))
            {
                var variable     = m.Groups["variable"].Value;
                var matchedValue = m.Value;
                var replaceField = TemplateFields.FirstOrDefault(x => x.Title == variable);
                var replaceValue = "";
                if (replaceField != null)
                {
                    replaceValue = replaceField.Value;
                }
                templateHtml = templateHtml.Replace(matchedValue, replaceValue);
            }

            // Replace ##index## with offseted Values
            const string indexpattern = "##(.*?)##";

            foreach (Match m in Regex.Matches(templateHtml, indexpattern))
            {
                var variable     = Convert.ToInt32(m.Groups[1].Value);
                var matchedValue = m.Value;
                var replaceValue = (IndexOffset + variable).ToString();
                templateHtml = templateHtml.Replace(matchedValue, replaceValue);
            }

            // Background Management
            const string bgpattern = "\"backgroundimage\": ?\"(?<image>.*?)\"";
            Match        mbg       = Regex.Match(templateHtml, bgpattern);

            if (mbg.Success && !String.IsNullOrEmpty(Background))
            {
                var variable = mbg.Groups[1].Value;
                var filepath = Background.Replace("\\", "/"); //"file://" +
                var localRex = new Regex(@"^\w:\/+", RegexOptions.IgnoreCase);
                if (localRex.IsMatch(filepath))
                {
                    // Add Local file:/// prefix for path with Drive
                    filepath = "file:///" + filepath;
                }
                templateHtml = templateHtml.Replace(variable, filepath);
            }

            // Render the image
            var rendered = HTMLRenderer.RenderHtml(html: templateHtml,
                                                   pathToWorkingDir: Config.SlidesConfiguration.TemplatePath,
                                                   zoomFactorOptions: generationProperties);

            return(rendered);
        }
        //async Task<BitmapImage> renderImageWithTemplateContextAsync()
        //{
        //    //var filename = System.IO.Path.Combine(_config.SlidesConfiguration.TemplatePath, template);
        //    if (Template == null || TemplateFields == null)
        //    {
        //        return null;
        //    }
        //    if (!File.Exists(Template) && File.Exists(Path.Combine(_config.SlidesConfiguration.TemplatePath, Template)))
        //    {
        //        Template = Path.Combine(_config.SlidesConfiguration.TemplatePath, Template);
        //    }
        //    else if (!File.Exists(Template))
        //    {
        //        return null;
        //    }

        //    var templateHtml = File.ReadAllText(Template);

        //    // Replace @@values@@ with context Values
        //    const string pattern = "@@(.*?)@@";
        //    foreach (Match m in Regex.Matches(templateHtml, pattern))
        //    {
        //        var variable = m.Groups[1].Value;
        //        var matchedValue = m.Value;
        //        var replaceField = TemplateFields.FirstOrDefault(x => x.Title == variable);
        //        var replaceValue = "";
        //        if (replaceField != null)
        //            replaceValue = replaceField.Value;
        //        templateHtml = templateHtml.Replace(matchedValue, replaceValue);
        //    }

        //    // Replace ##index## with offseted Values
        //    const string indexpattern = "##(.*?)##";
        //    foreach (Match m in Regex.Matches(templateHtml, indexpattern))
        //    {
        //        var variable = Convert.ToInt32(m.Groups[1].Value);
        //        var matchedValue = m.Value;
        //        var replaceValue = (IndexOffset + variable).ToString();
        //        templateHtml = templateHtml.Replace(matchedValue, replaceValue);
        //    }

        //    // Background Management
        //    const string bgpattern = "\"backgroundimage\": ?\"(?<image>.*?)\"";
        //    Match mbg = Regex.Match(templateHtml, bgpattern);
        //    if (mbg.Success && !String.IsNullOrEmpty(Background))
        //    {
        //        var variable = mbg.Groups[1].Value;
        //        var filepath = Background.Replace("\\", "/"); //"file://" +
        //        templateHtml = templateHtml.Replace(variable, filepath);
        //    }

        //    // Render the image
        //    var rendered = HTMLRenderer.RenderHtml(templateHtml, _config.SlidesConfiguration.TemplatePath);

        //    return rendered;
        //}

        private BitmapImage RenderImageWithTemplateContext()
        {
            //var filename = System.IO.Path.Combine(_config.SlidesConfiguration.TemplatePath, template);
            if (Template == null || TemplateFields == null)
            {
                return(null);
            }
            if (!File.Exists(Template) && File.Exists(Path.Combine(Config.SlidesConfiguration.TemplatePath, Template)))
            {
                Template = Path.Combine(Config.SlidesConfiguration.TemplatePath, Template);
            }
            else if (!File.Exists(Template) && File.Exists(Path.Combine(Config.SlidesConfiguration.TemplatePath, Path.GetFileName(Template))))
            {
                Template = Path.Combine(Config.SlidesConfiguration.TemplatePath, Path.GetFileName(Template));
            }
            else if (!File.Exists(Template))
            {
                return(null);
            }

            var templateHtml = File.ReadAllText(Template);

            // Replace @@values@@ with context Values
            const string pattern = "@@(.*?)@@";

            foreach (Match m in Regex.Matches(templateHtml, pattern))
            {
                var variable     = m.Groups[1].Value;
                var matchedValue = m.Value;
                var replaceField = TemplateFields.FirstOrDefault(x => x.Title == variable);
                var replaceValue = "";
                if (replaceField != null)
                {
                    replaceValue = replaceField.Value;
                }
                templateHtml = templateHtml.Replace(matchedValue, replaceValue);
            }

            // Replace ##index## with offseted Values
            const string indexpattern = "##(.*?)##";

            foreach (Match m in Regex.Matches(templateHtml, indexpattern))
            {
                var variable     = Convert.ToInt32(m.Groups[1].Value);
                var matchedValue = m.Value;
                var replaceValue = (IndexOffset + variable).ToString();
                templateHtml = templateHtml.Replace(matchedValue, replaceValue);
            }

            // Background Management
            const string bgpattern = "\"backgroundimage\": ?\"(?<image>.*?)\"";
            Match        mbg       = Regex.Match(templateHtml, bgpattern);

            if (mbg.Success && !String.IsNullOrEmpty(Background))
            {
                var variable = mbg.Groups[1].Value;
                var filepath = Background.Replace("\\", "/"); //"file://" +
                templateHtml = templateHtml.Replace(variable, filepath);
            }

            // Render the image
            var rendered = HTMLRenderer.RenderHtml(templateHtml, Config.SlidesConfiguration.TemplatePath);

            return(rendered);
        }