/// <summary> /// search child node special keyword /// </summary> /// <param name="Key">special keyword</param> /// <param name="Deeply">whether search deeplyl</param> /// <returns>node find</returns> public RTFNode SearchKey(string Key, bool Deeply) { foreach (RTFNode node in myNodes) { if (node.Type == RTFNodeType.Keyword || node.Type == RTFNodeType.ExtKeyword || node.Type == RTFNodeType.Control) { if (node.Keyword == Key) { return(node); } } if (Deeply) { if (node is RTFNodeGroup) { RTFNodeGroup g = ( RTFNodeGroup )node; RTFNode n = g.SearchKey(Key, true); if (n != null) { return(n); } } } } return(null); }
/// <summary> /// read font table /// </summary> /// <param name="group"></param> private void ReadFontTable(RTFNodeGroup group) { myFontTable.Clear(); foreach (RTFNode node in group.Nodes) { if (node is RTFNodeGroup) { var index = -1; string name = null; var charset = 0; foreach (RTFNode item in node.Nodes) { if (item.Keyword == "f" && item.HasParameter) { index = item.Parameter; } else if (item.Keyword == RTFConsts._fcharset) { charset = item.Parameter; } else if (item.Type == RTFNodeType.Text) { if (item.Keyword != null && item.Keyword.Length > 0) { name = item.Keyword; break; } } } if (index >= 0 && name != null) { if (name.EndsWith(";")) { name = name.Substring(0, name.Length - 1); } name = name.Trim(); //System.Console.WriteLine( "Index:" + index + " Name:" + name ); var font = new RTFFont(index, name); font.Charset = charset; myFontTable.Add(font); } } } }
/// <summary> /// read document information /// </summary> /// <param name="group"></param> private void ReadDocumentInfo(RTFNodeGroup group) { myInfo.Clear(); var list = group.GetAllNodes(false); foreach (RTFNode node in group.Nodes) { if ((node is RTFNodeGroup) == false) { continue; } if (node.Keyword == "creatim") { myInfo.Creatim = ReadDateTime(node); } else if (node.Keyword == "revtim") { myInfo.Revtim = ReadDateTime(node); } else if (node.Keyword == "printim") { myInfo.Printim = ReadDateTime(node); } else if (node.Keyword == "buptim") { myInfo.Buptim = ReadDateTime(node); } else { if (node.HasParameter) { myInfo.SetInfo(node.Keyword, node.Parameter.ToString()); } else { myInfo.SetInfo(node.Keyword, node.Nodes.Text); } } } }
/// <summary> /// read color table /// </summary> /// <param name="group"></param> private void ReadColorTable(RTFNodeGroup group) { myColorTable.Clear(); var r = -1; var g = -1; var b = -1; foreach (RTFNode node in group.Nodes) { if (node.Keyword == "red") { r = node.Parameter; } else if (node.Keyword == "green") { g = node.Parameter; } else if (node.Keyword == "blue") { b = node.Parameter; } if (node.Keyword == ";") { if (r >= 0 && g >= 0 && b >= 0) { var c = System.Drawing.Color.FromArgb(255, r, g, b); myColorTable.Add(c); r = -1; g = -1; b = -1; } } } if (r >= 0 && g >= 0 && b >= 0) { // read the last color var c = System.Drawing.Color.FromArgb(255, r, g, b); myColorTable.Add(c); } }
/// <summary> /// read font table /// </summary> /// <param name="group"></param> private void ReadFontTable(RTFNodeGroup group) { myFontTable.Clear(); foreach (RTFNode node in group.Nodes) { if (node is RTFNodeGroup) { int index = -1; string name = null; int charset = 0; foreach (RTFNode item in node.Nodes) { if (item.Keyword == "f" && item.HasParameter) { index = item.Parameter; } else if (item.Keyword == RTFConsts._fcharset) { charset = item.Parameter; } else if (item.Type == RTFNodeType.Text) { if (item.Keyword != null && item.Keyword.Length > 0) { name = item.Keyword; break; } } } if (index >= 0 && name != null) { if (name.EndsWith(";")) name = name.Substring(0, name.Length - 1); name = name.Trim(); //System.Console.WriteLine( "Index:" + index + " Name:" + name ); RTFFont font = new RTFFont(index, name); font.Charset = charset; myFontTable.Add(font); } } } }
/// <summary> /// read document information /// </summary> /// <param name="group"></param> private void ReadDocumentInfo(RTFNodeGroup group) { myInfo.Clear(); RTFNodeList list = group.GetAllNodes(false); foreach (RTFNode node in group.Nodes) { if ((node is RTFNodeGroup) == false) { continue; } if (node.Keyword == "creatim") { myInfo.Creatim = ReadDateTime(node); } else if (node.Keyword == "revtim") { myInfo.Revtim = ReadDateTime(node); } else if (node.Keyword == "printim") { myInfo.Printim = ReadDateTime(node); } else if (node.Keyword == "buptim") { myInfo.Buptim = ReadDateTime(node); } else { if (node.HasParameter) myInfo.SetInfo(node.Keyword, node.Parameter.ToString()); else { myInfo.SetInfo(node.Keyword, node.Nodes.Text); } } } }
/// <summary> /// read color table /// </summary> /// <param name="group"></param> private void ReadColorTable( RTFNodeGroup group ) { myColorTable.Clear(); int r = -1; int g = -1; int b = -1; foreach (RTFNode node in group.Nodes) { if (node.Keyword == "red") { r = node.Parameter; } else if (node.Keyword == "green") { g = node.Parameter; } else if (node.Keyword == "blue") { b = node.Parameter; } if (node.Keyword == ";") { if (r >= 0 && g >= 0 && b >= 0) { System.Drawing.Color c = System.Drawing.Color.FromArgb(255, r, g, b); myColorTable.Add(c); r = -1; g = -1; b = -1; } } } if (r >= 0 && g >= 0 && b >= 0) { // read the last color System.Drawing.Color c = System.Drawing.Color.FromArgb(255, r, g, b); myColorTable.Add(c); } }
/// <summary> /// load rtf /// </summary> /// <param name="reader">RTF text reader</param> public void Load( RTFReader reader ) { myNodes.Clear(); System.Collections.Stack groups = new System.Collections.Stack(); RTFNodeGroup NewGroup = null ; RTFNode NewNode = null; while( reader.ReadToken() != null ) { if( reader.TokenType == RTFTokenType.GroupStart ) { // begin group if( NewGroup == null) { NewGroup = this ; } else { NewGroup = new RTFNodeGroup(); NewGroup.OwnerDocument = this ; } if( NewGroup != this ) { RTFNodeGroup g = ( RTFNodeGroup ) groups.Peek(); g.AppendChild( NewGroup ); } groups.Push( NewGroup ); } else if( reader.TokenType == RTFTokenType.GroupEnd ) { // end group NewGroup = ( RTFNodeGroup ) groups.Pop(); NewGroup.MergeText(); if (NewGroup.FirstNode is RTFNode) { switch (NewGroup.Keyword) { case RTFConsts._fonttbl: // read font table ReadFontTable(NewGroup); break; case RTFConsts._colortbl: // read color table ReadColorTable(NewGroup); break; case RTFConsts._info : // read document information ReadDocumentInfo(NewGroup); break; } } if (groups.Count > 0) { NewGroup = (RTFNodeGroup)groups.Peek(); } else { break; } //NewGroup.MergeText(); } else { // read content NewNode = new RTFNode( reader.CurrentToken ); NewNode.OwnerDocument = this ; NewGroup.AppendChild( NewNode ); if (NewNode.Keyword == RTFConsts._f ) { RTFFont font = this.FontTable[NewNode.Parameter]; if (font != null) { myFontChartset = font.Encoding; } else { myFontChartset = null; } //myFontChartset = RTFFont.GetRTFEncoding( NewNode.Parameter ); } else if (NewNode.Keyword == RTFConsts._af) { RTFFont font = this.FontTable[NewNode.Parameter]; if (font != null) { myAssociateFontChartset = font.Encoding; } else { myAssociateFontChartset = null; } } } }// while( reader.ReadToken() != null ) while( groups.Count > 0 ) { NewGroup = ( RTFNodeGroup ) groups.Pop(); NewGroup.MergeText(); } //this.UpdateInformation(); }
/// <summary> /// read color table /// </summary> /// <param name="group"></param> private void ReadColorTable(RTFNodeGroup group) { MyColorTable.Clear(); var r = -1; var g = -1; var b = -1; foreach (var node in group.Nodes) { if (node.Keyword == "red") { r = node.Parameter; } else if (node.Keyword == "green") { g = node.Parameter; } else if (node.Keyword == "blue") { b = node.Parameter; } if (node.Keyword == ";") { if (r >= 0 && g >= 0 && b >= 0) { var c = Color.FromArgb(255, r, g, b); MyColorTable.Add(c); r = -1; g = -1; b = -1; } } } if (r >= 0 && g >= 0 && b >= 0) { // read the last color var c = Color.FromArgb(255, r, g, b); MyColorTable.Add(c); } }
/// <summary> /// load rtf /// </summary> /// <param name="reader">RTF text reader</param> public void Load(RTFReader reader) { MyNodes.Clear(); var groups = new Stack<RTFNodeGroup>(); RTFNodeGroup newGroup = null; RTFNode newNode = null; while (reader.ReadToken() != null) { if (reader.TokenType == RTFTokenType.GroupStart) { // begin group if (newGroup == null) { newGroup = this; } else { newGroup = new RTFNodeGroup(); newGroup.OwnerDocument = this; } if (newGroup != this) { var g = groups.Peek(); g.AppendChild(newGroup); } groups.Push(newGroup); } else if (reader.TokenType == RTFTokenType.GroupEnd) { // end group newGroup = groups.Pop(); newGroup.MergeText(); if (newGroup.FirstNode is RTFNode) { switch (newGroup.Keyword) { case RTFConsts.Fonttbl: // read font table ReadFontTable(newGroup); break; case RTFConsts.Colortbl: // read color table ReadColorTable(newGroup); break; case RTFConsts.Info: // read document information ReadDocumentInfo(newGroup); break; } } if (groups.Count > 0) { newGroup = groups.Peek(); } else { break; } //NewGroup.MergeText(); } else { // read content newNode = new RTFNode(reader.CurrentToken); newNode.OwnerDocument = this; newGroup.AppendChild(newNode); if (newNode.Keyword == RTFConsts.F) { var font = FontTable[newNode.Parameter]; if (font != null) { _myFontChartset = font.Encoding; } else { _myFontChartset = null; } //myFontChartset = RTFFont.GetRTFEncoding( NewNode.Parameter ); } else if (newNode.Keyword == RTFConsts.Af) { var font = FontTable[newNode.Parameter]; if (font != null) { _myAssociateFontChartset = font.Encoding; } else { _myAssociateFontChartset = null; } } } } // while( reader.ReadToken() != null ) while (groups.Count > 0) { newGroup = groups.Pop(); newGroup.MergeText(); } //this.UpdateInformation(); }
/// <summary> /// load rtf /// </summary> /// <param name="reader">RTF text reader</param> public void Load(RTFReader reader) { myNodes.Clear(); var groups = new System.Collections.Stack(); RTFNodeGroup NewGroup = null; RTFNode NewNode = null; while (reader.ReadToken() != null) { if (reader.TokenType == RTFTokenType.GroupStart) { // begin group if (NewGroup == null) { NewGroup = this; } else { NewGroup = new RTFNodeGroup(); NewGroup.OwnerDocument = this; } if (NewGroup != this) { var g = ( RTFNodeGroup )groups.Peek(); g.AppendChild(NewGroup); } groups.Push(NewGroup); } else if (reader.TokenType == RTFTokenType.GroupEnd) { // end group NewGroup = ( RTFNodeGroup )groups.Pop(); NewGroup.MergeText(); if (NewGroup.FirstNode is RTFNode) { switch (NewGroup.Keyword) { case RTFConsts._fonttbl: // read font table ReadFontTable(NewGroup); break; case RTFConsts._colortbl: // read color table ReadColorTable(NewGroup); break; case RTFConsts._info: // read document information ReadDocumentInfo(NewGroup); break; } } if (groups.Count > 0) { NewGroup = (RTFNodeGroup)groups.Peek(); } else { break; } //NewGroup.MergeText(); } else { // read content NewNode = new RTFNode(reader.CurrentToken); NewNode.OwnerDocument = this; NewGroup.AppendChild(NewNode); if (NewNode.Keyword == RTFConsts._f) { var font = FontTable[NewNode.Parameter]; if (font != null) { myFontChartset = font.Encoding; } else { myFontChartset = null; } //myFontChartset = RTFFont.GetRTFEncoding( NewNode.Parameter ); } else if (NewNode.Keyword == RTFConsts._af) { var font = FontTable[NewNode.Parameter]; if (font != null) { myAssociateFontChartset = font.Encoding; } else { myAssociateFontChartset = null; } } } } // while( reader.ReadToken() != null ) while (groups.Count > 0) { NewGroup = ( RTFNodeGroup )groups.Pop(); NewGroup.MergeText(); } //this.UpdateInformation(); }