/// <summary> /// Ajusta los colores del documento a insertar. /// </summary> /// <param name="parentNode">Nodo grupo que se está procesando.</param> /// <param name="colorDestTbl">Tabla de colores resultante.</param> /// <param name="colorToCopyTbl">Tabla de colores del documento a insertar.</param> private void adjustColorRecursive(RtfTreeNode parentNode, RtfColorTable colorDestTbl, RtfColorTable colorToCopyTbl) { if (parentNode != null && parentNode.HasChildNodes()) { for (int iNdIndex = 0; iNdIndex < parentNode.ChildNodes.Count; iNdIndex++) { if (parentNode.ChildNodes[iNdIndex].NodeType == RtfNodeType.Keyword && (parentNode.ChildNodes[iNdIndex].NodeKey == "cf" || parentNode.ChildNodes[iNdIndex].NodeKey == "cb" || parentNode.ChildNodes[iNdIndex].NodeKey == "pncf" || parentNode.ChildNodes[iNdIndex].NodeKey == "brdrcf" || parentNode.ChildNodes[iNdIndex].NodeKey == "cfpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "cbpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "clcfpatraw" || parentNode.ChildNodes[iNdIndex].NodeKey == "clcbpatraw" || parentNode.ChildNodes[iNdIndex].NodeKey == "ulc" || parentNode.ChildNodes[iNdIndex].NodeKey == "chcfpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "chcbpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "highlight" || parentNode.ChildNodes[iNdIndex].NodeKey == "clcbpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "clcfpat") && parentNode.ChildNodes[iNdIndex].HasParameter) { parentNode.ChildNodes[iNdIndex].Parameter = baseRtfDoc.GetColorID(colorDestTbl, colorToCopyTbl[parentNode.ChildNodes[iNdIndex].Parameter]); } adjustColorRecursive(parentNode.ChildNodes[iNdIndex], colorDestTbl, colorToCopyTbl); } } }
/// <summary> /// Ajusta los colores del documento a insertar. /// </summary> /// <param name="docToInsert">Documento a insertar.</param> private void mainAdjustColor(RtfTree docToInsert) { RtfColorTable colorDestTbl = baseRtfDoc.GetColorTable(); RtfColorTable colorToCopyTbl = docToInsert.GetColorTable(); adjustColorRecursive(docToInsert.RootNode, colorDestTbl, colorToCopyTbl); }
public void ColorTableTest() { RtfColorTable colorTable = tree.GetColorTable(); Assert.That(colorTable.Count, Is.EqualTo(3)); Assert.That(colorTable[0], Is.EqualTo(Color.FromArgb(0, 0, 0))); Assert.That(colorTable[1], Is.EqualTo(Color.FromArgb(0, 0, 128))); Assert.That(colorTable[2], Is.EqualTo(Color.FromArgb(255, 0, 0))); Assert.That(colorTable.IndexOf(Color.FromArgb(0, 0, 0)), Is.EqualTo(0)); Assert.That(colorTable.IndexOf(Color.FromArgb(0, 0, 128)), Is.EqualTo(1)); Assert.That(colorTable.IndexOf(Color.FromArgb(255, 0, 0)), Is.EqualTo(2)); }
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; }
/// <summary> /// Devuelve la tabla de colores del documento RTF. /// </summary> /// <returns>Tabla de colores del documento RTF</returns> public RtfColorTable GetColorTable() { RtfColorTable tablaColores = new RtfColorTable(); //Buscamos la tabla de colores en el árbol RtfTreeNode ntc = MainGroup.SelectSingleGroup("colortbl"); //Nodo con la tabla de fuentes //Rellenamos el array de colores int rojo = 0; int verde = 0; int azul = 0; //Añadimos el color por defecto, en este caso el negro. //tabla.Add(Color.FromArgb(rojo,verde,azul)); for (int j = 1; j < ntc.ChildNodes.Count; j++) { RtfTreeNode nodo = ntc.ChildNodes[j]; if (nodo.NodeType == RtfNodeType.Text && nodo.NodeKey.Trim() == ";") { tablaColores.AddColor(Color.FromArgb(rojo, verde, azul)); rojo = 0; verde = 0; azul = 0; } else if (nodo.NodeType == RtfNodeType.Keyword) { switch (nodo.NodeKey) { case "red": rojo = nodo.Parameter; break; case "green": verde = nodo.Parameter; break; case "blue": azul = nodo.Parameter; break; } } } return(tablaColores); }
/// <summary> /// Constructor de la clase RtfDocument. /// </summary> /// <param name="enc">Codificación del documento a generar.</param> public RtfDocument(Encoding enc) { encoding = enc; fontTable = new RtfFontTable(); fontTable.AddFont("Arial"); //Default font colorTable = new RtfColorTable(); colorTable.AddColor(Color.Black); //Default color currentFormat = null; currentParFormat = new RtfParFormat(); docFormat = new RtfDocumentFormat(); mainGroup = new RtfTreeNode(RtfNodeType.Group); InitializeTree(); }
/// <summary> /// Constructor de la clase RtfDocument. /// </summary> /// <param name="path">Ruta del fichero a generar.</param> /// <param name="enc">Codificación del documento a generar.</param> public RtfDocument(string path, Encoding enc) { this.path = path; this.encoding = enc; fontTable = new RtfFontTable(); fontTable.AddFont("Arial"); //Default font colorTable = new RtfColorTable(); colorTable.AddColor(Color.Black); //Default color currentFormat = null; tree = new RtfTree(); mainGroup = new RtfTreeNode(RtfNodeType.Group); InitializeTree(); }
/// <summary> /// Obtiene el código del color pasado como parámetro, insertándolo en la tabla de colores si es necesario. /// </summary> /// <param name="colorDestTbl">Tabla de colores resultante.</param> /// <param name="iColorName">Color buscado.</param> /// <returns></returns> private int getColorID(RtfColorTable colorDestTbl, Color iColorName) { int iExistingColorID; if ((iExistingColorID = colorDestTbl.IndexOf(iColorName)) == -1) { iExistingColorID = colorDestTbl.Count; colorDestTbl.AddColor(iColorName); RtfNodeCollection nodeListToInsert = baseRtfDoc.RootNode.SelectNodes("colortbl"); nodeListToInsert[0].ParentNode.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "red", true, iColorName.R)); nodeListToInsert[0].ParentNode.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "green", true, iColorName.G)); nodeListToInsert[0].ParentNode.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "blue", true, iColorName.B)); nodeListToInsert[0].ParentNode.AppendChild(new RtfTreeNode(RtfNodeType.Text, ";", false, 0)); } return(iExistingColorID); }
/// <summary> /// Obtiene el código del color pasado como parámetro, insertándolo en la tabla de colores si es necesario. /// </summary> /// <param name="colorDestTbl">Tabla de colores resultante.</param> /// <param name="iColorName">Color buscado.</param> /// <returns> /// Table index of requested color. /// Note: If color wasn't present in the table, this method will add it!!! /// </returns> public int GetColorID(RtfColorTable colorDestTbl, Color iColorName) { int iExistingColorID; if ((iExistingColorID = colorDestTbl.IndexOf(iColorName)) == -1) { iExistingColorID = colorDestTbl.Count; colorDestTbl.AddColor(iColorName); RtfTreeNode colorTableGroupNode = MainGroup.SelectSingleGroup("colortbl"); colorTableGroupNode.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "red", true, iColorName.R)); colorTableGroupNode.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "green", true, iColorName.G)); colorTableGroupNode.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "blue", true, iColorName.B)); colorTableGroupNode.AppendChild(new RtfTreeNode(RtfNodeType.Text, ";", false, 0)); } return(iExistingColorID); }
/// <summary> /// Ajusta los colores del documento a insertar. /// </summary> /// <param name="parentNode">Nodo grupo que se está procesando.</param> /// <param name="colorDestTbl">Tabla de colores resultante.</param> /// <param name="colorToCopyTbl">Tabla de colores del documento a insertar.</param> private void adjustColorRecursive(RtfTreeNode parentNode, RtfColorTable colorDestTbl, RtfColorTable colorToCopyTbl) { if (parentNode != null && parentNode.HasChildNodes()) { for (int iNdIndex = 0; iNdIndex < parentNode.ChildNodes.Count; iNdIndex++) { if (parentNode.ChildNodes[iNdIndex].NodeType == RtfNodeType.Keyword && (parentNode.ChildNodes[iNdIndex].NodeKey == "cf" || parentNode.ChildNodes[iNdIndex].NodeKey == "cb" || parentNode.ChildNodes[iNdIndex].NodeKey == "pncf" || parentNode.ChildNodes[iNdIndex].NodeKey == "brdrcf" || parentNode.ChildNodes[iNdIndex].NodeKey == "cfpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "cbpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "clcfpatraw" || parentNode.ChildNodes[iNdIndex].NodeKey == "clcbpatraw" || parentNode.ChildNodes[iNdIndex].NodeKey == "ulc" || parentNode.ChildNodes[iNdIndex].NodeKey == "chcfpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "chcbpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "highlight" || parentNode.ChildNodes[iNdIndex].NodeKey == "clcbpat" || parentNode.ChildNodes[iNdIndex].NodeKey == "clcfpat") && parentNode.ChildNodes[iNdIndex].HasParameter == true) { parentNode.ChildNodes[iNdIndex].Parameter = getColorID(colorDestTbl, colorToCopyTbl[parentNode.ChildNodes[iNdIndex].Parameter]); } adjustColorRecursive(parentNode.ChildNodes[iNdIndex], colorDestTbl, colorToCopyTbl); } } }
/// <summary> /// Devuelve la tabla de colores del documento RTF. /// </summary> /// <returns>Tabla de colores del documento RTF</returns> public RtfColorTable GetColorTable() { RtfColorTable tablaColores = new RtfColorTable(); //Nodo raiz del documento RtfTreeNode root = rootNode; //Grupo principal del documento RtfTreeNode nprin = root.FirstChild; //Buscamos la tabla de colores en el árbol bool enc = false; int i = 0; RtfTreeNode ntc = new RtfTreeNode(); //Nodo con la tabla de fuentes while (!enc && i < nprin.ChildNodes.Count) { if (nprin.ChildNodes[i].NodeType == RtfNodeType.Group && nprin.ChildNodes[i].FirstChild.NodeKey == "colortbl") { enc = true; ntc = nprin.ChildNodes[i]; } i++; } if(null == ntc.ChildNodes) return tablaColores; //Rellenamos el array de colores int rojo = 0; int verde = 0; int azul = 0; //Añadimos el color por defecto, en este caso el negro. //tabla.Add(Color.FromArgb(rojo,verde,azul)); for (int j = 1; j < ntc.ChildNodes.Count; j++) { RtfTreeNode nodo = ntc.ChildNodes[j]; if (nodo.NodeType == RtfNodeType.Text && nodo.NodeKey.Trim() == ";") { tablaColores.AddColor(Color.FromArgb(rojo, verde, azul)); rojo = 0; verde = 0; azul = 0; } else if (nodo.NodeType == RtfNodeType.Keyword) { switch (nodo.NodeKey) { case "red": rojo = nodo.Parameter; break; case "green": verde = nodo.Parameter; break; case "blue": azul = nodo.Parameter; break; } } } return tablaColores; }
/// <summary> /// Devuelve la tabla de colores del documento RTF. /// </summary> /// <returns>Tabla de colores del documento RTF</returns> public RtfColorTable GetColorTable() { RtfColorTable tablaColores = new RtfColorTable(); //Nodo raiz del documento RtfTreeNode root = rootNode; //Grupo principal del documento RtfTreeNode nprin = root.FirstChild; //Buscamos la tabla de colores en el árbol bool enc = false; int i = 0; RtfTreeNode ntc = new RtfTreeNode(); //Nodo con la tabla de fuentes while (!enc && i < nprin.ChildNodes.Count) { if (nprin.ChildNodes[i].NodeType == RtfNodeType.Group && nprin.ChildNodes[i].FirstChild.NodeKey == "colortbl") { enc = true; ntc = nprin.ChildNodes[i]; } i++; } //Rellenamos el array de colores int rojo = 0; int verde = 0; int azul = 0; //Añadimos el color por defecto, en este caso el negro. //tabla.Add(Color.FromArgb(rojo,verde,azul)); for (int j = 1; j < ntc.ChildNodes.Count; j++) { RtfTreeNode nodo = ntc.ChildNodes[j]; if (nodo.NodeType == RtfNodeType.Text && nodo.NodeKey.Trim() == ";") { tablaColores.AddColor(Color.FromArgb(rojo, verde, azul)); rojo = 0; verde = 0; azul = 0; } else if (nodo.NodeType == RtfNodeType.Keyword) { switch (nodo.NodeKey) { case "red": rojo = nodo.Parameter; break; case "green": verde = nodo.Parameter; break; case "blue": azul = nodo.Parameter; break; } } } return(tablaColores); }
/// <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()); }
/// <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> /// Obtiene el código del color pasado como parámetro, insertándolo en la tabla de colores si es necesario. /// </summary> /// <param name="colorDestTbl">Tabla de colores resultante.</param> /// <param name="iColorName">Color buscado.</param> /// <returns></returns> private int getColorID(RtfColorTable colorDestTbl, Color iColorName) { int iExistingColorID; if ((iExistingColorID = colorDestTbl.IndexOf(iColorName)) == -1) { iExistingColorID = colorDestTbl.Count; colorDestTbl.AddColor(iColorName); RtfNodeCollection nodeListToInsert = baseRtfDoc.RootNode.SelectNodes("colortbl"); nodeListToInsert[0].ParentNode.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "red", true, iColorName.R)); nodeListToInsert[0].ParentNode.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "green", true, iColorName.G)); nodeListToInsert[0].ParentNode.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "blue", true, iColorName.B)); nodeListToInsert[0].ParentNode.AppendChild(new RtfTreeNode(RtfNodeType.Text, ";", false, 0)); } return iExistingColorID; }