/// <summary> /// Devuelve la tabla de fuentes del documento RTF. /// </summary> /// <returns>Tabla de fuentes del documento RTF</returns> public RtfFontTable GetFontTable() { RtfFontTable tablaFuentes = new RtfFontTable(); //Nodo raiz del documento RtfTreeNode root = rootNode; //Grupo principal del documento RtfTreeNode nprin = root.FirstChild; //Buscamos la tabla de fuentes en el árbol bool enc = false; int i = 0; RtfTreeNode ntf = 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 == "fonttbl") { enc = true; ntf = nprin.ChildNodes[i]; } i++; } //Rellenamos el array de fuentes for (int j = 1; j < ntf.ChildNodes.Count; j++) { RtfTreeNode fuente = ntf.ChildNodes[j]; int indiceFuente = -1; string nombreFuente = null; int codePage = Encoding.Default.CodePage; foreach (RtfTreeNode nodo in fuente.ChildNodes) { if (nodo.NodeType == RtfNodeType.Keyword) { if (nodo.NodeKey == "f") { indiceFuente = nodo.Parameter; } else if (nodo.NodeKey == "fcharset") { codePage = CharSetConvertor.ToCodePage(nodo.Parameter, codePage); } } else if (nodo.NodeType == RtfNodeType.Text) { nombreFuente = nodo.NodeKey.Substring(0, nodo.NodeKey.Length - 1); } } tablaFuentes.AddFont(indiceFuente, new RtfFont(nombreFuente, codePage)); } return(tablaFuentes); }
/// <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); }
/// <summary> /// Devuelve la tabla de fuentes del documento RTF. /// </summary> /// <returns>Tabla de fuentes del documento RTF</returns> public RtfFontTable GetFontTable() { RtfFontTable tablaFuentes = new RtfFontTable(); //Buscamos la tabla de fuentes en el árbol RtfTreeNode ntf = MainGroup.SelectSingleGroup("fonttbl"); //Nodo con la tabla de fuentes //Rellenamos el array de fuentes for (int j = 1; j < ntf.ChildNodes.Count; j++) { RtfTreeNode fuente = ntf.ChildNodes[j]; int indiceFuente = -1; string nombreFuente = null; foreach (RtfTreeNode nodo in fuente.ChildNodes) { if (nodo.NodeKey == "f") { indiceFuente = nodo.Parameter; } if (nodo.NodeType == RtfNodeType.Text) { nombreFuente = nodo.NodeKey.Substring(0, nodo.NodeKey.Length - 1); } } tablaFuentes.AddFont(indiceFuente, nombreFuente); } return(tablaFuentes); }
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 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)); }
/// <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 de la fuente pasada como parámetro, insertándola en la tabla de fuentes si es necesario. /// </summary> /// <param name="fontDestTbl">Tabla de fuentes resultante.</param> /// <param name="sFontName">Fuente buscada.</param> /// <returns></returns> private int getFontID(ref RtfFontTable fontDestTbl, string sFontName) { int iExistingFontID; if ((iExistingFontID = fontDestTbl.IndexOf(sFontName)) == -1) { fontDestTbl.AddFont(sFontName); iExistingFontID = fontDestTbl.IndexOf(sFontName); RtfTreeNode fontTableGroupNode = baseRtfDoc.MainGroup.SelectSingleGroup("fonttbl"); RtfTreeNode ftFont = new RtfTreeNode(RtfNodeType.Group); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "f", true, iExistingFontID)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "fnil", false, 0)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Text, sFontName + ";", false, 0)); fontTableGroupNode.AppendChild(ftFont); } return(iExistingFontID); }
/// <summary> /// Obtiene el código de la fuente pasada como parámetro, insertándola en la tabla de fuentes si es necesario. /// </summary> /// <param name="fontDestTbl">Tabla de fuentes resultante.</param> /// <param name="sFontName">Fuente buscada.</param> /// <returns></returns> private int getFontID(ref RtfFontTable fontDestTbl, string sFontName) { int iExistingFontID = -1; if ((iExistingFontID = fontDestTbl.IndexOf(sFontName)) == -1) { fontDestTbl.AddFont(sFontName); iExistingFontID = fontDestTbl.IndexOf(sFontName); RtfNodeCollection nodeListToInsert = baseRtfDoc.RootNode.SelectNodes("fonttbl"); RtfTreeNode ftFont = new RtfTreeNode(RtfNodeType.Group); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "f", true, iExistingFontID)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "fnil", false, 0)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Text, sFontName + ";", false, 0)); nodeListToInsert[0].ParentNode.AppendChild(ftFont); } return(iExistingFontID); }
/// <summary> /// Ajusta las fuentes del documento a insertar. /// </summary> /// <param name="parentNode">Nodo grupo que se está procesando.</param> /// <param name="fontDestTbl">Tabla de fuentes resultante.</param> /// <param name="fontToCopyTbl">Tabla de fuentes del documento a insertar.</param> private void adjustFontRecursive(RtfTreeNode parentNode, RtfFontTable fontDestTbl, RtfFontTable fontToCopyTbl) { 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 == "f" || parentNode.ChildNodes[iNdIndex].NodeKey == "stshfdbch" || parentNode.ChildNodes[iNdIndex].NodeKey == "stshfloch" || parentNode.ChildNodes[iNdIndex].NodeKey == "stshfhich" || parentNode.ChildNodes[iNdIndex].NodeKey == "stshfbi" || parentNode.ChildNodes[iNdIndex].NodeKey == "deff" || parentNode.ChildNodes[iNdIndex].NodeKey == "af") && parentNode.ChildNodes[iNdIndex].HasParameter) { parentNode.ChildNodes[iNdIndex].Parameter = getFontID(ref fontDestTbl, fontToCopyTbl[parentNode.ChildNodes[iNdIndex].Parameter]); } adjustFontRecursive(parentNode.ChildNodes[iNdIndex], fontDestTbl, fontToCopyTbl); } } }
/// <summary> /// Obtiene el código de la fuente pasada como parámetro, insertándola en la tabla de fuentes si es necesario. /// </summary> /// <param name="fontDestTbl">Tabla de fuentes resultante.</param> /// <param name="sFontName">Fuente buscada.</param> /// <returns></returns> private int getFontID(ref RtfFontTable fontDestTbl, string sFontName) { int iExistingFontID = -1; if ((iExistingFontID = fontDestTbl.IndexOf(sFontName)) == -1) { fontDestTbl.AddFont(sFontName); iExistingFontID = fontDestTbl.IndexOf(sFontName); RtfNodeCollection nodeListToInsert = baseRtfDoc.RootNode.SelectNodes("fonttbl"); RtfTreeNode ftFont = new RtfTreeNode(RtfNodeType.Group); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "f", true, iExistingFontID)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "fnil", false, 0)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Text, sFontName + ";", false, 0)); nodeListToInsert[0].ParentNode.AppendChild(ftFont); } return iExistingFontID; }
/// <summary> /// Devuelve la tabla de fuentes del documento RTF. /// </summary> /// <returns>Tabla de fuentes del documento RTF</returns> public RtfFontTable GetFontTable() { RtfFontTable tablaFuentes = new RtfFontTable(); //Nodo raiz del documento RtfTreeNode root = rootNode; //Grupo principal del documento RtfTreeNode nprin = root.FirstChild; //Buscamos la tabla de fuentes en el árbol bool enc = false; int i = 0; RtfTreeNode ntf = 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 == "fonttbl") { enc = true; ntf = nprin.ChildNodes[i]; } i++; } //Rellenamos el array de fuentes for (int j = 1; j < ntf.ChildNodes.Count; j++) { RtfTreeNode fuente = ntf.ChildNodes[j]; int indiceFuente = -1; string nombreFuente = null; int codePage = Encoding.Default.CodePage; foreach (RtfTreeNode nodo in fuente.ChildNodes) { if (nodo.NodeType == RtfNodeType.Keyword) { if (nodo.NodeKey == "f") indiceFuente = nodo.Parameter; else if(nodo.NodeKey == "fcharset") codePage = CharSetConvertor.ToCodePage(nodo.Parameter, codePage); } else if (nodo.NodeType == RtfNodeType.Text) nombreFuente = nodo.NodeKey.Substring(0, nodo.NodeKey.Length - 1); } tablaFuentes.AddFont(indiceFuente, new RtfFont(nombreFuente, codePage)); } return tablaFuentes; }
/// <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> /// Ajusta las fuentes del documento a insertar. /// </summary> /// <param name="parentNode">Nodo grupo que se está procesando.</param> /// <param name="fontDestTbl">Tabla de fuentes resultante.</param> /// <param name="fontToCopyTbl">Tabla de fuentes del documento a insertar.</param> private void adjustFontRecursive(RtfTreeNode parentNode, RtfFontTable fontDestTbl, RtfFontTable fontToCopyTbl) { 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 == "f" || parentNode.ChildNodes[iNdIndex].NodeKey == "stshfdbch" || parentNode.ChildNodes[iNdIndex].NodeKey == "stshfloch" || parentNode.ChildNodes[iNdIndex].NodeKey == "stshfhich" || parentNode.ChildNodes[iNdIndex].NodeKey == "stshfbi" || parentNode.ChildNodes[iNdIndex].NodeKey == "deff" || parentNode.ChildNodes[iNdIndex].NodeKey == "af") && parentNode.ChildNodes[iNdIndex].HasParameter == true) { parentNode.ChildNodes[iNdIndex].Parameter = getFontID(ref fontDestTbl, fontToCopyTbl[parentNode.ChildNodes[iNdIndex].Parameter]); } adjustFontRecursive(parentNode.ChildNodes[iNdIndex], fontDestTbl, fontToCopyTbl); } } }