using System; using System.ComponentModel; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Text.RegularExpressions; namespace System.Windows.Forms { public partial class MyRTB : RichTextBox { public MyRTB() { InitializeComponent(); } public MyRTB(IContainer container) { container.Add(this); InitializeComponent(); } private void InitializeComponent() { //throw new NotImplementedException(); } public static string StaticStripRtfCommands(string retval) { // replace \{ & \} with (![ & (!] respectively and then redo at end. The curly braces // are rtf so were getting removed and/or not handled correctly. retval = retval.Replace(@"\{", @" (!["); retval = retval.Replace(@"\}", @" (!]"); // For hardspaces, the windows richtextbox does some 'quirky' things: // A unicode representation of \u160? is sent INTO the rtb. Coming out, // that \u160? was translated to a \~ (by the underlying windows rtb). // Note that if the \~ is sent to the rtb, it is treated as a regular space, // i.e. no longer a hardspace, and actually is converted to a regular space. // SO, on the way out, convert any \~ to \u160? //retval = retval.Replace(@"\~", @"\u160?"); // convert \'99 to \u8482? this is for the trade mark symbol. For some reason RTF is automatically // converting the unicode \u8482? to \'99, but once this is done, PROMS StepRTB (edit windows) do not show it //retval = retval.Replace(@"\'99", @"\u8482?"); retval = RTFConvertedSymbolsToUnicode(retval); // remove carriage return/newlines after \par commands (these are introduced by rtb // for hard returns, goes into rtb as \par and comes out as \par\r\n): retval = sreg1.Replace(retval, "\\par "); retval = retval.Replace("\\v0\r\n", "\\v0 "); // Replace Carriage Return and Newline after comment retval = sreg2.Replace(retval, ""); // Strip Carriage Returns and Newlines retval = sreg3.Replace(retval, "$1"); // Strip Opening and Closing Braces retval = sreg4.Replace(retval, ""); // Strip Clauses - remove anything from curly braces retval = sreg5.Replace(retval, ""); // Strip Clauses - remove anything from curly braces retval = sreg6.Replace(retval, "$1 "); // add space after token if followed by token //retval = sreg7.Replace(retval, new MatchEvaluator(StaticReplaceRTFClause)); // take backslash xyz and evaluates them retval = sreg8.Replace(retval, "$1"); // remove space between tokens retval = sreg9.Replace(retval, "$1"); // remove space before /r/n //retval = Regex.Replace(retval, @"\\par\r\n(?!\\)", "\\par "); ////retval = Regex.Replace(retval, @"\\par\r\n(?=\\)", "\\par"); //retval = retval.Replace("\\v0\r\n", "\\v0 "); // Replace Carriage Return and Newline after comment //retval = Regex.Replace(retval, @"[\r\n]", "", RegexOptions.Singleline); // Strip Carriage Returns and Newlines //retval = Regex.Replace(retval, @"^\{(.*)\}$", "$1", RegexOptions.Singleline); // Strip Opening and Closing Braces //retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces //retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces ////retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+)\\f[0-9] ", "$1 "); // remove font command - if next to another command, keep other command and space //// OLD: The following two lines are replaced by the more generic replace above. ////retval = Regex.Replace(retval, @"\\v\\f[0-9] ", "\\v "); // remove font command - if next to Comment keep space ////retval = Regex.Replace(retval, @"\\line\\f[0-9] ", "\\line "); // remove font command - if next to Line keep space ////retval = Regex.Replace(retval, @"\\f[0-9] ", ""); // remove font command with ending space ////retval = Regex.Replace(retval, @"\\f[0-9]", ""); // remove font command without ending space ////retval = Regex.Replace(retval, @"\\par ", "\r\n"); //retval = Regex.Replace(retval, @"(\\[^' \\?\r\n\t]+)(?=\\)", "$1 "); // add space after token if followed by token ////retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+)(\\)", "$1 $2"); // take backslash xyz and evaluates them //retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+ )", new MatchEvaluator(StaticReplaceRTFClause)); // take backslash xyz and evaluates them //retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+) (?=\\)", "$1"); // remove space between tokens //retval = Regex.Replace(retval, @"(\\[^ \\?\r\n\t]+) (?=\r\n)", "$1"); // remove space before /r/n // remove a space if there is one as the first character or the last character //if (retval[0] == ' ') retval = retval.Remove(0, 1); //retval = retval.TrimEnd(' '); // remove \r\n at end of string if the string has 2 or more characters if (retval.Length > 1 && retval.Substring(retval.Length - 2, 2) == "\r\n") retval = retval.Remove(retval.Length - 2, 2); // remove \par at end of string if the string has 4 or more characters //if (retval.Length > 3 && retval.Substring(retval.Length - 4, 4) == @"\par") retval = retval.Remove(retval.Length - 4, 4); // remove a space following \r\n //retval = Regex.Replace(retval, "\r\n ", "\r\n"); ////if there are still spaces following \r\n, then probable in a table - we need to put the space back //retval = Regex.Replace(retval, "\r\n ", "\r\n "); if (retval.Length == 0) return ""; if (retval.Length > 1 && retval.Substring(retval.Length - 2, 2) == @"\v") retval = retval.Remove(retval.Length - 2, 2); //retval = RemoveRtfStyles(retval); //if (itmInfo != null) // retval = StaticRemoveRtfStyles(retval, itmInfo); retval = retval.Replace(@" (![", @"\{"); retval = retval.Replace(@" (!]", @"\}"); retval = retval.TrimEnd(' '); // the indent character was translated in the richtextbox, change it back: if (retval.IndexOf(@"\'05") > -1) retval = retval.Replace(@"\'05", "\x05"); return retval; } private static string RTFConvertedSymbolsToUnicode(string str) { string rtnStr = str; // convert \~ to a hard spece. RTF is automatically converting \u160? to \~ but will then convert // the \~ to a regular space! rtnStr = rtnStr.Replace(@"\~", @"\u160?"); rtnStr = rtnStr.Replace(@"\'a0", @"\u160?"); // convert \'99 to \u8482? this is for the trade mark symbol. RTF is automatically // converting the unicode \u8482? to \'99, but once this is done, PROMS StepRTB (edit windows) does not show it rtnStr = rtnStr.Replace(@"\'99", @"\u8482?"); // convert \'ae to \u174? this is for the registered symbol. RTF converts the unicode character to \'ae rtnStr = rtnStr.Replace(@"\'ae", @"\u174?"); // convert \'a9 to \u169? this is for the copyright symbol. RTF converts the unicode character to \'a9 rtnStr = rtnStr.Replace(@"\'a9", @"\u169?"); return rtnStr; } private static Regex sreg1 = new Regex(@"\\par\r\n(?!\\)"); private static Regex sreg2 = new Regex(@"[\r\n]", RegexOptions.Singleline); // Strip Carriage Returns and Newlines private static Regex sreg3 = new Regex(@"^\{(.*)\}$", RegexOptions.Singleline); // Strip Opening and Closing Braces private static Regex sreg4 = new Regex(@"\{[^{]*?\}", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces private static Regex sreg5 = new Regex(@"\{[^{]*?\}", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces private static Regex sreg6 = new Regex(@"(\\[^' \\?\r\n\t]+)(?=\\)"); // add space after token if followed by token private static Regex sreg7 = new Regex(@"(\\[^ \\?\r\n\t]+ )"); // take backslash xyz and evaluates them private static Regex sreg8 = new Regex(@"(\\[^ \\?\r\n\t]+) (?=\\)"); // remove space between tokens private static Regex sreg9 = new Regex(@"(\\[^ \\?\r\n\t]+) (?=\r\n)"); // remove space before /r/n } }