Пример #1
0
        public void TestTable()
        {
            var xaml = Documents.Table_Xaml;
            var html = HtmlFromXamlConverter.ConvertXamlToHtml(xaml, new HtmlFromXamlContext(new HtmlFromXamlDocumentOptions()));

            Assert.AreEqual(Documents.Table_HtmlFromXaml, html);
        }
Пример #2
0
 public string ConvertXamlToHtml(string xamlText)
 {
     return(HtmlFromXamlConverter.ConvertXamlToHtml
            (
                xamlText,
                false));
 }
Пример #3
0
        /// <summary>
        /// Formats a SupplementalInfo for browser display as follows:
        ///  - if null, returns null.
        ///  - if it is a XAML FlowDocument, it is converted to HTML.
        ///  - any CRLF linefeed characters in the string are converted to <br /> tags.
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        private string FormatSupplementalInfo(string supp)
        {
            if (supp == null)
            {
                return("None");
            }

            if (supp.StartsWith("<FlowDocument"))
            {
                string html = HtmlFromXamlConverter.ConvertXamlToHtml(supp);
                return(html.Replace("margin:0 0 0 0;", "").Replace("padding:0 0 0 0;", ""));
            }

            // Convert any linefeed characters to HTML line break tags
            const string pattern = "</?\\w+((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\">\\s]+))?)+\\s*|\\s*)/?>";
            Regex        reg     = new Regex(pattern);
            var          matches = reg.Matches(supp);

            if (matches.Count > 0)
            {
                return(supp);
            }

            return(supp.Replace("\r\n", "<br/>").Replace("\n", "<br/>").Replace("\r", "<br/>"));
        }
Пример #4
0
        public void TestMultipleStyles()
        {
            var xaml = Documents.MultipleFontStyles_Xaml;
            var html = HtmlFromXamlConverter.ConvertXamlToHtml(xaml, new HtmlFromXamlContext(new HtmlFromXamlDocumentOptions()));

            Assert.AreEqual(string.Format(Documents.FullHtml_Format, Documents.MultipleFontStyles_HtmlFromXaml), html);
        }
Пример #5
0
        public void TestParagraphRun()
        {
            var xaml = Documents.TestParagraph_Xaml;
            var html = HtmlFromXamlConverter.ConvertXamlToHtml(xaml, new HtmlFromXamlContext(new HtmlFromXamlDocumentOptions()));

            Assert.AreEqual(string.Format(Documents.FullHtml_Format, Documents.TestParagraph_Html), html);
        }
        public static void SetBoundDocument(DependencyObject dp, string value)
        {
            var xaml = value;
            var html = HtmlFromXamlConverter.ConvertXamlToHtml(xaml, false);

            dp.SetValue(BoundDocument, html);
        }
Пример #7
0
        public static void SendData(RichTextBoxUtil richTextBoxUtil)
        {
            string str    = richTextBoxUtil.GetRichTextBoxToString;
            string result = HtmlFromXamlConverter.ConvertXamlToHtml(str);

            WebSocketUtil.WebSockets.Send(new ContUtil(result).ContParam.ToString());
            //int width = CommonUtil.GetRichTextBoxWidth(richTextBoxUtil.GetRichTextBoxToString, richTextBoxUtil.GetRichTextBoxCont);
        }
Пример #8
0
        public static void UnLoadRichTextbox(RichTextBox box, object context, bool encode)
        {
            if (null == context)
            {
                return;
            }

            TextRange tr = new TextRange(box.Document.ContentStart, box.Document.ContentEnd);

            using (MemoryStream ms = new MemoryStream())
            {
                tr.Save(ms, DataFormats.Xaml);

                // Need to explictly create this to force a byte-order-mark
                UTF8Encoding encoder  = new UTF8Encoding(true, true);
                string       xamlText = encoder.GetString(ms.ToArray());
                var          html     = HtmlFromXamlConverter.ConvertXamlToHtml(xamlText, false);

                // Test to see if there's anything in the HTML
                string striped = Regex.Replace(html, @"<(.|\n)*?>", "").Trim();
                if (0 == striped.Length)
                {
                    html = String.Empty;
                }

                if (context is XmlElement)
                {
                    var contextElement = context as XmlElement;
                    var oldXml         = contextElement.InnerText;
                    if (oldXml == html)
                    {
                        return;
                    }
                }

                IEnumerable <XmlNode> xmlNodes = Utils.GetXmlDataContext(context);

                // Load from encoded HTML
                if (null == xmlNodes)
                {
                    return;
                }

                foreach (XmlNode node in xmlNodes)
                {
                    if (encode)
                    {
                        node.InnerText = html.Trim(); // encodes automagically
                    }
                    else
                    {
                        node.InnerXml = html.Trim();
                    }

                    break;
                }
            }
        }
Пример #9
0
        public void TestParagraphRunSpan()
        {
            var xaml = Documents.TestParagraph_Xaml;
            var html = HtmlFromXamlConverter.ConvertXamlToHtml(xaml, new HtmlFromXamlContext(new HtmlFromXamlDocumentOptions {
                OuterElement = "", InnerElement = "span"
            }));

            Assert.AreEqual($"<span>{Documents.TestParagraph_Html}</span>", html);
        }
Пример #10
0
        public static string ToHtmlString(this FlowDocument doc)
        {
            var xamlString = doc.ToXamlString();

            if (string.IsNullOrEmpty(xamlString))
            {
                return(null);
            }
            return(HtmlFromXamlConverter.ConvertXamlToHtml(xamlString, false));
        }
Пример #11
0
        public string GetText(FlowDocument document)
        {
            var tr = new TextRange(document.ContentStart, document.ContentEnd);

            using (var ms = new MemoryStream())
            {
                tr.Save(ms, DataFormats.Xaml);
                return(HtmlFromXamlConverter.ConvertXamlToHtml(Encoding.Default.GetString(ms.ToArray())));
            }
        }
Пример #12
0
        public string GetText(FlowDocument document)
        {
            TextRange tr = new TextRange(document.ContentStart, document.ContentEnd);

            using (MemoryStream ms = new MemoryStream())
            {
                tr.Save(ms, DataFormats.Xaml);
                var xaml = Encoding.UTF8.GetString(ms.ToArray());
                return(HtmlFromXamlConverter.ConvertXamlToHtml(xaml, false));
            }
        }
Пример #13
0
        public void TestMultipleStylesToPlain()
        {
            var xaml    = Documents.MultipleFontStyles_Xaml;
            var context = new HtmlFromXamlContext(new HtmlFromXamlDocumentOptions());

            texts.Clear();
            context.OnWriteText = ReadText;
            var html         = HtmlFromXamlConverter.ConvertXamlToHtml(xaml, context);
            var expectedList = new List <string> {
                "word1 ", "word2"
            };

            CollectionAssert.AreEqual(expectedList, texts);
        }
Пример #14
0
        public static void SetBoundDocument(DependencyObject dp, FlowDocument value)
        {
            // I don't want to convert xaml to html now. I want to user flowdocument format directly.
            // For other platform, we will convert flowdocument to html on player side.
            // Set Directly
#if _SAVE_CONTENT_AS_XAML_
            FlowDocument document = value as FlowDocument;
            dp.SetValue(BoundDocument, HtmlFromXamlConverter.SerializeFlowDocument(document));
#else
            // Convert then set.
            var xaml = value;
            var html = HtmlFromXamlConverter.ConvertXamlToHtml(value);
            dp.SetValue(BoundDocument, html);
#endif
        }
Пример #15
0
        /// <summary>
        /// Formats a SupplementalInfo for browser display as follows:
        ///  - if null, returns null.
        ///  - if it is a XAML FlowDocument, it is converted to HTML.
        ///  - any CRLF linefeed characters in the string are converted to <br /> tags.
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        private string FormatSupplementalInfo(string supp)
        {
            if (supp == null)
            {
                return("None");
            }

            if (supp.StartsWith("<FlowDocument"))
            {
                string html = HtmlFromXamlConverter.ConvertXamlToHtml(supp);
                return(html.Replace("margin:0 0 0 0;", "").Replace("padding:0 0 0 0;", ""));
            }

            // Convert any linefeed characters to HTML line break tags
            return(supp.Replace("\r\n", "<br/>").Replace("\n", "<br/>").Replace("\r", "<br/>"));
        }
Пример #16
0
        private static void ConvertRtfInSTAThread(object rtfText)
        {
            try
            {
                var richTextBox = new RichTextBox();
                if (string.IsNullOrEmpty((string)rtfText))
                {
                    _result = "";
                    return;
                }
                var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                using (var rtfMemoryStream = new MemoryStream())
                {
                    using (var rtfStreamWriter = new StreamWriter(rtfMemoryStream))
                    {
                        rtfStreamWriter.Write(rtfText);
                        rtfStreamWriter.Flush();
                        rtfMemoryStream.Seek(0, SeekOrigin.Begin);
                        textRange.Load(rtfMemoryStream, DataFormats.Rtf);
                    }
                }

                string xaml;
                using (var rtfMemoryStream = new MemoryStream())
                {
                    textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                    textRange.Save(rtfMemoryStream, DataFormats.Xaml);
                    rtfMemoryStream.Seek(0, SeekOrigin.Begin);
                    using (var rtfStreamReader = new StreamReader(rtfMemoryStream))
                    {
                        xaml = rtfStreamReader.ReadToEnd();
                    }
                }

                _result = HtmlFromXamlConverter.ConvertXamlToHtml(xaml, false);
            }
            catch (Exception e)
            {
                _exception = e;
                _result    = null;
            }
        }
Пример #17
0
        public string GetText(FlowDocument document)
        {
            string       html = null;
            TextRange    tr   = new TextRange(document.ContentStart, document.ContentEnd);
            MemoryStream ms   = new MemoryStream();

            try
            {
                tr.Save(ms, DataFormats.Xaml);
                html = HtmlFromXamlConverter.ConvertXamlToHtml(Encoding.Default.GetString(ms.ToArray()));
            }
            finally
            {
                if (ms != null)
                {
                    ((IDisposable)ms).Dispose();
                }
            }
            return(html);
        }
Пример #18
0
        public string GetFlowDoc(string type, int id)
        {
            string flowDoc = string.Empty;

            using (var db = new CSET_Context())
            {
                if (type.ToLower() == "cat")
                {
                    flowDoc = db.CATALOG_RECOMMENDATIONS_DATA.FirstOrDefault(d => d.Data_Id == id).Flow_Document;
                }

                if (type.ToLower() == "proc")
                {
                    flowDoc = db.PROCUREMENT_LANGUAGE_DATA.FirstOrDefault(d => d.Procurement_Id == id).Flow_Document;
                }
            }



            if (flowDoc == null)
            {
                return("None");
            }

            if (flowDoc.StartsWith("<FlowDocument"))
            {
                string html = HtmlFromXamlConverter.ConvertXamlToHtml(flowDoc);

                // add some style to the first <p> element to make it look like a title
                if (html.StartsWith("<p>"))
                {
                    html = "<p style=\"font-size: 150%; font-weight: bold;\">" + html.Substring(3);
                }

                return(html.Replace("margin:0 0 0 0;", "").Replace("padding:0 0 0 0;", ""));
            }

            return(string.Empty);
        }