/// <summary> /// Inserta el nuevo árbol en el árbol base (como un nuevo grupo) eliminando toda la cabecera del documento insertado. /// </summary> /// <param name="parentNode">Grupo base en el que se insertará el nuevo arbol.</param> /// <param name="treeToCopyParent">Nuevo árbol a insertar.</param> /// <param name="intCurrIndex">Índice en el que se insertará el nuevo árbol dentro del grupo base.</param> private void execMergeDoc(RtfTreeNode parentNode, RtfTree treeToCopyParent, int intCurrIndex) { //Se busca el primer "\pard" del documento (comienzo del texto) RtfTreeNode nodePard = treeToCopyParent.RootNode.FirstChild.SelectSingleChildNode("pard"); //Se obtiene el índice del nodo dentro del principal int indPard = treeToCopyParent.RootNode.FirstChild.ChildNodes.IndexOf(nodePard); //Se crea el nuevo grupo RtfTreeNode newGroup = new RtfTreeNode(RtfNodeType.Group); //Se resetean las opciones de párrafo y fuente newGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "pard", false, 0)); newGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "plain", false, 0)); //Se inserta cada nodo hijo del documento nuevo en el documento base for (int i = indPard + 1; i < treeToCopyParent.RootNode.FirstChild.ChildNodes.Count; i++) { RtfTreeNode newNode = treeToCopyParent.RootNode.FirstChild.ChildNodes[i].CloneNode(); newGroup.AppendChild(newNode); } //Se inserta el nuevo grupo con el nuevo documento parentNode.InsertChild(intCurrIndex, newGroup); }
/// <summary> /// Inserta un campo en el documento /// </summary> /// <param name="parent">Nodo padre del campo</param> /// <param name="instruction">El codigo de campo</param> /// <param name="initial_result">El valor del campo inicial</param> public void AddField(RtfTreeNode parent, string instruction, string initial_result) { var fieldInstructionGroup = new RtfTreeNode(RtfNodeType.Group); fieldInstructionGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "*", false, 0)); fieldInstructionGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "fldinst", false, 0)); var fieldInstructionTextGroup = new RtfTreeNode(RtfNodeType.Group); this.InsertText(fieldInstructionTextGroup, instruction); fieldInstructionGroup.AppendChild(fieldInstructionTextGroup); var fieldResultGroup = new RtfTreeNode(RtfNodeType.Group); fieldResultGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "fldrslt", false, 0)); var fieldResultTextGroup = new RtfTreeNode(RtfNodeType.Group); this.InsertText(fieldResultTextGroup, initial_result); fieldResultGroup.AppendChild(fieldResultTextGroup); var fieldGroup = new RtfTreeNode(RtfNodeType.Group); fieldGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "field", false, 0)); fieldGroup.AppendChild(fieldInstructionGroup); fieldGroup.AppendChild(fieldResultGroup); (parent ?? this.mainGroup).AppendChild(fieldGroup); }
/// <summary> /// Inserta el código RTF de la aplicación generadora del documento. /// </summary> private void InsertGenerator() { RtfTreeNode genGroup = new RtfTreeNode(RtfNodeType.Group); genGroup.AppendChild(new RtfTreeNode(RtfNodeType.Control, "*", false, 0)); genGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "generator", false, 0)); genGroup.AppendChild(new RtfTreeNode(RtfNodeType.Text, "NRtfTree Library 1.3.0;", false, 0)); mainGroup.InsertChild(7, genGroup); }
public void SimpleTreeNavigation() { //Creación de un árbol sencillo RtfTree tree = new RtfTree(); RtfTreeNode mainGroup = new RtfTreeNode(RtfNodeType.Group); RtfTreeNode rtfNode = new RtfTreeNode(RtfNodeType.Keyword, "rtf", true, 0); mainGroup.AppendChild(rtfNode); RtfTreeNode newGroup = new RtfTreeNode(RtfNodeType.Group); RtfTreeNode node1 = new RtfTreeNode(RtfNodeType.Keyword, "ul", false, 0); RtfTreeNode node2 = new RtfTreeNode(RtfNodeType.Text, "Test", false, 0); RtfTreeNode node3 = new RtfTreeNode(RtfNodeType.Text, "ulnone", false, 0); newGroup.AppendChild(node1); newGroup.AppendChild(node2); newGroup.AppendChild(node3); mainGroup.AppendChild(newGroup); tree.RootNode.AppendChild(mainGroup); //Navegación básica: tree Assert.That(tree.RootNode, Is.Not.Null); Assert.That(tree.MainGroup, Is.SameAs(mainGroup)); //Navegación básica: newGroup Assert.That(newGroup.Tree, Is.SameAs(tree)); Assert.That(newGroup.ParentNode, Is.SameAs(mainGroup)); Assert.That(newGroup.RootNode, Is.SameAs(tree.RootNode)); Assert.That(newGroup.ChildNodes, Is.Not.Null); Assert.That(newGroup[1], Is.SameAs(node2)); Assert.That(newGroup.ChildNodes[1], Is.SameAs(node2)); Assert.That(newGroup["ul"], Is.SameAs(node1)); Assert.That(newGroup.FirstChild, Is.SameAs(node1)); Assert.That(newGroup.LastChild, Is.SameAs(node3)); Assert.That(newGroup.PreviousSibling, Is.SameAs(rtfNode)); Assert.That(newGroup.NextSibling, Is.Null); Assert.That(newGroup.Index, Is.EqualTo(1)); //Navegación básica: nodo2 Assert.That(node2.Tree, Is.SameAs(tree)); Assert.That(node2.ParentNode, Is.SameAs(newGroup)); Assert.That(node2.RootNode, Is.SameAs(tree.RootNode)); Assert.That(node2.ChildNodes, Is.Null); Assert.That(node2[1], Is.Null); Assert.That(node2["ul"], Is.Null); Assert.That(node2.FirstChild, Is.Null); Assert.That(node2.LastChild, Is.Null); Assert.That(node2.PreviousSibling, Is.SameAs(node1)); Assert.That(node2.NextSibling, Is.SameAs(node3)); Assert.That(node2.Index, Is.EqualTo(1)); }
/// <summary> /// Inserta el código RTF de la tabla de colores en el documento. /// </summary> private void InsertColorTable() { RtfTreeNode ctGroup = new RtfTreeNode(RtfNodeType.Group); ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "colortbl", false, 0)); for (int i = 0; i < colorTable.Count; i++) { ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "red", true, colorTable[i].R)); ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "green", true, colorTable[i].G)); ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "blue", true, colorTable[i].B)); ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Text, ";", false, 0)); } mainGroup.InsertChild(6, ctGroup); }
public void AdjacentNodes() { //Creación de un árbol sencillo RtfTree tree = new RtfTree(); RtfTreeNode mainGroup = new RtfTreeNode(RtfNodeType.Group); RtfTreeNode rtfNode = new RtfTreeNode(RtfNodeType.Keyword, "rtf", true, 0); mainGroup.AppendChild(rtfNode); RtfTreeNode newGroup = new RtfTreeNode(RtfNodeType.Group); RtfTreeNode node1 = new RtfTreeNode(RtfNodeType.Keyword, "ul", false, 0); RtfTreeNode node2 = new RtfTreeNode(RtfNodeType.Text, "Test", false, 0); RtfTreeNode node3 = new RtfTreeNode(RtfNodeType.Keyword, "ulnone", false, 0); newGroup.AppendChild(node1); newGroup.AppendChild(node2); newGroup.AppendChild(node3); mainGroup.AppendChild(newGroup); tree.RootNode.AppendChild(mainGroup); RtfTreeNode node4 = new RtfTreeNode(RtfNodeType.Text, "fin", false, 0); mainGroup.AppendChild(node4); Assert.That(tree.RootNode.NextNode, Is.SameAs(mainGroup)); Assert.That(mainGroup.NextNode, Is.SameAs(rtfNode)); Assert.That(rtfNode.NextNode, Is.SameAs(newGroup)); Assert.That(newGroup.NextNode, Is.SameAs(node1)); Assert.That(node1.NextNode, Is.SameAs(node2)); Assert.That(node2.NextNode, Is.SameAs(node3)); Assert.That(node3.NextNode, Is.SameAs(node4)); Assert.That(node4.NextNode, Is.Null); Assert.That(node4.PreviousNode, Is.SameAs(node3)); Assert.That(node3.PreviousNode, Is.SameAs(node2)); Assert.That(node2.PreviousNode, Is.SameAs(node1)); Assert.That(node1.PreviousNode, Is.SameAs(newGroup)); Assert.That(newGroup.PreviousNode, Is.SameAs(rtfNode)); Assert.That(rtfNode.PreviousNode, Is.SameAs(mainGroup)); Assert.That(mainGroup.PreviousNode, Is.SameAs(tree.RootNode)); Assert.That(tree.RootNode.PreviousNode, Is.Null); }
public void AdjacentNodes() { //Creación de un árbol sencillo RtfTree tree = new RtfTree(); RtfTreeNode mainGroup = new RtfTreeNode(RtfNodeType.Group); RtfTreeNode rtfNode = new RtfTreeNode(RtfNodeType.Keyword, "rtf", true, 0); mainGroup.AppendChild(rtfNode); RtfTreeNode newGroup = new RtfTreeNode(RtfNodeType.Group); RtfTreeNode node1 = new RtfTreeNode(RtfNodeType.Keyword, "ul", false, 0); RtfTreeNode node2 = new RtfTreeNode(RtfNodeType.Text, "Test", false, 0); RtfTreeNode node3 = new RtfTreeNode(RtfNodeType.Keyword, "ulnone", false, 0); newGroup.AppendChild(node1); newGroup.AppendChild(node2); newGroup.AppendChild(node3); mainGroup.AppendChild(newGroup); tree.RootNode.AppendChild(mainGroup); RtfTreeNode node4 = new RtfTreeNode(RtfNodeType.Text, "fin", false, 0); mainGroup.AppendChild(node4); Assert.That(tree.RootNode.NextNode, Is.SameAs(mainGroup)); Assert.That(mainGroup.NextNode, Is.SameAs(rtfNode)); Assert.That(rtfNode.NextNode, Is.SameAs(newGroup)); Assert.That(newGroup.NextNode, Is.SameAs(node1)); Assert.That(node1.NextNode, Is.SameAs(node2)); Assert.That(node2.NextNode, Is.SameAs(node3)); Assert.That(node3.NextNode, Is.SameAs(node4)); Assert.That(node4.NextNode, Is.Null); Assert.That(node4.PreviousNode, Is.SameAs(node3)); Assert.That(node3.PreviousNode, Is.SameAs(node2)); Assert.That(node2.PreviousNode, Is.SameAs(node1)); Assert.That(node1.PreviousNode, Is.SameAs(newGroup)); Assert.That(newGroup.PreviousNode, Is.SameAs(rtfNode)); Assert.That(rtfNode.PreviousNode, Is.SameAs(mainGroup)); Assert.That(mainGroup.PreviousNode, Is.SameAs(tree.RootNode)); Assert.That(tree.RootNode.PreviousNode, Is.Null); }
/// <summary> /// Inserta el código RTF de la tabla de fuentes en el documento. /// </summary> private void InsertFontTable() { RtfTreeNode ftGroup = new RtfTreeNode(RtfNodeType.Group); ftGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "fonttbl", false, 0)); for (int i = 0; i < fontTable.Count; i++) { RtfTreeNode ftFont = new RtfTreeNode(RtfNodeType.Group); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "f", true, i)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "fnil", false, 0)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Text, fontTable[i] + ";", false, 0)); ftGroup.AppendChild(ftFont); } mainGroup.InsertChild(5, ftGroup); }
/// <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> /// Inserta una imagen en el documento. /// </summary> /// <param name="path">Ruta de la imagen a insertar.</param> /// <param name="width">Ancho deseado de la imagen en el documento.</param> /// <param name="height">Alto deseado de la imagen en el documento.</param> public void AddImage(string path, int width, int height) { FileStream fStream = null; BinaryReader br = null; try { byte[] data = null; FileInfo fInfo = new FileInfo(path); long numBytes = fInfo.Length; fStream = new FileStream(path, FileMode.Open, FileAccess.Read); br = new BinaryReader(fStream); data = br.ReadBytes((int)numBytes); StringBuilder hexdata = new StringBuilder(); for (int i = 0; i < data.Length; i++) { hexdata.Append(GetHexa(data[i])); } Image img = Image.FromFile(path); RtfTreeNode imgGroup = new RtfTreeNode(RtfNodeType.Group); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "pict", false, 0)); string format = ""; if (path.ToLower().EndsWith("wmf")) { format = "emfblip"; } else { format = "jpegblip"; } imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, format, false, 0)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "picw", true, img.Width * 20)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "pich", true, img.Height * 20)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "picwgoal", true, width * 20)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "pichgoal", true, height * 20)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Text, hexdata.ToString(), false, 0)); mainGroup.AppendChild(imgGroup); } finally { br.Close(); fStream.Close(); } }
/// <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> /// 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); }
public void AddChildToEmptyNode() { RtfTreeNode node = new RtfTreeNode(); Assert.That(node.ChildNodes, Is.Null); RtfTreeNode childNode = new RtfTreeNode(); node.InsertChild(0, childNode); Assert.That(node.ChildNodes, Is.Not.Null); Assert.That(node.ChildNodes[0], Is.SameAs(childNode)); Assert.That(childNode.ChildNodes, Is.Null); RtfTreeNode anotherChildNode = new RtfTreeNode(); childNode.AppendChild(anotherChildNode); Assert.That(childNode.ChildNodes, Is.Not.Null); Assert.That(childNode.ChildNodes[0], Is.SameAs(anotherChildNode)); }
/// <summary> /// Appends the definition of single table row /// </summary> /// <param name="node">Where to append the rtf elemenets (consider this as output)</param> /// <param name="cells">the definitions of the cells</param> public static void AddTableRow(this RtfTreeNode node, TextCell[] cells) { node.AddKeyword("trowd"); node.AddKeyword("trgaph", 400); int borderOffset = 0; foreach (var cell in cells) { borderOffset += cell.Width; node.AddSingleBorder(cell.BorderWidth); node.AddKeyword("cellx", borderOffset); } foreach (var cell in cells) { node.AddKeyword("pard"); switch (cell.TextAlignement) { case TextAlignement.Centered: node.AddCommand("qc"); break; case TextAlignement.Left: node.AddCommand("ql"); break; default: throw new NotSupportedException("Unexpected alignment"); } var grp = new RtfTreeNode(RtfNodeType.Group); if (cell.IsBold) { grp.AddCommand("b"); } node.AddKeyword("intbl"); grp.AddText(cell.Text); node.AppendChild(grp); node.AddKeyword("cell"); } node.AddKeyword("row"); }
public void AddChildToEmptyNode() { RtfTreeNode node = new RtfTreeNode(); Assert.That(node.ChildNodes, Is.Null); RtfTreeNode childNode = new RtfTreeNode(); node.InsertChild(0, childNode); Assert.That(node.ChildNodes, Is.Not.Null); Assert.That(node.ChildNodes[0], Is.SameAs(childNode)); Assert.That(childNode.ChildNodes, Is.Null); RtfTreeNode anotherChildNode = new RtfTreeNode(); childNode.AppendChild(anotherChildNode); Assert.That(childNode.ChildNodes, Is.Not.Null); Assert.That(childNode.ChildNodes[0], Is.SameAs(anotherChildNode)); }
/// <summary> /// Analiza el documento y lo carga con estructura de árbol. /// </summary> /// <returns>Se devuelve el valor 0 en caso de no producirse ningún error en la carga del documento. /// En caso contrario se devuelve el valor -1.</returns> private int parseRtfTree() { //Resultado de la carga del documento int res = 0; //Codificación por defecto del documento Encoding encoding = Encoding.Default; //Nodo actual RtfTreeNode curNode = rootNode; //Nuevos nodos para construir el árbol RTF RtfTreeNode newNode = null; //Se obtiene el primer token tok = lex.NextToken(); while (tok.Type != RtfTokenType.Eof) { switch (tok.Type) { case RtfTokenType.GroupStart: newNode = new RtfTreeNode(RtfNodeType.Group, "GROUP", false, 0); curNode.AppendChild(newNode); curNode = newNode; level++; break; case RtfTokenType.GroupEnd: curNode = curNode.ParentNode; level--; break; case RtfTokenType.Keyword: case RtfTokenType.Control: case RtfTokenType.Text: if (mergeSpecialCharacters) { //Contributed by Jan Stuchlík bool isText = tok.Type == RtfTokenType.Text || (tok.Type == RtfTokenType.Control && tok.Key == "'"); if (curNode.LastChild != null && (curNode.LastChild.NodeType == RtfNodeType.Text && isText)) { if (tok.Type == RtfTokenType.Text) { curNode.LastChild.NodeKey += tok.Key; break; } if (tok.Type == RtfTokenType.Control && tok.Key == "'") { curNode.LastChild.NodeKey += DecodeControlChar(tok.Parameter, encoding); break; } } else { //Primer caracter especial \' if (tok.Type == RtfTokenType.Control && tok.Key == "'") { newNode = new RtfTreeNode(RtfNodeType.Text, DecodeControlChar(tok.Parameter, encoding), false, 0); curNode.AppendChild(newNode); break; } } } newNode = new RtfTreeNode(tok); curNode.AppendChild(newNode); if (mergeSpecialCharacters) { //Contributed by Jan Stuchlík if (level == 1 && newNode.NodeType == RtfNodeType.Keyword && newNode.NodeKey == "ansicpg") { encoding = Encoding.GetEncoding(newNode.Parameter); } } break; default: res = -1; break; } //Se obtiene el siguiente token tok = lex.NextToken(); } //Si el nivel actual no es 0 ( == Algun grupo no está bien formado ) if (level != 0) { res = -1; } //Se devuelve el resultado de la carga return(res); }
/// <summary> /// Cierra el documento RTF. /// </summary> public void Close() { InsertFontTable(); InsertColorTable(); InsertGenerator(); mainGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "par", false, 0)); tree.RootNode.AppendChild(mainGroup); tree.SaveRtf(path); }
public void SimpleTreeNavigation() { //Creación de un árbol sencillo RtfTree tree = new RtfTree(); RtfTreeNode mainGroup = new RtfTreeNode(RtfNodeType.Group); RtfTreeNode rtfNode = new RtfTreeNode(RtfNodeType.Keyword, "rtf", true, 0); mainGroup.AppendChild(rtfNode); RtfTreeNode newGroup = new RtfTreeNode(RtfNodeType.Group); RtfTreeNode node1 = new RtfTreeNode(RtfNodeType.Keyword, "ul", false, 0); RtfTreeNode node2 = new RtfTreeNode(RtfNodeType.Text, "Test", false, 0); RtfTreeNode node3 = new RtfTreeNode(RtfNodeType.Text, "ulnone", false, 0); newGroup.AppendChild(node1); newGroup.AppendChild(node2); newGroup.AppendChild(node3); mainGroup.AppendChild(newGroup); tree.RootNode.AppendChild(mainGroup); //Navegación básica: tree Assert.That(tree.RootNode, Is.Not.Null); Assert.That(tree.MainGroup, Is.SameAs(mainGroup)); //Navegación básica: newGroup Assert.That(newGroup.Tree, Is.SameAs(tree)); Assert.That(newGroup.ParentNode, Is.SameAs(mainGroup)); Assert.That(newGroup.RootNode, Is.SameAs(tree.RootNode)); Assert.That(newGroup.ChildNodes, Is.Not.Null); Assert.That(newGroup[1], Is.SameAs(node2)); Assert.That(newGroup.ChildNodes[1], Is.SameAs(node2)); Assert.That(newGroup["ul"], Is.SameAs(node1)); Assert.That(newGroup.FirstChild, Is.SameAs(node1)); Assert.That(newGroup.LastChild, Is.SameAs(node3)); Assert.That(newGroup.PreviousSibling, Is.SameAs(rtfNode)); Assert.That(newGroup.NextSibling, Is.Null); Assert.That(newGroup.Index, Is.EqualTo(1)); //Navegación básica: nodo2 Assert.That(node2.Tree, Is.SameAs(tree)); Assert.That(node2.ParentNode, Is.SameAs(newGroup)); Assert.That(node2.RootNode, Is.SameAs(tree.RootNode)); Assert.That(node2.ChildNodes, Is.Null); Assert.That(node2[1], Is.Null); Assert.That(node2["ul"], Is.Null); Assert.That(node2.FirstChild, Is.Null); Assert.That(node2.LastChild, Is.Null); Assert.That(node2.PreviousSibling, Is.SameAs(node1)); Assert.That(node2.NextSibling, Is.SameAs(node3)); Assert.That(node2.Index, Is.EqualTo(1)); }
/// <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> /// Inserta el código RTF de la tabla de colores en el documento. /// </summary> private void InsertColorTable() { RtfTreeNode ctGroup = new RtfTreeNode(RtfNodeType.Group); ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "colortbl", false, 0)); for (int i = 0; i < colorTable.Count; i++) { ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "red", true, colorTable[i].R)); ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "green", true, colorTable[i].G)); ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "blue", true, colorTable[i].B)); ctGroup.AppendChild(new RtfTreeNode(RtfNodeType.Text, ";", false, 0)); } mainGroup.InsertChild(6, ctGroup); }
/// <summary> /// Inserta el código RTF de la tabla de fuentes en el documento. /// </summary> private void InsertFontTable() { RtfTreeNode ftGroup = new RtfTreeNode(RtfNodeType.Group); ftGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "fonttbl", false, 0)); for(int i=0; i<fontTable.Count; i++) { RtfTreeNode ftFont = new RtfTreeNode(RtfNodeType.Group); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "f", true, i)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "fnil", false, 0)); ftFont.AppendChild(new RtfTreeNode(RtfNodeType.Text, fontTable[i] + ";", false, 0)); ftGroup.AppendChild(ftFont); } mainGroup.InsertChild(5, ftGroup); }
/// <summary> /// Inserta una imagen en el documento. /// </summary> /// <param name="path">Ruta de la imagen a insertar.</param> /// <param name="width">Ancho deseado de la imagen en el documento.</param> /// <param name="height">Alto deseado de la imagen en el documento.</param> public void AddImage(string path, int width, int height) { FileStream fStream = null; BinaryReader br = null; try { byte[] data = null; FileInfo fInfo = new FileInfo(path); long numBytes = fInfo.Length; fStream = new FileStream(path, FileMode.Open, FileAccess.Read); br = new BinaryReader(fStream); data = br.ReadBytes((int)numBytes); StringBuilder hexdata = new StringBuilder(); for (int i = 0; i < data.Length; i++) { hexdata.Append(GetHexa(data[i])); } Image img = Image.FromFile(path); RtfTreeNode imgGroup = new RtfTreeNode(RtfNodeType.Group); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "pict", false, 0)); string format = ""; if (path.ToLower().EndsWith("wmf")) format = "emfblip"; else format = "jpegblip"; imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, format, false, 0)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "picw", true, img.Width * 20)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "pich", true, img.Height * 20)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "picwgoal", true, width * 20)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "pichgoal", true, height * 20)); imgGroup.AppendChild(new RtfTreeNode(RtfNodeType.Text, hexdata.ToString(), false, 0)); mainGroup.AppendChild(imgGroup); } finally { br.Close(); fStream.Close(); } }
/// <param name="node">Where to append (add) results</param> public static void AddKeyword(this RtfTreeNode node, string value) { node.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, value, false, 0)); }
/// <summary> /// Inserta todos los nodos de texto y control necesarios para representar un texto determinado. /// </summary> /// <param name="parent">Nodo padre del fragmento de texto</param> /// <param name="text">Texto a insertar.</param> /// <param name="highlight">Resaltar el texto?</param> private void InsertText(RtfTreeNode parent, string text, bool highlight = false) { int i = 0; int code = 0; RtfTreeNode textGroup = parent ?? this.mainGroup; if (highlight && text.Length > 0) { int indColor = colorTable.IndexOf(Color.Yellow); if (indColor == -1) { colorTable.AddColor(Color.Yellow); indColor = colorTable.IndexOf(Color.Yellow); } textGroup = new RtfTreeNode(RtfNodeType.Group); textGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, String.Format("highlight{0}", indColor), false, 0)); } while (i < text.Length) { code = Char.ConvertToUtf32(text, i); if (code >= 32 && code < 128) { StringBuilder s = new StringBuilder(""); while (i < text.Length && code >= 32 && code < 128) { s.Append(text[i]); i++; if (i < text.Length) { code = Char.ConvertToUtf32(text, i); } } textGroup.AppendChild(new RtfTreeNode(RtfNodeType.Text, s.ToString(), false, 0)); } else { if (text[i] == '\t') { textGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "tab", false, 0)); } else if (text[i] == '\n') { textGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "line", false, 0)); } else if (text[i] == '\r' && (i + 1) < text.Length && text.Substring(i, 2) == System.Environment.NewLine) { // Ignore carriage-return characters that make up a newline } else { if (code <= 255) { byte[] bytes = encoding.GetBytes(new char[] { text[i] }); textGroup.AppendChild(new RtfTreeNode(RtfNodeType.Control, "'", true, bytes[0])); } else { textGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "u", true, code)); textGroup.AppendChild(new RtfTreeNode(RtfNodeType.Text, "?", false, 0)); } } i++; } } if (highlight && text.Length > 0) { (parent ?? this.mainGroup).AppendChild(textGroup); } }
/// <summary> /// Inserta un número determinado de saltos de línea en el documento. /// </summary> /// <param name="n">Número de saltos de línea a insertar.</param> public void AddNewLine(int n) { for (int i = 0; i < n; i++) { mainGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "line", false, 0)); } }
/// <param name="node">Where to append (add) results</param> public static void AddCommand(this RtfTreeNode node, string value) { node.AppendChild(new RtfTreeNode(RtfNodeType.Control, value, false, 0)); }
/// <summary> /// Inserta el código RTF de la aplicación generadora del documento. /// </summary> private void InsertGenerator() { RtfTreeNode genGroup = new RtfTreeNode(RtfNodeType.Group); genGroup.AppendChild(new RtfTreeNode(RtfNodeType.Control, "*", false, 0)); genGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "generator", false, 0)); genGroup.AppendChild(new RtfTreeNode(RtfNodeType.Text, "NRtfTree Library 1.3.0;", false, 0)); mainGroup.InsertChild(7, genGroup); }
/// <param name="node">Where to append (add) results</param> public static void AddKeyword(this RtfTreeNode node, string value, int parameter) { node.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, value, true, parameter)); }
/// <summary> /// Inserta el nuevo árbol en el árbol base (como un nuevo grupo) eliminando toda la cabecera del documento insertado. /// </summary> /// <param name="parentNode">Grupo base en el que se insertará el nuevo arbol.</param> /// <param name="treeToCopyParent">Nuevo árbol a insertar.</param> /// <param name="intCurrIndex">Índice en el que se insertará el nuevo árbol dentro del grupo base.</param> private void execMergeDoc(RtfTreeNode parentNode, RtfTree treeToCopyParent, int intCurrIndex) { //Se busca el primer "\pard" del documento (comienzo del texto) RtfTreeNode nodePard = treeToCopyParent.RootNode.FirstChild.SelectSingleChildNode("pard"); //Se obtiene el índice del nodo dentro del principal int indPard = treeToCopyParent.RootNode.FirstChild.ChildNodes.IndexOf(nodePard); //Se crea el nuevo grupo RtfTreeNode newGroup = new RtfTreeNode(RtfNodeType.Group); //Se resetean las opciones de párrafo y fuente newGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "pard", false, 0)); newGroup.AppendChild(new RtfTreeNode(RtfNodeType.Keyword, "plain", false, 0)); //Se inserta cada nodo hijo del documento nuevo en el documento base for (int i = indPard + 1; i < treeToCopyParent.RootNode.FirstChild.ChildNodes.Count; i++) { RtfTreeNode newNode = treeToCopyParent.RootNode.FirstChild.ChildNodes[i].CloneNode(true); newGroup.AppendChild(newNode); } //Se inserta el nuevo grupo con el nuevo documento parentNode.InsertChild(intCurrIndex, newGroup); }