public static FlowDocument ConvertToFlowDocument(string evml, out Hyperlink[] links, FontFamily font, Brush background, double base_font_size = 12) { HtmlDocument doc = new HtmlDocument(); FlowDocument ret = new FlowDocument(); List <Hyperlink> linklist = new List <Hyperlink>(); ret.FontFamily = font; ret.Background = background; doc.LoadHtml("<html><body>" + evml + "</body></html>"); var body = doc.DocumentNode.SelectSingleNode("//body"); Paragraph paragraph = new Paragraph(); ret.Blocks.Add(paragraph); double fontsizemul = base_font_size / 12.0; StyleOptions base_style = new StyleOptions() { Color = Colors.White, FontWeight = FontWeights.Normal, FontStyle = FontStyles.Normal, TextDecoration = null, FontSize = 12, FontSizeMultiplier = fontsizemul }; base_style.Apply(paragraph); AddFlowNode(body, doc, paragraph.Inlines, linklist, base_style); links = linklist.ToArray(); return(ret); }
private static void AddFlowNode(HtmlNode node, HtmlDocument doc, InlineCollection items, List <Hyperlink> linklist, StyleOptions styles) { foreach (HtmlNode i in node.ChildNodes) { switch (i.NodeType) { case HtmlNodeType.Text: items.Add((Inline)styles.Apply(new Run(i.InnerHtml))); continue; case HtmlNodeType.Element: break; default: continue; } switch (i.Name) { case "font": AddFlowNode(i, doc, items, linklist, new StyleOptions(styles) { Color = ParseColorValue(i.GetAttributeValue("color", ""), styles.Color), FontSize = i.GetAttributeValue("size", 12) }); break; case "b": AddFlowNode(i, doc, items, linklist, new StyleOptions(styles) { FontWeight = FontWeights.Bold }); break; case "u": AddFlowNode(i, doc, items, linklist, new StyleOptions(styles) { TextDecoration = TextDecorations.Underline }); break; case "i": AddFlowNode(i, doc, items, linklist, new StyleOptions(styles) { FontStyle = FontStyles.Italic }); break; case "br": items.Add(new LineBreak()); break; case "a": { var linkstyle = new StyleOptions(styles) { TextDecoration = TextDecorations.Underline }; Hyperlink link = (Hyperlink)linkstyle.Apply(new Hyperlink()); link.NavigateUri = new Uri(i.GetAttributeValue("href", ""), UriKind.RelativeOrAbsolute); items.Add(link); linklist.Add(link); AddFlowNode(i, doc, link.Inlines, linklist, linkstyle); break; } default: AddFlowNode(i, doc, items, linklist, styles); break; } } }