/// <summary> /// Read color table /// </summary> /// <param name="group"></param> private void ReadColorTable(NodeGroup group) { InternalColorTable.Clear(); var r = -1; var g = -1; var b = -1; foreach (Node 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); InternalColorTable.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); InternalColorTable.Add(c); } }
/// <summary> /// Write formatted string. Call WriteStartString before this method /// </summary> /// <param name="text">text</param> /// <param name="info">format</param> public void WriteString(string text, DocumentFormatInfo info) { if (CollectionInfo) { FontTable.Add(info.FontName); ColorTable.Add(info.TextColor); ColorTable.Add(info.BackColor); } else { WriteStartString(info); if (info.Multiline) { if (text != null) { text = text.Replace("\n", ""); var reader = new StringReader(text); var strLine = reader.ReadLine(); var count = 0; while (strLine != null) { if (count > 0) { Writer.WriteKeyword("line"); } count++; Writer.WriteText(strLine); strLine = reader.ReadLine(); } reader.Close(); } } else { Writer.WriteText(text); } WriteEndString(info); } }
/// <summary> /// Start write of formatted text /// </summary> /// <param name="info">format</param> /// <remarks> /// This function must assort with WriteEndString /// </remarks> // ReSharper disable once MemberCanBePrivate.Global public void WriteStartString(DocumentFormatInfo info) { if (CollectionInfo) { FontTable.Add(info.FontName); ColorTable.Add(info.TextColor); ColorTable.Add(info.BackColor); if (info.BorderColor.A != 0) { ColorTable.Add(info.BorderColor); } return; } if (!string.IsNullOrEmpty(info.Link)) { Writer.WriteStartGroup(); Writer.WriteKeyword("field"); Writer.WriteStartGroup(); Writer.WriteKeyword("fldinst", true); Writer.WriteStartGroup(); Writer.WriteKeyword("hich"); Writer.WriteText(" HYPERLINK \"" + info.Link + "\""); Writer.WriteEndGroup(); Writer.WriteEndGroup(); Writer.WriteStartGroup(); Writer.WriteKeyword("fldrslt"); Writer.WriteStartGroup(); } switch (info.Align) { case RtfAlignment.Left: Writer.WriteKeyword("ql"); break; case RtfAlignment.Center: Writer.WriteKeyword("qc"); break; case RtfAlignment.Right: Writer.WriteKeyword("qr"); break; case RtfAlignment.Justify: Writer.WriteKeyword("qj"); break; } Writer.WriteKeyword("plain"); int index = FontTable.IndexOf(info.FontName); if (index >= 0) { Writer.WriteKeyword("f" + index); } if (info.Bold) { Writer.WriteKeyword("b"); } if (info.Italic) { Writer.WriteKeyword("i"); } if (info.Underline) { Writer.WriteKeyword("ul"); } if (info.Strikeout) { Writer.WriteKeyword("strike"); } Writer.WriteKeyword("fs" + Convert.ToInt32(info.FontSize * 2)); // Back color index = ColorTable.IndexOf(info.BackColor); if (index >= 0) { Writer.WriteKeyword("chcbpat" + Convert.ToString(index + 1)); } index = ColorTable.IndexOf(info.TextColor); if (index >= 0) { Writer.WriteKeyword("cf" + Convert.ToString(index + 1)); } if (info.Subscript) { Writer.WriteKeyword("sub"); } if (info.Superscript) { Writer.WriteKeyword("super"); } if (info.NoWwrap) { Writer.WriteKeyword("nowwrap"); } if (info.LeftBorder || info.TopBorder || info.RightBorder || info.BottomBorder) { // Border color if (info.BorderColor.A != 0) { Writer.WriteKeyword("chbrdr"); Writer.WriteKeyword("brdrs"); Writer.WriteKeyword("brdrw10"); index = ColorTable.IndexOf(info.BorderColor); if (index >= 0) { Writer.WriteKeyword("brdrcf" + Convert.ToString(index + 1)); } } } }