Examples
I. Example Documents
II. Working with trees
- Load an RTF document from a file.
- Load an RTF document from a string.
- Save an RTF document to a file.
- Get RTF code from a tree.
- Get the RTF tree of the document.
- Get font table.
- Get color table.
- Get document properties.
III. Working with tree nodes.
- Get RTF code from a node.
- Node type, key and text.
- Node paramaters (Parameter and HasParameter properties).
- Navigating properties (RootNode, FirstChild, NextSibling, ...).
- Search nodes by key (SelectXXX() methods).
- Search nodes by type (SelectXXXByType() methods).
- Create new nodes (Constructors).
- Add node to the tree (AppendChild() method).
- Image nodes (ImageNode class).
- Object nodes (ObjectNode class).
Example Document 1 (General)
Download document: example.rtf
Document RTF Code:
{\rtf1\ansi\ansicpg1252\deff0\deflang1034\deflangfe1034\deftab708
{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\fswiss\fprq2\fcharset0 Arial;}}
{\colortbl ;\red255\green0\blue0;\red51\green153\blue102;\red255\green102\blue0;\red0\green0\blue255;}
{\*\generator Msftedit 5.41.15.1507;}{\info{\title Demo NRtfTree Title}{\subject Demo NRtfTree Subject}
{\author Sgoliver (Author)}{\keywords demo;nrtftree;sgoliver}{\doccomm This is a comment.}{\operator Ninguno}
{\creatim\yr2005\mo12\dy11\hr18\min52}{\revtim\yr2005\mo12\dy11\hr20\min23}{\version6}{\edmins4}{\nofpages1}
{\nofwords12}{\nofchars59}{\*\manager Sgoliver (Admin)}{\*\company www50.brinkster.com/sgolivernet}
{\*\category Demos (Category)}{\nofcharsws70}{\vern24579}}
\viewkind4\uc1\pard\nowidctlpar\lang3082\f0\fs24 Esto es una prueba para \cf1 probar
\cf0 la librer\'eda \b NRtfTree\b0 .\par\par
\ul\i Segunda\ulnone\i0 l\'ednea de la \cf2\b prueba\cf0\b0 .\par\par
Y por \cf3\ul\b\i\'faltimo\cf0\ulnone\b0\i0 , la \fs32 tercera\fs24 l\'ednea del
\cf4\f1 documento\cf0\f0 RTF.\par}
Rendered RTF Code: Esto es una prueba para probar la librería NRtfTree. Segunda línea de la prueba. Y por último, la tercera línea del documento RTF.
Example Document 2 (Images)
Download document: example2.rtf
Example Document 3 (Objects)
Download document: example3.rtf
Load an RTF document from a file - LoadRtfFile() method:
//Create an RTF object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
Load an RTF document from a string - LoadRtfText() method:
//RTF document in a string
String rtfText =
@"{\rtf1\ansi\ansicpg1252\deff0\deflang1034\deflangfe1034\deftab708" +
@"{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}" +
@"\viewkind4\uc1\pard\nowidctlpar\lang3082\f0\fs24 example\par}";
//Create an RTF object
RtfTree tree2 = new RtfTree();
//Load an RTF document from a string
tree2.LoadRtfText(rtfText);
Save an RtfTree document to a file - SaveRtf() method:
//RTF document in a string
String rtfText =
@"{\rtf1\ansi\ansicpg1252\deff0\deflang1034\deflangfe1034\deftab708" +
@"{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}" +
@"\viewkind4\uc1\pard\nowidctlpar\lang3082\f0\fs24 example\par}";
//Create an RtfTree object
RtfTree tree = new RtfTree();
//Load an RTF document from a string
tree.LoadRtfText(rtfText);
//Save the RTF document to a file
tree.SaveRtf("c:\\Example-SaveRtf.rtf");
Get the RTF code of the document - Rtf property:
//Create an RtfTree object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Get and print RTF code
Console.Write(tree.Rtf);
Get the RTF Tree of the document - ToString() and ToStringEx() methods:
//Create an RtfTree object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Get and print RTF code
Console.Write(tree.ToStringEx());
----------------------------------------------------
OUTPUT
----------------------------------------------------
ROOT
GROUP
KEYWORD: rtf 1
KEYWORD: ansi
KEYWORD: ansicpg 1252
KEYWORD: deff 0
KEYWORD: deflang 1034
KEYWORD: deflangfe 1034
KEYWORD: deftab 708
GROUP
KEYWORD: fonttbl
GROUP
KEYWORD: f 0
KEYWORD: froman
KEYWORD: fprq 2
KEYWORD: fcharset 0
TEXT: Times New Roman;
GROUP
KEYWORD: f 1
KEYWORD: fswiss
KEYWORD: fprq 2
KEYWORD: fcharset 0
TEXT: Arial;
GROUP
KEYWORD: colortbl
TEXT: ;
KEYWORD: red 255
KEYWORD: green 0
KEYWORD: blue 0
TEXT: ;
KEYWORD: red 51
KEYWORD: green 153
KEYWORD: blue 102
TEXT: ;
KEYWORD: red 255
KEYWORD: green 102
KEYWORD: blue 0
TEXT: ;
KEYWORD: red 0
KEYWORD: green 0
KEYWORD: blue 255
TEXT: ;
GROUP
CONTROL: *
KEYWORD: generator
TEXT: Msftedit 5.41.15.1507;
GROUP
KEYWORD: info
GROUP
KEYWORD: title
TEXT: Demo NRtfTree Title
GROUP
KEYWORD: subject
TEXT: Demo NRtfTree Subject
GROUP
KEYWORD: author
TEXT: Sgoliver (Author)
GROUP
KEYWORD: keywords
TEXT: demo;nrtftree;sgoliver
GROUP
KEYWORD: doccomm
TEXT: This is a comment.
GROUP
KEYWORD: operator
TEXT: Ninguno
GROUP
KEYWORD: creatim
KEYWORD: yr 2005
KEYWORD: mo 12
KEYWORD: dy 11
KEYWORD: hr 18
KEYWORD: min 52
GROUP
KEYWORD: revtim
KEYWORD: yr 2005
KEYWORD: mo 12
KEYWORD: dy 11
KEYWORD: hr 20
KEYWORD: min 23
GROUP
KEYWORD: version 6
GROUP
KEYWORD: edmins 4
GROUP
KEYWORD: nofpages 1
GROUP
KEYWORD: nofwords 12
GROUP
KEYWORD: nofchars 59
GROUP
CONTROL: *
KEYWORD: manager
TEXT: Sgoliver (Admin)
GROUP
CONTROL: *
KEYWORD: company
TEXT: www50.brinkster.com/sgolivernet
GROUP
CONTROL: *
KEYWORD: category
TEXT: Demos (Category)
GROUP
KEYWORD: nofcharsws 70
GROUP
KEYWORD: vern 24579
KEYWORD: viewkind 4
KEYWORD: uc 1
KEYWORD: pard
KEYWORD: nowidctlpar
KEYWORD: lang 3082
KEYWORD: f 0
KEYWORD: fs 24
TEXT: Esto es una prueba para
KEYWORD: cf 1
TEXT: probar
KEYWORD: cf 0
TEXT: la librer
CONTROL: ' 237
TEXT: a
KEYWORD: b
TEXT: NRtfTree
KEYWORD: b 0
TEXT: .
KEYWORD: par
KEYWORD: par
KEYWORD: ul
KEYWORD: i
TEXT: Segunda
KEYWORD: ulnone
KEYWORD: i 0
TEXT: l
CONTROL: ' 237
TEXT: nea de la
KEYWORD: cf 2
KEYWORD: b
TEXT: prueba
KEYWORD: cf 0
KEYWORD: b 0
TEXT: .
KEYWORD: par
KEYWORD: par
TEXT: Y por
KEYWORD: cf 3
KEYWORD: ul
KEYWORD: b
KEYWORD: i
CONTROL: ' 250
TEXT: ltimo
KEYWORD: cf 0
KEYWORD: ulnone
KEYWORD: b 0
KEYWORD: i 0
TEXT: , la
KEYWORD: fs 32
TEXT: tercera
KEYWORD: fs 24
TEXT: l
CONTROL: ' 237
TEXT: nea del
KEYWORD: cf 4
KEYWORD: f 1
TEXT: documento
KEYWORD: cf 0
KEYWORD: f 0
TEXT: RTF.
KEYWORD: par
Get font table - GetFontTable() method:
//Create an RtfTree object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Get Font Table
string[] fonts = tree.GetFontTable();
//Print Font Table
foreach(string font in fonts)
{
Console.WriteLine(font);
}
----------------------------------------------------
OUTPUT
----------------------------------------------------
Times New Roman
Arial
Get Color Table - GetColorTable() method:
//Create an RtfTree object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Get Color Table
Color[] colors = tree.GetColorTable();
//Print Color Table
foreach(Color color in colors)
{
Console.WriteLine(color);
}
----------------------------------------------------
OUTPUT
----------------------------------------------------
Color [A=255, R=0, G=0, B=0]
Color [A=255, R=255, G=0, B=0]
Color [A=255, R=51, G=153, B=102]
Color [A=255, R=255, G=102, B=0]
Color [A=255, R=0, G=0, B=255]
Get Document Properties - GetInfoGroup() method:
//Create an RtfTree object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Get Document Properties
InfoGroup info = tree.GetInfoGroup();
//Print some Document Properties
Console.WriteLine("Author: " + info.Author);
Console.WriteLine("Subject: " + info.Subject);
Console.WriteLine("Title: " + info.Title);
Console.WriteLine("Category: " + info.Category);
Console.WriteLine("Num. of Chars: " + info.NumberOfChars);
----------------------------------------------------
OUTPUT
----------------------------------------------------
Author: Sgoliver (Author)
Subject: Demo NRtfTree Subject
Title: Demo NRtfTree Title
Category: Demos (Category)
Num. of Chars: 59
Get RTF code from node - Rtf property:
//Create an RTF object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Search Font Table Node
RtfTreeNode ftNode = tree.RootNode.SelectSingleNode("fonttbl");
//Get Font Table Group
RtfTreeNode ftGroup = ftNode.ParentNode;
//Print RTF code
Console.Write(ftGroup.Rtf);
----------------------------------------------------
OUTPUT
----------------------------------------------------
{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\fswiss\fprq2\fcharset0 Arial;}
Get node type, key and text - NodeType - NodeKey properties:
//Create an RTF object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Search Font Table Node
RtfTreeNode ftNode = tree.RootNode.SelectSingleNode("fonttbl");
//Search first Text Node
RtfTreeNode textNode =
tree.RootNode.FirstChild.SelectSingleChildNodeByType(RtfNodeType.Text);
//If (NodeType == Keyword) --> NodeKey returns the keyword
Console.WriteLine("Node type: " + ftNode.NodeType);
Console.WriteLine("Node key: " + ftNode.NodeKey);
//If (NodeType == Text) --> NodeKey returns the text
Console.WriteLine("Node type: " + textNode.NodeType);
Console.WriteLine("Node key: " + textNode.NodeKey);
----------------------------------------------------
OUTPUT
----------------------------------------------------
Node type: Keyword
Node key: fonttbl
Node type: Text
Node key: Esto es una prueba para
Node Parameters - Parameter - HasParameter properties:
//Create an RTF object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Search First \fs keyword (font size keyword)
RtfTreeNode fsNode = tree.RootNode.SelectSingleNode("fs");
//Print parameter and value
Console.WriteLine("Has Parameter? : " + fsNode.HasParameter);
Console.WriteLine("Parameter Value: " + fsNode.Parameter);
----------------------------------------------------
OUTPUT
----------------------------------------------------
Has Parameter? : True
Parameter Value: 24
Navigating properties
//Create an RTF object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Get Root Node
RtfTreeNode node = tree.RootNode;
Console.WriteLine("Root Node: " + node);
//Get First Child
node = node.FirstChild;
Console.WriteLine("First Child: " + node);
//Get First Child
node = node.FirstChild;
Console.WriteLine("First Child: " + node);
//Get Next Sibling
node = node.NextSibling;
Console.WriteLine("Next Sibling: " + node);
//Get Parent Node
node = node.ParentNode;
Console.WriteLine("Parent Node: " + node);
----------------------------------------------------
OUTPUT
----------------------------------------------------
Root Node: [Root, ROOT, False, 0]
First Child: [Group, GROUP, False, 0]
First Child: [Keyword, rtf, True, 1]
Next Sibling: [Keyword, ansi, False, 0]
Parent Node: [Group, GROUP, False, 0]
Search nodes by key (SelectXXX() methods)
//Create an RTF object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Search the first "\fs" keyword
RtfTreeNode node = tree.RootNode.SelectSingleNode("fs");
Console.WriteLine("First \\fs keyword: " + node);
//Search all the "\cf" keywords:
RtfNodeCollection nodes = tree.RootNode.SelectNodes("cf");
Console.WriteLine("All \\cf keywords: ");
foreach(RtfTreeNode n in nodes)
Console.WriteLine("\t" + n);
----------------------------------------------------
OUTPUT
----------------------------------------------------
First \fs keyword: [Keyword, fs, True, 24]
All \cf keywords:
[Keyword, cf, True, 1]
[Keyword, cf, True, 0]
[Keyword, cf, True, 2]
[Keyword, cf, True, 0]
[Keyword, cf, True, 3]
[Keyword, cf, True, 0]
[Keyword, cf, True, 4]
[Keyword, cf, True, 0]
Search nodes by type (SelectXXXByType() methods)
//Create an RTF object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Search the first "\fs" keyword
RtfTreeNode node = tree.RootNode.SelectSingleNodeByType(RtfNodeType.Control);
Console.WriteLine("First control node: " + node);
//Search all the "\cf" keywords:
RtfNodeCollection nodes = tree.RootNode.SelectNodesByType(RtfNodeType.Control);
Console.WriteLine("All control nodes: ");
foreach(RtfTreeNode n in nodes)
Console.WriteLine("\t" + n);
----------------------------------------------------
OUTPUT
----------------------------------------------------
First control node: [Control, *, False, 0]
All control nodes:
[Control, *, False, 0]
[Control, *, False, 0]
[Control, *, False, 0]
[Control, *, False, 0]
[Control, ', True, 237]
[Control, ', True, 237]
[Control, ', True, 250]
[Control, ', True, 237]
Creating new nodes (Constructors)
//Create a default node RtfTreeNode defaultNode = new RtfTreeNode(); //Create a Keyword node RtfTreeNode typeNode = new RtfTreeNode(RtfNodeType.Keyword); //Create a full node (Type, Key, HasParameter, Parameter) RtfTreeNode fullNode = new RtfTreeNode(RtfNodeType.Keyword, "cf", true, 1);
Adding nodes to the tree - AppendChild() method
//Create an RTF object
RtfTree tree = new RtfTree();
//Load an RTF document from a file
tree.LoadRtfFile("c:\\example.rtf");
//Create a new nodes
RtfTreeNode newLineNode = new RtfTreeNode(RtfNodeType.Keyword, "par", false, 0);
RtfTreeNode textNode = new RtfTreeNode(RtfNodeType.Text, "New Text", false, 0);
//Append the new node to the tree
tree.RootNode.FirstChild.AppendChild(newLineNode);
tree.RootNode.FirstChild.AppendChild(textNode);
//Save the document
tree.SaveRtf("c:\\newDocument.rtf");
New RTF Document: Esto es una prueba para probar la librería NRtfTree. Segunda línea de la prueba. Y por último, la tercera línea del documento RTF. New Text
Image nodes (ImageNode class)
//Create an RTF object
RtfTree arbol = new RtfTree();
//Load an RTF document from a file
arbol.LoadRtfFile("example2.rtf");
//Search all "\pict" nodes (Images)
RtfNodeCollection imageNodes = arbol.RootNode.SelectNodes("pict");
//Save all found images
int i = 1;
foreach (RtfTreeNode node in imageNodes)
{
//Create an image node
ImageNode imageNode = new ImageNode(node.ParentNode);
//Save the image with its original format
imageNode.SaveImage("image" + i + "." + getExtension(imageNode.ImageFormat));
i++;
}
Object nodes (ObjectNode class)
//Create an RtfTree object
RtfTree arbol = new RtfTree();
//Load an RTF document from a file
arbol.LoadRtfFile("example3.rtf");
//Search the first Object keyword
RtfTreeNode node = arbol.RootNode.SelectSingleNode("object");
//Create an Object Node
ObjectNode objectNode = new ObjectNode(node.ParentNode);
//Print some object information
txtArbol.Text += "Object type: " + objectNode.ObjectType + "\r\n";
txtArbol.Text += "Object class: " + objectNode.ObjectClass + "\r\n";
//Get the "\result" node
RtfTreeNode resultNode = objectNode.ResultNode;
RtfTreeNode auxNode = null;
//If an image exists in the "\result" group
if ((auxNode = resultNode.SelectSingleNode("pict")) != null)
{
//Create an image node
ImageNode imageNode = new ImageNode(auxNode.ParentNode);
//Print some image information
txtArbol.Text += "Image width: " + imageNode.Width/20 + "\r\n";
txtArbol.Text += "Image heigh: " + imageNode.Height/20 + "\r\n";
txtArbol.Text += "Image format: " + imageNode.ImageFormat + "\r\n";
//Save the image
MessageBox.Show("File: image-example3." + getExtension(imageNode.ImageFormat));
imageNode.SaveImage("image-example3." + getExtension(imageNode.ImageFormat));
}
else
{
MessageBox.Show("'\result' node contains no images!");
}
