Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for rtf2txt.cs

(view source code of rtf2txt.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Forms;
  7.  
  8.  
  9. namespace RobvanderWoude
  10. {
  11. 	internal class Rtf2Txt
  12. 	{
  13. 		static string progver = "1.00";
  14.  
  15.  
  16. 		static int Main( string[] args )
  17. 		{
  18. 			Encoding encoding = null;
  19.  
  20. 			if ( args.Length == 0 || args.Length > 2 || args.Contains( "/?" ) )
  21. 			{
  22. 				return ShowHelp( );
  23. 			}
  24.  
  25. 			if ( args.Contains( "/E", StringComparer.OrdinalIgnoreCase ) )
  26. 			{
  27. 				return ListEncodings( );
  28. 			}
  29.  
  30. 			string file = args[0];
  31.  
  32. 			if ( args.Length == 2 )
  33. 			{
  34. 				encoding = GetEncoding( args[1] );
  35. 			}
  36.  
  37. 			if ( File.Exists( file ) && Path.GetExtension( file ).ToLower( ) == ".rtf" )
  38. 			{
  39. 				// Use a hidden RichTextBox to convert RTF to plain text, by Wendy Zang
  40. 				// https://social.msdn.microsoft.com/Forums/vstudio/en-US/6e56af9b-d7d3-49f3-9ec4-80edde3fe54b/reading-modifying-rtf-files?forum=csharpgeneral#a64345e9-cfcb-43be-ab18-c08fae02cb2a
  41. 				RichTextBox rtbox = new RichTextBox( );
  42. 				string rtftext = File.ReadAllText( file );
  43. 				rtbox.Rtf = rtftext;
  44. 				string plaintext = rtbox.Text;
  45.  
  46. 				if ( encoding == null )
  47. 				{
  48. 					Console.WriteLine( plaintext );
  49. 				}
  50. 				else
  51. 				{
  52. 					Encoding oldencoding = Console.OutputEncoding;
  53. 					Console.OutputEncoding = encoding;
  54. 					Console.WriteLine( plaintext );
  55. 					Console.OutputEncoding = oldencoding;
  56. 				}
  57.  
  58. 				return 0;
  59. 			}
  60.  
  61. 			return ShowHelp( );
  62. 		}
  63.  
  64.  
  65. 		static Encoding GetEncoding( string myencoding )
  66. 		{
  67. 			if ( string.IsNullOrEmpty( myencoding ) )
  68. 			{
  69. 				return null;
  70. 			}
  71. 			// Get a list of available encodings
  72. 			EncodingInfo[] encodings = Encoding.GetEncodings( );
  73. 			// Try correctly spelled encodings first
  74. 			foreach ( EncodingInfo encoding in encodings )
  75. 			{
  76. 				if ( encoding.Name.ToLower( ) == myencoding.ToLower( ) )
  77. 				{
  78. 					return Encoding.GetEncoding( encoding.CodePage );
  79. 				}
  80. 			}
  81. 			// No direct match found, try again, ignoring dashes
  82. 			foreach ( EncodingInfo encoding in encodings )
  83. 			{
  84. 				if ( encoding.Name.Replace( "-", "" ).ToLower( ) == myencoding.Replace( "-", "" ).ToLower( ) )
  85. 				{
  86. 					return Encoding.GetEncoding( encoding.CodePage );
  87. 				}
  88. 			}
  89. 			// Still no match, try codepages
  90. 			foreach ( EncodingInfo encoding in encodings )
  91. 			{
  92. 				if ( encoding.CodePage.ToString( ) == myencoding )
  93. 				{
  94. 					return Encoding.GetEncoding( encoding.CodePage );
  95. 				}
  96. 			}
  97. 			// Still no match, giving up
  98. 			return null;
  99. 		}
  100.  
  101.  
  102. 		static int ListEncodings( )
  103. 		{
  104. 			try
  105. 			{
  106. 				Console.Clear( );
  107. 			}
  108. 			catch
  109. 			{
  110. 				// Console.Clear( ) throws an IO exception if the output is redirected
  111. 			}
  112. 			int columnwidth = 8;
  113. 			EncodingInfo[] allencodings = Encoding.GetEncodings( );
  114. 			List<string> allencodingnames = new List<string>( );
  115. 			foreach ( EncodingInfo enc in allencodings )
  116. 			{
  117. 				allencodingnames.Add( enc.Name );
  118. 			}
  119. 			allencodingnames.Sort( );
  120. 			foreach ( string enc in allencodingnames )
  121. 			{
  122. 				columnwidth = Math.Max( columnwidth, enc.Length );
  123. 			}
  124. 			Console.WriteLine( "{0,-" + columnwidth + "}   {1}", "Encoding", "CodePage" );
  125. 			Console.WriteLine( "{0,-" + columnwidth + "}   {1}", "========", "========" );
  126. 			foreach ( string enc in allencodingnames )
  127. 			{
  128. 				Console.WriteLine( "{0,-" + columnwidth + "}   {1}", enc, GetEncoding( enc ).CodePage );
  129. 			}
  130. 			return 0;
  131. 		}
  132.  
  133.  
  134. 		static int ShowHelp( params string[] errmsg )
  135. 		{
  136. 			#region Error Message
  137.  
  138. 			if ( errmsg.Length > 0 )
  139. 			{
  140. 				List<string> errargs = new List<string>( errmsg );
  141. 				errargs.RemoveAt( 0 );
  142. 				Console.Error.WriteLine( );
  143. 				Console.ForegroundColor = ConsoleColor.Red;
  144. 				Console.Error.Write( "ERROR:\t" );
  145. 				Console.ForegroundColor = ConsoleColor.White;
  146. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  147. 				Console.ResetColor( );
  148. 			}
  149.  
  150. 			#endregion Error Message
  151.  
  152.  
  153. 			#region Help Text
  154.  
  155. 			/*
  156. 			Rtf2Txt.exe,  Version 1.00
  157. 			Return the plain text content of a Rich Text Format (.RTF) file
  158.  
  159. 			Usage:    Rtf2Txt.exe   rtffile  [ encoding ]
  160.  
  161. 			or:       Rtf2Txt.exe   /E
  162.  
  163. 			Where:    rtffile       is the path of the file to be read (no wildcards,
  164. 			                        only .rtf extension allowed)
  165. 			          encoding      is the output encoding, e.g. UTF-8 to preserve
  166. 			                        Unicode characters, or IBM437 to convert Unicode
  167. 			                        doublequotes to ASCII
  168. 			          /E            list all available encodings
  169.  
  170. 			Notes:    If the specified encoding does not match any available encoding
  171. 			          name, the program will try again, ignoring dashes; if that does
  172. 			          not provide a match, the program will try matching the specified
  173. 			          encoding with the available encodings' codepages.
  174. 			          Return code ("errorlevel") 1 in case of errors, 0 on success.
  175.  
  176. 			Credits:  Use hidden RichTextBox to convert RTF to plain text by Wendy Zang
  177. 			          https://social.msdn.microsoft.com/Forums/vstudio/en-US
  178. 			          /6e56af9b-d7d3-49f3-9ec4-80edde3fe54b/reading-modifying-rtf-files
  179. 			          ?forum=csharpgeneral#a64345e9-cfcb-43be-ab18-c08fae02cb2a
  180.  
  181. 			Written by Rob van der Woude
  182. 			https://www.robvanderwoude.com
  183. 			*/
  184.  
  185. 			#endregion Help Text
  186.  
  187.  
  188. 			#region Display Help Text
  189.  
  190. 			Console.Error.WriteLine( );
  191.  
  192. 			Console.Error.WriteLine( "Rtf2Txt.exe,  Version {0}", progver );
  193.  
  194. 			Console.Error.WriteLine( "Return the plain text content of a Rich Text Format (.RTF) file" );
  195.  
  196. 			Console.Error.WriteLine( );
  197.  
  198. 			Console.Error.Write( "Usage:    " );
  199. 			Console.ForegroundColor = ConsoleColor.White;
  200. 			Console.Error.WriteLine( "Rtf2Txt.exe   rtffile  [ encoding ]" );
  201. 			Console.ResetColor( );
  202.  
  203. 			Console.Error.WriteLine( );
  204.  
  205. 			Console.Error.Write( "or:       " );
  206. 			Console.ForegroundColor = ConsoleColor.White;
  207. 			Console.Error.WriteLine( "Rtf2Txt.exe   /E" );
  208. 			Console.ResetColor( );
  209.  
  210. 			Console.Error.WriteLine( );
  211.  
  212. 			Console.Error.Write( "Where:    " );
  213. 			Console.ForegroundColor = ConsoleColor.White;
  214. 			Console.Error.Write( "rtffile" );
  215. 			Console.ResetColor( );
  216. 			Console.Error.WriteLine( "       is the path of the file to be read" );
  217.  
  218. 			Console.Error.WriteLine( "                        (no wildcards, only .rtf extension allowed)" );
  219.  
  220. 			Console.ForegroundColor = ConsoleColor.White;
  221. 			Console.Error.Write( "          encoding" );
  222. 			Console.ResetColor( );
  223. 			Console.Error.Write( "      is the output encoding, e.g. " );
  224. 			Console.ForegroundColor = ConsoleColor.White;
  225. 			Console.Error.Write( "UTF-8" );
  226. 			Console.ResetColor( );
  227. 			Console.Error.WriteLine( " to preserve" );
  228.  
  229. 			Console.Error.Write( "                        Unicode characters, or " );
  230. 			Console.ForegroundColor = ConsoleColor.White;
  231. 			Console.Error.Write( "IBM437" );
  232. 			Console.ResetColor( );
  233. 			Console.Error.WriteLine( " to convert Unicode" );
  234.  
  235. 			Console.Error.WriteLine( "                        doublequotes to ASCII" );
  236.  
  237. 			Console.ForegroundColor = ConsoleColor.White;
  238. 			Console.Error.Write( "          /E" );
  239. 			Console.ResetColor( );
  240. 			Console.Error.WriteLine( "            list all available encodings" );
  241.  
  242. 			Console.Error.WriteLine( );
  243.  
  244. 			Console.Error.WriteLine( "Notes:    If the specified encoding does not match any available encoding" );
  245.  
  246. 			Console.Error.WriteLine( "          name, the program will try again, ignoring dashes; if that does" );
  247.  
  248. 			Console.Error.WriteLine( "          not provide a match, the program will try matching the specified" );
  249.  
  250. 			Console.Error.WriteLine( "          encoding with the available encodings' codepages." );
  251.  
  252. 			Console.Error.WriteLine( "          Return code (\"errorlevel\") 1 in case of errors, 0 on success." );
  253.  
  254. 			Console.Error.WriteLine( );
  255.  
  256. 			Console.Error.WriteLine( "Credits:  Use hidden RichTextBox to convert RTF to plain text by Wendy Zang" );
  257.  
  258. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  259. 			Console.Error.WriteLine( "          https://social.msdn.microsoft.com/Forums/vstudio/en-US" );
  260.  
  261. 			Console.Error.WriteLine( "          /6e56af9b-d7d3-49f3-9ec4-80edde3fe54b/reading-modifying-rtf-files" );
  262.  
  263. 			Console.Error.WriteLine( "          ?forum=csharpgeneral#a64345e9-cfcb-43be-ab18-c08fae02cb2a" );
  264. 			Console.ResetColor( );
  265.  
  266. 			Console.Error.WriteLine( );
  267.  
  268. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  269.  
  270. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  271.  
  272. 			#endregion Display Help Text
  273.  
  274.  
  275. 			return 1;
  276. 		}
  277. 	}
  278. }
  279.  

page last uploaded: 2021-01-27