IModulo5
IModulo5

Reputation: 279

Remove Microsoft Word table from Windows Form in C#

My application sends text to an API using a JSON string that is then placed into a live chat. The interface is written using WinForms and makes use of regular textBoxes and imported from WPF spellBoxes. The main issue that occurs is when a user copy/pastes a MS Word formatted table, the JSON string is not accepted by the API. My guess is that some hidden characters or tags in the Word table breaks the JSON string. My Question is: How can I remove all the formatting from the string?

Here is my current code to clean the content before sending through the API: private string theCleaner(string text) { if (text.Equals(String.Empty) || text.Equals(null)) { return ""; } else { return text.Replace('\u2018', '\'') .Replace('\u2019', '\'') .Replace('\u201c', '"') .Replace('\u201d', '"')
.Replace("\"", "\\\"") .Replace('\u0009', ' ') .TrimEnd() .TrimStart() .Normalize(); } } private string theScrubber(string toughGreese) { if (toughGreese.Equals(String.Empty) || toughGreese.Equals(null)) { return ""; } else { return Regex.Replace(toughGreese, "[^\\\"\\s\\r\\n%#&\\w\\.@-]", ""); } }

Upvotes: 0

Views: 66

Answers (1)

IModulo5
IModulo5

Reputation: 279

This was solved by cleaning all MS Word format string endings.

So adding these three statements solved the issue:

.Replace("\r\n", "<br>").Replace("\r","<br>").Replace("\n","<br>");

The additional \r and \n might be unnecessary but you never can tell with MS decides to change their format what will break your code in the future.

Upvotes: 1

Related Questions