using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace RobvanderWoude { internal class Rtf2Txt { static string progver = "1.00"; static int Main( string[] args ) { Encoding encoding = null; if ( args.Length == 0 || args.Length > 2 || args.Contains( "/?" ) ) { return ShowHelp( ); } if ( args.Contains( "/E", StringComparer.OrdinalIgnoreCase ) ) { return ListEncodings( ); } string file = args[0]; if ( args.Length == 2 ) { encoding = GetEncoding( args[1] ); } if ( File.Exists( file ) && Path.GetExtension( file ).ToLower( ) == ".rtf" ) { // Use a hidden RichTextBox to convert RTF to plain text, by Wendy Zang // 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 RichTextBox rtbox = new RichTextBox( ); string rtftext = File.ReadAllText( file ); rtbox.Rtf = rtftext; string plaintext = rtbox.Text; if ( encoding == null ) { Console.WriteLine( plaintext ); } else { Encoding oldencoding = Console.OutputEncoding; Console.OutputEncoding = encoding; Console.WriteLine( plaintext ); Console.OutputEncoding = oldencoding; } return 0; } return ShowHelp( ); } static Encoding GetEncoding( string myencoding ) { if ( string.IsNullOrEmpty( myencoding ) ) { return null; } // Get a list of available encodings EncodingInfo[] encodings = Encoding.GetEncodings( ); // Try correctly spelled encodings first foreach ( EncodingInfo encoding in encodings ) { if ( encoding.Name.ToLower( ) == myencoding.ToLower( ) ) { return Encoding.GetEncoding( encoding.CodePage ); } } // No direct match found, try again, ignoring dashes foreach ( EncodingInfo encoding in encodings ) { if ( encoding.Name.Replace( "-", "" ).ToLower( ) == myencoding.Replace( "-", "" ).ToLower( ) ) { return Encoding.GetEncoding( encoding.CodePage ); } } // Still no match, try codepages foreach ( EncodingInfo encoding in encodings ) { if ( encoding.CodePage.ToString( ) == myencoding ) { return Encoding.GetEncoding( encoding.CodePage ); } } // Still no match, giving up return null; } static int ListEncodings( ) { try { Console.Clear( ); } catch { // Console.Clear( ) throws an IO exception if the output is redirected } int columnwidth = 8; EncodingInfo[] allencodings = Encoding.GetEncodings( ); List allencodingnames = new List( ); foreach ( EncodingInfo enc in allencodings ) { allencodingnames.Add( enc.Name ); } allencodingnames.Sort( ); foreach ( string enc in allencodingnames ) { columnwidth = Math.Max( columnwidth, enc.Length ); } Console.WriteLine( "{0,-" + columnwidth + "} {1}", "Encoding", "CodePage" ); Console.WriteLine( "{0,-" + columnwidth + "} {1}", "========", "========" ); foreach ( string enc in allencodingnames ) { Console.WriteLine( "{0,-" + columnwidth + "} {1}", enc, GetEncoding( enc ).CodePage ); } return 0; } static int ShowHelp( params string[] errmsg ) { #region Error Message if ( errmsg.Length > 0 ) { List errargs = new List( errmsg ); errargs.RemoveAt( 0 ); Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) ); Console.ResetColor( ); } #endregion Error Message #region Help Text /* Rtf2Txt.exe, Version 1.00 Return the plain text content of a Rich Text Format (.RTF) file Usage: Rtf2Txt.exe rtffile [ encoding ] or: Rtf2Txt.exe /E Where: rtffile is the path of the file to be read (no wildcards, only .rtf extension allowed) encoding is the output encoding, e.g. UTF-8 to preserve Unicode characters, or IBM437 to convert Unicode doublequotes to ASCII /E list all available encodings Notes: If the specified encoding does not match any available encoding name, the program will try again, ignoring dashes; if that does not provide a match, the program will try matching the specified encoding with the available encodings' codepages. Return code ("errorlevel") 1 in case of errors, 0 on success. Credits: Use hidden RichTextBox to convert RTF to plain text by Wendy Zang 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 Written by Rob van der Woude https://www.robvanderwoude.com */ #endregion Help Text #region Display Help Text Console.Error.WriteLine( ); Console.Error.WriteLine( "Rtf2Txt.exe, Version {0}", progver ); Console.Error.WriteLine( "Return the plain text content of a Rich Text Format (.RTF) file" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "Rtf2Txt.exe rtffile [ encoding ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "or: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "Rtf2Txt.exe /E" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "rtffile" ); Console.ResetColor( ); Console.Error.WriteLine( " is the path of the file to be read" ); Console.Error.WriteLine( " (no wildcards, only .rtf extension allowed)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " encoding" ); Console.ResetColor( ); Console.Error.Write( " is the output encoding, e.g. " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "UTF-8" ); Console.ResetColor( ); Console.Error.WriteLine( " to preserve" ); Console.Error.Write( " Unicode characters, or " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "IBM437" ); Console.ResetColor( ); Console.Error.WriteLine( " to convert Unicode" ); Console.Error.WriteLine( " doublequotes to ASCII" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /E" ); Console.ResetColor( ); Console.Error.WriteLine( " list all available encodings" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Notes: If the specified encoding does not match any available encoding" ); Console.Error.WriteLine( " name, the program will try again, ignoring dashes; if that does" ); Console.Error.WriteLine( " not provide a match, the program will try matching the specified" ); Console.Error.WriteLine( " encoding with the available encodings' codepages." ); Console.Error.WriteLine( " Return code (\"errorlevel\") 1 in case of errors, 0 on success." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Credits: Use hidden RichTextBox to convert RTF to plain text by Wendy Zang" ); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Error.WriteLine( " https://social.msdn.microsoft.com/Forums/vstudio/en-US" ); Console.Error.WriteLine( " /6e56af9b-d7d3-49f3-9ec4-80edde3fe54b/reading-modifying-rtf-files" ); Console.Error.WriteLine( " ?forum=csharpgeneral#a64345e9-cfcb-43be-ab18-c08fae02cb2a" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "https://www.robvanderwoude.com" ); #endregion Display Help Text return 1; } } }