/// <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) { 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(); }