Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for word2any.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using Word = Microsoft.Office.Interop.Word;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class Word2Any
  11. 	{
  12. 		static string progver = "1.02";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			#region Initialize Variables
  18.  
  19. 			bool overwrite = false;
  20. 			string inputfilespec = null;
  21. 			string outputfilespec = null;
  22. 			string inputfolder;
  23. 			string outputfolder;
  24. 			string inputfile;
  25. 			string outputfile;
  26. 			string inputfilename;
  27. 			string outputfilename;
  28. 			string inputfileext;
  29. 			string outputfileext = null;
  30. 			string outputrange = null;
  31. 			int outputrangestart = 0;
  32. 			int outputrangeend = -1;
  33. 			Word.WdOpenFormat inputformat = Word.WdOpenFormat.wdOpenFormatAuto;
  34. 			Word.WdSaveFormat outputformat = Word.WdSaveFormat.wdFormatDocument;
  35.  
  36. 			#endregion Initialize Variables
  37.  
  38.  
  39. 			#region Command Line Parsing
  40.  
  41. 			if ( args.Length == 0 || ( args.Length == 1 && args[0].ToUpper( ) != "/T" ) )
  42. 			{
  43. 				return ShowHelp( );
  44. 			}
  45. 			foreach ( string arg in args )
  46. 			{
  47. 				if ( arg[0] == '/' )
  48. 				{
  49. 					if ( arg.ToString( ).ToUpper( ) == "/O" )
  50. 					{
  51. 						if ( overwrite )
  52. 						{
  53. 							return ShowHelp( "Duplicate command line switch /O" );
  54. 						}
  55. 						overwrite = true;
  56. 						break;
  57. 					}
  58. 					else if ( arg.ToString( ).ToUpper( ) == "/T" )
  59. 					{
  60. 						ListFormats( );
  61. 						return 0;
  62. 					}
  63. 					else if ( arg.Length > 3 && arg.Substring( 0, 3 ).ToUpper( ) == "/T:" )
  64. 					{
  65. 						if ( outputformat != Word.WdSaveFormat.wdFormatDocument )
  66. 						{
  67. 							return ShowHelp( "Duplicate command line switch /T" );
  68. 						}
  69. 						outputformat = GetOutputFormat( arg.Substring( 3 ) );
  70. 						if ( outputformat == Word.WdSaveFormat.wdFormatDocument )
  71. 						{
  72. 							return ShowHelp( "Output file type not recognized, use /T to list all available types" );
  73. 						}
  74. 					}
  75. 					else
  76. 					{
  77. 						return ShowHelp( "Invalid command line switch {0}", arg );
  78. 					}
  79. 				}
  80. 				else
  81. 				{
  82. 					if ( String.IsNullOrEmpty( inputfilespec ) )
  83. 					{
  84. 						inputfilespec = arg;
  85. 					}
  86. 					else if ( String.IsNullOrEmpty( outputfilespec ) )
  87. 					{
  88. 						outputfilespec = arg;
  89. 					}
  90. 					else if ( String.IsNullOrEmpty( outputrange ) )
  91. 					{
  92. 						outputrange = arg;
  93. 					}
  94. 					else
  95. 					{
  96. 						return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  97. 					}
  98. 				}
  99. 			}
  100.  
  101. 			#endregion Command Line Parsing
  102.  
  103.  
  104. 			#region Command Line Validation
  105.  
  106. 			#region Input File Validation
  107.  
  108. 			// Validate input filespec
  109. 			if ( String.IsNullOrEmpty( inputfilespec ) )
  110. 			{
  111. 				return ShowHelp( "Please specify an input file" );
  112. 			}
  113. 			switch ( ValidateFilespec( inputfilespec ) )
  114. 			{
  115. 				case -1:
  116. 					if ( inputfilespec.IndexOf( '*' ) == -1 )
  117. 					{
  118. 						return ShowHelp( "Parent folder of input file not found" );
  119. 					}
  120. 					else
  121. 					{
  122. 						return ShowHelp( "Parent folder of input files not found" );
  123. 					}
  124. 				case 0:
  125. 					if ( inputfilespec.IndexOf( '*' ) == -1 )
  126. 					{
  127. 						return ShowHelp( "Input file not found" );
  128. 					}
  129. 					else
  130. 					{
  131. 						return ShowHelp( "No matching input files found" );
  132. 					}
  133. 				case 1:
  134. 					break;
  135. 				default:
  136. 					if ( !String.IsNullOrEmpty( outputfilespec ) && Path.GetFileNameWithoutExtension( outputfilespec ) != "*" )
  137. 					{
  138. 						return ShowHelp( "When using wildcards in the input file names,\n\tyou must use wildcard \"*\" for the output file names, if specified" );
  139. 					}
  140. 					break;
  141. 			}
  142. 			inputfolder = Directory.GetParent( inputfilespec ).FullName;
  143. 			inputfile = Path.GetFileName( inputfilespec );
  144. 			inputfilename = Path.GetFileNameWithoutExtension( inputfilespec );
  145. 			inputfileext = Path.GetExtension( inputfilespec );
  146. 			inputformat = GetInputFormatByExtension( inputfilespec );
  147.  
  148. 			#endregion Input File Validation
  149.  
  150. 			#region Output File Validation
  151.  
  152. 			// Validate or build output filespec
  153. 			if ( String.IsNullOrEmpty( outputfilespec ) )
  154. 			{
  155. 				if ( outputformat == Word.WdSaveFormat.wdFormatDocument )
  156. 				{
  157. 					return ShowHelp( "Please specify output file(s) and/or output file type" );
  158. 				}
  159. 				outputfolder = inputfolder;
  160. 				outputfile = "*";
  161. 				outputfilename = "*";
  162. 				outputfilespec = Path.Combine( outputfolder, outputfile );
  163. 				// Extension will be default extension based on file type
  164. 			}
  165. 			else
  166. 			{
  167. 				if ( outputfilespec.IndexOf( '\\' ) == -1 )
  168. 				{
  169. 					outputfolder = inputfolder;
  170. 					outputfile = Path.GetFileName( outputfilespec );
  171. 					outputfilename = Path.GetFileNameWithoutExtension( outputfilespec );
  172. 					outputfileext = Path.GetExtension( outputfilespec );
  173. 					outputfilespec = Path.Combine( outputfolder, outputfile );
  174. 				}
  175. 				else
  176. 				{
  177. 					outputfolder = Directory.GetParent( outputfilespec ).FullName;
  178. 					outputfile = Path.GetFileName( outputfilespec );
  179. 					outputfilename = Path.GetFileNameWithoutExtension( outputfilespec );
  180. 					outputfileext = Path.GetExtension( outputfilespec );
  181. 				}
  182. 				if ( ValidateFilespec( outputfilespec ) == -1 )
  183. 				{
  184. 					if ( outputfilespec.IndexOf( '*' ) == -1 )
  185. 					{
  186. 						return ShowHelp( "Parent folder for output file not found" );
  187. 					}
  188. 					else
  189. 					{
  190. 						return ShowHelp( "Parent folder for output files not found" );
  191. 					}
  192. 				}
  193. 			}
  194. 			if ( outputformat == Word.WdSaveFormat.wdFormatDocument )
  195. 			{
  196. 				outputformat = GetOutputFormatByExtension( outputfilespec );
  197. 			}
  198.  
  199. 			#endregion Output File Validation
  200.  
  201. 			// Input and output file types should be different
  202. 			if ( inputformat == Word.WdOpenFormat.wdOpenFormatAuto && outputformat == Word.WdSaveFormat.wdFormatDocument )
  203. 			{
  204. 				return ShowHelp( "Input and output file types should be different" );
  205. 			}
  206. 			// Input and output extensions should be different
  207. 			if ( inputfileext == outputfileext )
  208. 			{
  209. 				return ShowHelp( "Input and output file extensions should be different" );
  210. 			}
  211.  
  212. 			#region Page range Validation
  213.  
  214. 			if ( !String.IsNullOrEmpty( outputrange ) )
  215. 			{
  216. 				bool error = true;
  217. 				string pattern = @"^(\d+)(?:-(\d+))?$";
  218. 				Regex regex = new Regex( pattern );
  219. 				if ( regex.IsMatch( outputrange ) )
  220. 				{
  221. 					MatchCollection matches = regex.Matches( outputrange );
  222. 					if ( matches.Count == 1 )
  223. 					{
  224. 						foreach ( Match match in matches )
  225. 						{
  226. 							//							try
  227. 							{
  228. 								if ( match.Groups.Count == 2 || ( match.Groups.Count == 3 && String.IsNullOrEmpty( match.Groups[2].ToString( ) ) ) )
  229. 								{
  230. 									outputrangestart = Convert.ToInt32( match.Groups[1].ToString( ) );
  231. 									outputrangeend = Convert.ToInt32( match.Groups[1].ToString( ) );
  232. 								}
  233. 								else if ( match.Groups.Count == 3 )
  234. 								{
  235. 									outputrangestart = Convert.ToInt32( match.Groups[1].ToString( ) );
  236. 									outputrangeend = Convert.ToInt32( match.Groups[2].ToString( ) );
  237.  
  238. 								}
  239. 								if ( outputrangeend >= outputrangestart )
  240. 								{
  241. 									error = false;
  242. 								}
  243. 							}
  244. 							//							catch { }
  245. 						}
  246. 					}
  247. 				}
  248. 				if ( error )
  249. 				{
  250. 					return ShowHelp( "Invalid page range: \"{0}\"", outputrange );
  251. 				}
  252. 			}
  253.  
  254. 			#endregion Page range Validation
  255.  
  256. 			#endregion Command Line Validation
  257.  
  258.  
  259. 			#region Iterate File List and Convert Each File
  260.  
  261. 			foreach ( string file in Directory.GetFiles( inputfolder, inputfile ) )
  262. 			{
  263. 				if ( Path.GetExtension( file ) == inputfileext ) // prevent including *.docx when *.doc is specified
  264. 				{
  265. 					string output;
  266. 					if ( inputfilename.IndexOf( '*' ) > -1 )
  267. 					{
  268. 						output = Path.Combine( outputfolder, Path.GetFileNameWithoutExtension( file ) + outputfileext );
  269. 					}
  270. 					else
  271. 					{
  272. 						output = outputfilespec;
  273. 					}
  274. 					if ( File.Exists( output ) && !overwrite )
  275. 					{
  276. 						if ( inputfilename.IndexOf( '*' ) > -1 )
  277. 						{
  278. 							Console.WriteLine( "Skipped \"{0}\" because \"{1}\" already exists", Path.GetFileName( file ), Path.GetFileName( output ) );
  279. 						}
  280. 						else
  281. 						{
  282. 							return ShowHelp( "Output file \"{0}\" already exists, use /O to silently overwrite existing files", Path.GetFileName( output ) );
  283. 						}
  284. 					}
  285. 					else
  286. 					{
  287. 						Console.Write( "Converting \"{0}\" . . . ", Path.GetFileName( file ) );
  288. 						Console.WriteLine( WordConvert( file, output, outputformat, outputrangestart, outputrangeend ) ? "Success" : "Failed" );
  289. 					}
  290. 				}
  291. 			}
  292.  
  293. 			#endregion Iterate File List and Convert Each File
  294.  
  295.  
  296. 			return 0;
  297. 		}
  298.  
  299.  
  300. 		static Word.WdOpenFormat GetInputFormatByExtension( string file )
  301. 		{
  302. 			string ext = Path.GetExtension( file ).ToLower( ).Substring( 1 );
  303. 			Dictionary<string, Word.WdOpenFormat> knownwordexts = new Dictionary<string, Word.WdOpenFormat>( );
  304. 			knownwordexts["doc"] = Word.WdOpenFormat.wdOpenFormatDocument;
  305. 			knownwordexts["docx"] = Word.WdOpenFormat.wdOpenFormatDocument;
  306. 			knownwordexts["odt"] = Word.WdOpenFormat.wdOpenFormatOpenDocumentText;
  307. 			knownwordexts["rtf"] = Word.WdOpenFormat.wdOpenFormatRTF;
  308. 			knownwordexts["txt"] = Word.WdOpenFormat.wdOpenFormatText;
  309. 			knownwordexts["xml"] = Word.WdOpenFormat.wdOpenFormatXML;
  310. 			if ( knownwordexts.ContainsKey( ext ) )
  311. 			{
  312. 				return knownwordexts[ext];
  313. 			}
  314. 			return Word.WdOpenFormat.wdOpenFormatAuto;
  315. 		}
  316.  
  317.  
  318. 		static Word.WdSaveFormat GetOutputFormat( string format )
  319. 		{
  320. 			// test for numeric fomat (type number)
  321. 			try { return (Word.WdSaveFormat) Convert.ToInt32( format ); }
  322. 			catch { }
  323. 			// test for string format (type name)
  324. 			for ( int i = 0; i < 64; i++ )
  325. 			{
  326. 				try
  327. 				{
  328. 					if ( ( (Word.WdSaveFormat) i ).ToString( ) != i.ToString( ) )
  329. 					{
  330. 						string type = ( (Word.WdSaveFormat) i ).ToString( );
  331. 						if ( format.ToUpper( ) == type.ToUpper( ) )
  332. 						{
  333. 							return (Word.WdSaveFormat) i;
  334. 						}
  335. 					}
  336. 				}
  337. 				catch { }
  338. 			}
  339. 			// return default if format not valid
  340. 			return Word.WdSaveFormat.wdFormatDocument;
  341. 		}
  342.  
  343.  
  344. 		static Word.WdSaveFormat GetOutputFormatByExtension( string file )
  345. 		{
  346. 			string ext = Path.GetExtension( file ).ToLower( ).Substring( 1 );
  347. 			Dictionary<string, Word.WdSaveFormat> knownextensions = new Dictionary<string, Word.WdSaveFormat>( );
  348. 			knownextensions["htm"] = Word.WdSaveFormat.wdFormatFilteredHTML;
  349. 			knownextensions["html"] = Word.WdSaveFormat.wdFormatFilteredHTML;
  350. 			knownextensions["odt"] = Word.WdSaveFormat.wdFormatOpenDocumentText;
  351. 			knownextensions["pdf"] = Word.WdSaveFormat.wdFormatPDF;
  352. 			knownextensions["rtf"] = Word.WdSaveFormat.wdFormatRTF;
  353. 			knownextensions["txt"] = Word.WdSaveFormat.wdFormatDOSText;
  354. 			knownextensions["xml"] = Word.WdSaveFormat.wdFormatFlatXML;
  355. 			knownextensions["xps"] = Word.WdSaveFormat.wdFormatXPS;
  356. 			if ( knownextensions.ContainsKey( ext ) )
  357. 			{
  358. 				return knownextensions[ext];
  359. 			}
  360. 			return Word.WdSaveFormat.wdFormatDocument;
  361. 		}
  362.  
  363.  
  364. 		static void ListFormats( )
  365. 		{
  366. 			int maxlen = 0;
  367. 			for ( int i = 0; i < 64; i++ )
  368. 			{
  369. 				if ( ( (Word.WdSaveFormat) i ).ToString( ).Length > maxlen )
  370. 				{
  371. 					maxlen = ( (Word.WdSaveFormat) i ).ToString( ).Length;
  372. 				}
  373. 			}
  374. 			ConsoleColor bgblue = ConsoleColor.DarkBlue;
  375. 			ConsoleColor bgdefault = Console.BackgroundColor;
  376. 			Console.ForegroundColor = ConsoleColor.White;
  377. 			Console.WriteLine( String.Format( "{0,-" + maxlen + "}  {1}", "File Type", "Number" ) );
  378. 			if ( maxlen > 12 )
  379. 			{
  380. 				Console.WriteLine( new String( '=', 12 ) + new String( ' ', maxlen - 12 + 2 ) + new String( '=', 6 ) );
  381. 			}
  382. 			else
  383. 			{
  384. 				Console.WriteLine( new String( '=', maxlen ) + "  " + new String( '=', 6 ) );
  385. 			}
  386. 			int linenum = 0;
  387. 			for ( int i = 0; i < 64; i++ )
  388. 			{
  389. 				if ( ( (Word.WdSaveFormat) i ).ToString( ) != i.ToString( ) )
  390. 				{
  391. 					if ( linenum % 2 == 1 )
  392. 					{
  393. 						Console.BackgroundColor = bgblue;
  394. 					}
  395. 					Console.Write( String.Format( "{0,-" + maxlen + "}  {1,4}  ", (Word.WdSaveFormat) i, i ) );
  396. 					if ( linenum % 2 == 1 )
  397. 					{
  398. 						Console.BackgroundColor = bgdefault;
  399. 					}
  400. 					Console.WriteLine( );
  401. 					linenum += 1;
  402. 				}
  403. 			}
  404. 			Console.ResetColor( );
  405. 		}
  406.  
  407.  
  408. 		static int ShowHelp( params string[] errmsg )
  409. 		{
  410. 			#region Help Text
  411.  
  412. 			/*
  413. 			Word2Any,  Version 1.02
  414. 			Open a Microsoft Word document and save it in "any" (known) format
  415.  
  416. 			Usage:     WORD2ANY    "wordfile"  [ "outfile"  [ pages ] ]  [ options ]
  417.  
  418. 			Where:     "wordfile"  Word document(s) to be converted (wildcard "*" allowed
  419. 			                       in file name, e.g. "name*.docx")
  420. 			           "outfile"   output file(s) to be created (wildcard "*" allowed for
  421. 			                       file name, e.g. "*.pdf" or "*.html")
  422. 			           pages       page range to be saved (e.g. 5 or 1-3; default: all)
  423.  
  424. 			Options:   /O          silently overwrite existing output file(s)
  425. 			                       (default: abort or skip if output file exists)
  426. 			           /T          list available output file types
  427. 			           /T:type     set output file type (required if "outfile" is not
  428. 			                       specified; type may be number or string)
  429.  
  430. 			Notes: [1] This program requires a "regular" (MSI based) Microsoft Word
  431. 			           (2007 or later) installation, it will fail on an MS Office
  432. 			           "click-to-run" installation.
  433. 			       [2] For Word 2007, to save as PDF or XPS, this program requires the
  434. 			           "Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office
  435. 			           programs", available at:
  436. 			           http://www.microsoft.com/en-us/download/details.aspx?id=7
  437. 			       [3] If wildcards are used in the Word file names, and the output file
  438. 			           path is not specified, /T:type must be used, and the input file
  439. 			           names should not contain dots.
  440. 			       [4] If wildcards are used in the Word file names, and the output file
  441. 			           path is specified, the output file name must be "*".
  442. 			       [5] If wildcards are used in the Word file names, and the /O switch
  443. 			           is not used, the program will display an error message in case an
  444. 			           output file already exists, but it will then continue to convert
  445. 			           the next file instead of aborting.
  446. 			       [6] If a page range is specified, the selected pages will be copied
  447. 			           and pasted to a temporary document, which will then be saved;
  448. 			           this may affect page numbers.
  449. 			       [7] If Word was already active when this program is started, any other
  450. 			           opened document(s) will be left alone, and only the document(s)
  451. 			           opened by this program will be closed.
  452.  
  453. 			Examples:  WORD2ANY "D:\folder\myfile.doc" *.pdf
  454. 			           will save to "D:\folder\myfile.pdf"
  455.  
  456. 			           WORD2ANY "D:\folder\myfile.docx" "D:\otherfolder\*.rtf"
  457. 			           will save to "D:\otherfolder\myfile.rtf"
  458.  
  459. 			           WORD2ANY "D:\folder\myfile.rtf" "D:\elsewhere\page3.xps" 3
  460. 			           will save page 3 of "myfile.rtf" to "D:\elsewhere\page3.xps"
  461.  
  462. 			           WORD2ANY "D:\folder\name*.doc" *.html 1-2
  463. 			           will save pages 1 and 2 of all matching files as HTML in "D:\folder"
  464. 			           recognized extensions: htm, html, odt, pdf, rtf, txt, xml, xps
  465.  
  466. 			           WORD2ANY "D:\folder\*.doc" /T:8
  467. 			           will save all matching files as HTML in "D:\folder"
  468.  
  469. 			           WORD2ANY /T
  470. 			           will list all available file types
  471.  
  472. 			Credits:   Page range selection based on code by George Hua
  473. 			           http://social.msdn.microsoft.com/Forums/office/en-US/e48b3126-
  474. 			           941d-490a-85ee-e327bbe7e81b/convert-specific-word-pages-to-pdf-in-c
  475.  
  476. 			Written by Rob van der Woude
  477. 			http://www.robvanderwoude.com
  478. 			*/
  479.  
  480. 			if ( errmsg.Length > 0 )
  481. 			{
  482. 				List<string> errargs = new List<string>( errmsg );
  483. 				errargs.RemoveAt( 0 );
  484. 				Console.Error.WriteLine( );
  485. 				Console.ForegroundColor = ConsoleColor.Red;
  486. 				Console.Error.Write( "ERROR:\t" );
  487. 				Console.ForegroundColor = ConsoleColor.White;
  488. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  489. 				Console.ResetColor( );
  490. 			}
  491.  
  492. 			Console.Error.WriteLine( );
  493.  
  494. 			Console.Error.WriteLine( "Word2Any,  Version {0}", progver );
  495.  
  496. 			Console.Error.WriteLine( "Open a Microsoft Word document and save it in \"any\" (known) format" );
  497.  
  498. 			Console.Error.WriteLine( );
  499.  
  500. 			Console.Error.Write( "Usage:     " );
  501. 			Console.ForegroundColor = ConsoleColor.White;
  502. 			Console.Error.WriteLine( "WORD2ANY    \"wordfile\"  [ \"outfile\"  [ pages ] ]  [ options ]" );
  503. 			Console.ResetColor( );
  504.  
  505. 			Console.Error.WriteLine( );
  506.  
  507. 			Console.Error.Write( "Where:     " );
  508. 			Console.ForegroundColor = ConsoleColor.White;
  509. 			Console.Error.Write( "\"wordfile\"" );
  510. 			Console.ResetColor( );
  511. 			Console.Error.WriteLine( "  Word document(s) to be converted (wildcard \"*\" allowed" );
  512.  
  513. 			Console.Error.WriteLine( "                       in file name, e.g. \"name*.docx\")" );
  514.  
  515. 			Console.ForegroundColor = ConsoleColor.White;
  516. 			Console.Error.Write( "           \"outfile\"" );
  517. 			Console.ResetColor( );
  518. 			Console.Error.WriteLine( "   output file(s) to be created (wildcard \"*\" allowed for" );
  519.  
  520. 			Console.Error.WriteLine( "                       file name, e.g. \"*.pdf\" or \"*.html\")" );
  521.  
  522. 			Console.ForegroundColor = ConsoleColor.White;
  523. 			Console.Error.Write( "           pages       page range" );
  524. 			Console.ResetColor( );
  525. 			Console.Error.Write( " to be saved (e.g. " );
  526. 			Console.ForegroundColor = ConsoleColor.White;
  527. 			Console.Error.Write( "5" );
  528. 			Console.ResetColor( );
  529. 			Console.Error.Write( " or " );
  530. 			Console.ForegroundColor = ConsoleColor.White;
  531. 			Console.Error.Write( "1-3" );
  532. 			Console.ResetColor( );
  533. 			Console.Error.WriteLine( "; default: all)" );
  534.  
  535. 			Console.Error.WriteLine( );
  536.  
  537. 			Console.Error.Write( "Options:   " );
  538. 			Console.ForegroundColor = ConsoleColor.White;
  539. 			Console.Error.Write( "/O" );
  540. 			Console.ResetColor( );
  541. 			Console.Error.Write( "          silently " );
  542. 			Console.ForegroundColor = ConsoleColor.White;
  543. 			Console.Error.Write( "O" );
  544. 			Console.ResetColor( );
  545. 			Console.Error.WriteLine( "verwrite existing output file(s)" );
  546.  
  547. 			Console.Error.WriteLine( "                       (default: abort or skip if output file exists)" );
  548.  
  549. 			Console.ForegroundColor = ConsoleColor.White;
  550. 			Console.Error.Write( "           /T" );
  551. 			Console.ResetColor( );
  552. 			Console.Error.Write( "          list available output file " );
  553. 			Console.ForegroundColor = ConsoleColor.White;
  554. 			Console.Error.Write( "T" );
  555. 			Console.ResetColor( );
  556. 			Console.Error.WriteLine( "ypes" );
  557.  
  558.  
  559. 			Console.ForegroundColor = ConsoleColor.White;
  560. 			Console.Error.Write( "           /T:type" );
  561. 			Console.ResetColor( );
  562. 			Console.Error.Write( "     set output file " );
  563. 			Console.ForegroundColor = ConsoleColor.White;
  564. 			Console.Error.Write( "T" );
  565. 			Console.ResetColor( );
  566. 			Console.Error.Write( "ype (required if " );
  567. 			Console.ForegroundColor = ConsoleColor.White;
  568. 			Console.Error.Write( "\"outfile\"" );
  569. 			Console.ResetColor( );
  570. 			Console.Error.WriteLine( " is not" );
  571.  
  572. 			Console.Error.Write( "                       specified; " );
  573. 			Console.ForegroundColor = ConsoleColor.White;
  574. 			Console.Error.Write( "type" );
  575. 			Console.ResetColor( );
  576. 			Console.Error.WriteLine( " may be number or string)" );
  577.  
  578. 			Console.Error.WriteLine( );
  579.  
  580. 			Console.Error.WriteLine( "Notes: [1] This program requires a \"regular\" (MSI based) Microsoft Word" );
  581.  
  582. 			Console.Error.WriteLine( "           (2007 or later) installation, it will fail on an MS Office" );
  583.  
  584. 			Console.Error.WriteLine( "           \"click-to-run\" installation" );
  585.  
  586. 			Console.Error.WriteLine( "       [2] For Word 2007, to save as PDF or XPS, this program requires the" );
  587.  
  588. 			Console.Error.WriteLine( "           \"Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office" );
  589.  
  590. 			Console.Error.WriteLine( "           programs\", available at:" );
  591.  
  592. 			Console.Error.WriteLine( "           http://www.microsoft.com/en-us/download/details.aspx?id=7" );
  593.  
  594. 			Console.Error.WriteLine( "       [3] If wildcards are used in the Word file names, and the output file" );
  595.  
  596. 			Console.Error.Write( "           path is not specified, " );
  597. 			Console.ForegroundColor = ConsoleColor.White;
  598. 			Console.Error.Write( "/T:type" );
  599. 			Console.ResetColor( );
  600. 			Console.Error.WriteLine( " must be used, and the input file" );
  601.  
  602. 			Console.Error.WriteLine( "           names should not contain dots." );
  603.  
  604. 			Console.Error.WriteLine( "       [4] If wildcards are used in the Word file names, and the output file" );
  605.  
  606. 			Console.Error.WriteLine( "           path is specified, the output file name must be \"*\"." );
  607.  
  608. 			Console.Error.Write( "       [5] If wildcards are used in the Word file names, and the " );
  609. 			Console.ForegroundColor = ConsoleColor.White;
  610. 			Console.Error.Write( "/O" );
  611. 			Console.ResetColor( );
  612. 			Console.Error.WriteLine( " switch" );
  613.  
  614. 			Console.Error.WriteLine( "           is not used, the program will display an error message in case an" );
  615.  
  616. 			Console.Error.WriteLine( "           output file already exists, but it will then continue to convert" );
  617.  
  618. 			Console.Error.WriteLine( "           the next file instead of aborting." );
  619.  
  620. 			Console.Error.WriteLine( "       [6] If a page range is specified, the selected pages will be copied" );
  621.  
  622. 			Console.Error.WriteLine( "           and pasted to a temporary document, which will then be saved;" );
  623.  
  624. 			Console.Error.WriteLine( "           this may affect page numbers." );
  625.  
  626. 			Console.Error.WriteLine( "       [7] If Word was already active when this program is started, any other" );
  627.  
  628. 			Console.Error.WriteLine( "           opened document(s) will be left alone, and only the document(s)" );
  629.  
  630. 			Console.Error.WriteLine( "           opened by this program will be closed." );
  631.  
  632. 			Console.Error.WriteLine( );
  633.  
  634. 			Console.Error.Write( "Examples:  " );
  635. 			Console.ForegroundColor = ConsoleColor.White;
  636. 			Console.Error.WriteLine( "WORD2ANY \"D:\\folder\\myfile.doc\" *.pdf" );
  637. 			Console.ResetColor( );
  638.  
  639. 			Console.Error.WriteLine( "           will save to \"D:\\folder\\myfile.pdf\"" );
  640.  
  641. 			Console.Error.WriteLine( );
  642.  
  643. 			Console.ForegroundColor = ConsoleColor.White;
  644. 			Console.Error.WriteLine( "           WORD2ANY \"D:\\folder\\myfile.docx\" \"D:\\otherfolder\\*.rtf\"" );
  645. 			Console.ResetColor( );
  646.  
  647. 			Console.Error.WriteLine( "           will save to \"D:\\otherfolder\\myfile.rtf\"" );
  648.  
  649. 			Console.Error.WriteLine( );
  650.  
  651. 			Console.ForegroundColor = ConsoleColor.White;
  652. 			Console.Error.WriteLine( "           WORD2ANY \"D:\\folder\\myfile.rtf\" \"D:\\elsewhere\\page3.xps\" 3" );
  653. 			Console.ResetColor( );
  654.  
  655. 			Console.Error.WriteLine( "           will save page 3 of \"myfile.rtf\" to \"D:\\elsewhere\\page3.xps\"" );
  656.  
  657. 			Console.Error.WriteLine( );
  658.  
  659. 			Console.ForegroundColor = ConsoleColor.White;
  660. 			Console.Error.WriteLine( "           WORD2ANY \"D:\\folder\\name*.doc\" *.html 1-2" );
  661. 			Console.ResetColor( );
  662.  
  663. 			Console.Error.WriteLine( "           will save pages 1 and 2 of all matching files as HTML in \"D:\\folder\"" );
  664.  
  665. 			Console.Error.WriteLine( "           recognized extensions: htm, html, odt, pdf, rtf, txt, xml, xps" );
  666.  
  667. 			Console.Error.WriteLine( );
  668.  
  669. 			Console.ForegroundColor = ConsoleColor.White;
  670. 			Console.Error.WriteLine( "           WORD2ANY \"D:\\folder\\*.doc\" /T:8" );
  671. 			Console.ResetColor( );
  672.  
  673. 			Console.Error.WriteLine( "           will save all matching files as HTML in \"D:\\folder\"" );
  674.  
  675. 			Console.Error.WriteLine( );
  676.  
  677. 			Console.ForegroundColor = ConsoleColor.White;
  678. 			Console.Error.WriteLine( "           WORD2ANY /T" );
  679. 			Console.ResetColor( );
  680.  
  681. 			Console.Error.WriteLine( "           will list all available file types" );
  682.  
  683. 			Console.Error.WriteLine( );
  684.  
  685. 			Console.Error.WriteLine( "Credits:   Page range selection based on code by George Hua" );
  686.  
  687. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  688. 			Console.Error.WriteLine( "           http://social.msdn.microsoft.com/Forums/office/en-US/e48b3126-" );
  689.  
  690. 			Console.Error.WriteLine( "           941d-490a-85ee-e327bbe7e81b/convert-specific-word-pages-to-pdf-in-c" );
  691. 			Console.ResetColor( );
  692.  
  693. 			Console.Error.WriteLine( );
  694.  
  695. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  696.  
  697. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  698.  
  699. 			#endregion Help Text
  700.  
  701. 			return 1;
  702. 		}
  703.  
  704.  
  705. 		static int ValidateFilespec( string filespec )
  706. 		{
  707. 			int matchingfiles = -1;
  708. 			//filespec = Path.GetFullPath( filespec );
  709. 			try
  710. 			{
  711. 				string parentfolder = Directory.GetParent( filespec ).FullName;
  712. 				if ( Directory.Exists( parentfolder ) )
  713. 				{
  714. 					matchingfiles = 0;
  715. 					//foreach ( string matchingfile in Directory.GetFiles( Path.GetFileName( filespec ) ) )
  716. 					foreach ( string matchingfile in Directory.GetFiles( parentfolder, Path.GetFileName( filespec ) ) )
  717. 					{
  718. 						matchingfiles += 1;
  719. 					}
  720. 				}
  721. 			}
  722. 			catch { };
  723. 			return matchingfiles;
  724. 		}
  725.  
  726.  
  727. 		static bool WordConvert( string inputpath, string outputpath, Word.WdSaveFormat outputtype, int outputrangestart = 0, int outputrangeend = -1 )
  728. 		{
  729. 			try
  730. 			{
  731. 				Word.Application wordapp = new Word.Application( );
  732. 				wordapp.Visible = false;
  733. 				Word.Document worddoc = wordapp.Documents.Open( inputpath );
  734. 				if ( outputrangeend < outputrangestart )
  735. 				{
  736. 					// Save the entire original document with the new file name and file type
  737. 					worddoc.SaveAs( outputpath, outputtype );
  738. 				}
  739. 				else
  740. 				{
  741. 					// Based on code by George Hua
  742. 					// https://social.msdn.microsoft.com/Forums/office/en-US/e48b3126-941d-490a-85ee-e327bbe7e81b/convert-specific-word-pages-to-pdf-in-c
  743. 					// Select the specified page(s)
  744. 					object what = Word.WdGoToItem.wdGoToPage;
  745. 					object which = Word.WdGoToDirection.wdGoToFirst;
  746. 					Word.Range startRange = wordapp.Selection.GoTo( ref what, ref which, outputrangestart );
  747. 					Word.Range endRange = wordapp.Selection.GoTo( ref what, ref which, outputrangeend + 1 );
  748. 					endRange.SetRange( startRange.Start, endRange.End );
  749. 					endRange.Select( );
  750. 					// Copy and paste the selection into a new document
  751. 					wordapp.Selection.Copy( );
  752. 					wordapp.Documents.Add( );
  753. 					wordapp.Selection.Paste( );
  754. 					// Save the new document with the new file name and file type
  755. 					wordapp.ActiveDocument.SaveAs( outputpath, outputtype );
  756. 				}
  757. 				// Close the document(s) and application
  758. 				object savechanges = Word.WdSaveOptions.wdDoNotSaveChanges;
  759. 				worddoc.Close( ref savechanges );
  760. 				wordapp.Quit( ref savechanges );
  761. 				return true;
  762. 			}
  763. 			catch ( Exception )
  764. 			{
  765. 				return false;
  766. 			}
  767. 		}
  768. 	}
  769. }
  770.  

page last modified: 2024-04-16; loaded in 0.0178 seconds