/// <summary> /// Ajusta las fuentes del documento a insertar. /// </summary> /// <param name="docToInsert">Documento a insertar.</param> private void mainAdjustFont(RtfTree docToInsert) { RtfFontTable fontDestTbl = baseRtfDoc.GetFontTable(); RtfFontTable fontToCopyTbl = docToInsert.GetFontTable(); adjustFontRecursive(docToInsert.RootNode, fontDestTbl, fontToCopyTbl); }
public void FontTableTest() { RtfFontTable fontTable = tree.GetFontTable(); Assert.That(fontTable.Count, Is.EqualTo(3)); Assert.That(fontTable[0], Is.EqualTo("Times New Roman")); Assert.That(fontTable[1], Is.EqualTo("Arial")); Assert.That(fontTable[2], Is.EqualTo("Arial")); Assert.That(fontTable.IndexOf("Times New Roman"), Is.EqualTo(0)); Assert.That(fontTable.IndexOf("Arial"), Is.EqualTo(1)); Assert.That(fontTable.IndexOf("nofont"), Is.EqualTo(-1)); }
public RtfDocument(string path, Encoding enc) : this(enc) { RtfTree tree = new RtfTree(); //Load and parse RTF document tree.LoadRtfFile(path); this.fontTable = tree.GetFontTable(); this.colorTable = tree.GetColorTable(); this.encoding = tree.GetEncoding(); this.mainGroup = tree.MainGroup; }
public string traducir() { string res = ""; //Se extrae la tabla de fuentes del documento tFuentes = tree.GetFontTable(); //Se extrae la tabla de colores del documento tColores = tree.GetColorTable(); //Se lanza el proceso de traducción del documento a formato HTML res = traducirTexto(); //Se devuelve el resultado del proceso return(res); }
/// <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()); }