private void ConvertData(object sender, ClipboardMonitor.ClipboardChangedEventArgs e) //private void ConvertData(object sender, System.Windows.Automation.AutomationFocusChangedEventArgs e) { //Console.WriteLine("ConvertData"); iData = Clipboard.GetDataObject(); if (!iData.GetDataPresent(DataFormats.Rtf)) { return; } reset(); rtfText = iData.GetData(DataFormats.Rtf).ToString(); //Console.WriteLine(rtfText); RtfTree tree = new RtfTree(); tree.LoadRtfText(rtfText); //tree = _ConvertMMath(tree); tree = _ConvertObject(tree); tree = _ConvertPict(tree); //Console.WriteLine(tree.Rtf); string html = RtfToHtmlConverter.ConvertRtfToHtml(tree.Rtf); //Console.WriteLine(html); string regularExpressionPattern = string.Format(@"\[{0}\](.*?)\[\/{1}\]", REPLACETAG, REPLACETAG); Regex regex = new Regex(regularExpressionPattern, RegexOptions.Singleline); MatchCollection collection = regex.Matches(html); foreach (Match m in collection) { string Matchstr = m.Groups[0].Value; string Rid = m.Groups[1].Value; int index = int.Parse(Rid); if (index < rtf_replace.Count) { IReplace equationObj = (IReplace)rtf_replace[index]; string s = equationObj.toHtml(); //Console.WriteLine(m.Groups[0].Value + "\n\r"); //Console.WriteLine(s + "\n\r"); html = html.Replace(Matchstr, s); } } //Console.WriteLine(html + "\n\r"); if (DataConverted != null) { DataConverted(this, new DataConvertEventArgs(rtfText, html)); } }
public void LoadSimpleDocMergeSpecialFromString() { RtfTree tree = new RtfTree(); tree.MergeSpecialCharacters = true; StreamReader sr = new StreamReader("..\\..\\testdocs\\testdoc1.rtf"); string strDoc = sr.ReadToEnd(); sr.Close(); int res = tree.LoadRtfText(strDoc); sr = new StreamReader("..\\..\\testdocs\\result1-3.txt"); string strTree1 = sr.ReadToEnd(); sr.Close(); sr = new StreamReader("..\\..\\testdocs\\result1-4.txt"); string strTree2 = sr.ReadToEnd(); sr.Close(); sr = new StreamReader("..\\..\\testdocs\\rtf1.txt"); string rtf1 = sr.ReadToEnd(); sr.Close(); sr = new StreamReader("..\\..\\testdocs\\text1.txt"); string text1 = sr.ReadToEnd(); sr.Close(); Assert.That(res, Is.EqualTo(0)); Assert.That(tree.MergeSpecialCharacters, Is.True); Assert.That(tree.ToString(), Is.EqualTo(strTree1)); Assert.That(tree.ToStringEx(), Is.EqualTo(strTree2)); Assert.That(tree.Rtf, Is.EqualTo(rtf1)); Assert.That(tree.Text, Is.EqualTo(text1)); }
public static void FormRTFDocument(string filename, ReportTable table) { RtfTree tree = new RtfTree(); string rtfBase = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 Times New Roman;}}\fs24"; tree.LoadRtfText(rtfBase); //Load an RTF document from a file RtfTreeNode grp = new RtfTreeNode(RtfNodeType.Group); foreach (ReportRow row in table.Rows) { grp.AddTableRow(row.Cells); } tree.RootNode.FirstChild.AppendChild(grp); tree.SaveRtf(filename); //Get and print RTF code //Console.Write(tree.ToStringEx()); }
/// <summary> /// Convierte una cadena de código RTF a formato HTML /// </summary> public string Convert(string rtf) { //Generar arbol DOM RtfTree rtfTree = new RtfTree(); rtfTree.LoadRtfText(rtf); //Inicializar variables empleadas _builder = new StringBuilder(); _htmlFormat = new Format(); _currentFormat = new Format(); _fontTable = rtfTree.GetFontTable(); _colorTable = rtfTree.GetColorTable(); //Buscar el inicio del contenido visible del documento int inicio; for (inicio = 0; inicio < rtfTree.RootNode.FirstChild.ChildNodes.Count; inicio++) { if (rtfTree.RootNode.FirstChild.ChildNodes[inicio].NodeKey == "pard") { break; } } //Procesar todos los nodos visibles ProcessChildNodes(rtfTree.RootNode.FirstChild.ChildNodes, inicio); //Cerrar etiquetas pendientes _currentFormat.Reset(); WriteText(string.Empty); //Arreglar HTML //Arreglar listas Regex repairList = new Regex("<span [^>]*>·</span><span style=\"([^\"]*)\">(.*?)<br\\s+/><" + "/span>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant); foreach (Match match in repairList.Matches(_builder.ToString())) { _builder.Replace(match.Value, string.Format("<li style=\"{0}\">{1}</li>", match.Groups[1].Value, match.Groups[2].Value)); } Regex repairUl = new Regex("(?<!</li>)<li", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); foreach (Match match in repairUl.Matches(_builder.ToString())) { _builder.Insert(match.Index, "<ul>"); } repairUl = new Regex("/li>(?!<li)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); foreach (Match match in repairUl.Matches(_builder.ToString())) { _builder.Insert(match.Index + match.Length, "</ul>"); } //Generar párrafos (cada 2 <br /><br /> se cambiará por un <p>) if (AutoParagraph) { string[] partes = _builder.ToString().Split(new[] { "<br /><br />" }, StringSplitOptions.RemoveEmptyEntries); _builder = new StringBuilder(_builder.Length + 7 * partes.Length); foreach (string parte in partes) { _builder.Append("<p>"); _builder.Append(parte); _builder.Append("</p>"); } } return(EscapeHtmlEntities ? HtmlEntities.Encode(_builder.ToString()) : _builder.ToString()); }
/// <summary> /// Convierte una cadena de código RTF a formato HTML /// </summary> public string Convert(string rtf) { //Generar arbol DOM RtfTree rtfTree = new RtfTree(); rtfTree.LoadRtfText(rtf); //Inicializar variables empleadas _builder = new StringBuilder(); _htmlFormat = new Format(); _currentFormat = new Format(); _fontTable = rtfTree.GetFontTable(); _colorTable = rtfTree.GetColorTable(); //Buscar el inicio del contenido visible del documento int inicio; for (inicio = 0; inicio < rtfTree.RootNode.FirstChild.ChildNodes.Count; inicio++) { if (rtfTree.RootNode.FirstChild.ChildNodes[inicio].NodeKey == "pard") break; } //Procesar todos los nodos visibles ProcessChildNodes(rtfTree.RootNode.FirstChild.ChildNodes, inicio); //Cerrar etiquetas pendientes _currentFormat.Reset(); WriteText(string.Empty); //Arreglar HTML //Arreglar listas Regex repairList = new Regex("<span [^>]*>·</span><span style=\"([^\"]*)\">(.*?)<br\\s+/><" + "/span>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant); foreach (Match match in repairList.Matches(_builder.ToString())) { _builder.Replace(match.Value, string.Format("<li style=\"{0}\">{1}</li>", match.Groups[1].Value, match.Groups[2].Value)); } Regex repairUl = new Regex("(?<!</li>)<li", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); foreach (Match match in repairUl.Matches(_builder.ToString())) { _builder.Insert(match.Index, "<ul>"); } repairUl = new Regex("/li>(?!<li)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); foreach (Match match in repairUl.Matches(_builder.ToString())) { _builder.Insert(match.Index + match.Length, "</ul>"); } //Generar párrafos (cada 2 <br /><br /> se cambiará por un <p>) if (AutoParagraph) { string[] partes = _builder.ToString().Split(new[] { "<br /><br />" }, StringSplitOptions.RemoveEmptyEntries); _builder = new StringBuilder(_builder.Length + 7 * partes.Length); foreach (string parte in partes) { _builder.Append("<p>"); _builder.Append(parte); _builder.Append("</p>"); } } return EscapeHtmlEntities ? HtmlEntities.Encode(_builder.ToString()) : _builder.ToString(); }
/// <summary> /// Convierte una cadena de código RTF a formato HTML /// </summary> public string Convert(string rtf) { //Console.WriteLine(rtf); //Generar arbol DOM RtfTree rtfTree = new RtfTree(); rtfTree.LoadRtfText(rtf); Console.WriteLine(rtfTree.ToStringEx()); //Inicializar variables empleadas _builder = new StringBuilder(); _htmlFormat = new Format(); this._builder.Append("<!DOCTYPE html><html><body> "); _formatList = new List <Format>(); _formatList.Add(new Format()); try { _fontTable = rtfTree.GetFontTable(); } catch { } try { _colorTable = rtfTree.GetColorTable(); } catch { } int inicio; for (inicio = 0; inicio < rtfTree.RootNode.FirstChild.ChildNodes.Count; inicio++) { if (rtfTree.RootNode.FirstChild.ChildNodes[inicio].NodeKey == "pard") { break; } } //Procesar todos los nodos visibles TransformChildNodes(rtfTree.RootNode.FirstChild.ChildNodes, inicio); ProcessChildNodes(rtfTree.RootNode.FirstChild.ChildNodes, inicio); //Cerrar etiquetas pendientes _formatList.Last().Reset(); WriteText(string.Empty); Regex repairList = new Regex("<span [^>]*>·</span><span style=\"([^\"]*)\">(.*?)<br\\s+/><" + "/span>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant); //foreach (Match match in repairList.Matches(_builder.ToString())) //{ // _builder.Replace(match.Value, string.Format("<li style=\"{0}\">{1}</li>", match.Groups[1].Value, match.Groups[2].Value)); //} //Regex repairUl = new Regex("(?<!</li>)<li", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); //foreach (Match match in repairUl.Matches(_builder.ToString())) //{ // _builder.Insert(match.Index, "<ul>"); //} //repairUl = new Regex("/li>(?!<li)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); //foreach (Match match in repairUl.Matches(_builder.ToString())) //{ // _builder.Insert(match.Index + match.Length, "</ul>"); //} if (AutoParagraph) { string[] partes = _builder.ToString().Split(new[] { "<br /><br />" }, StringSplitOptions.RemoveEmptyEntries); _builder = new StringBuilder(_builder.Length + 7 * partes.Length); foreach (string parte in partes) { _builder.Append("<p>"); _builder.Append(parte); _builder.Append("</p>"); } } this._builder.Append("</body></html>"); //Console.WriteLine(_builder.ToString()); return(EscapeHtmlEntities ? HtmlEntities.Encode(_builder.ToString()) : _builder.ToString()); }