Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for odt2docx.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using unoidl.com.sun.star.beans;
  6. using unoidl.com.sun.star.frame;
  7. using unoidl.com.sun.star.lang;
  8. using unoidl.com.sun.star.uno;
  9.  
  10.  
  11. namespace RobvanderWoude
  12. {
  13. 	internal class ODT2Docx
  14. 	{
  15. 		static readonly string progver = "1.00";
  16.  
  17.  
  18. 		static int Main( string[] args )
  19. 		{
  20. 			string fileIn, fileOut;
  21.  
  22.  
  23. 			#region Command Line Arguments
  24.  
  25. 			if ( args.Length == 0 || args.Length > 2 || args.Contains( "/?" ) )
  26. 			{
  27. 				return ShowHelp( );
  28. 			}
  29.  
  30. 			fileIn = Path.GetFullPath( args[0] );
  31. 			if ( !Path.GetExtension( fileIn ).Equals( ".odt", StringComparison.OrdinalIgnoreCase ) )
  32. 			{
  33. 				return ShowHelp( "Invalid input file type/extension" );
  34. 			}
  35. 			if ( !File.Exists( fileIn ) )
  36. 			{
  37. 				return ShowHelp( "Input file does not exist" );
  38. 			}
  39. 			if ( !Directory.GetParent( fileIn ).Exists )
  40. 			{
  41. 				return ShowHelp( "Parent folder of input file does not exist" );
  42. 			}
  43.  
  44. 			if ( args.Length == 2 )
  45. 			{
  46. 				fileOut = Path.GetFullPath( args[1] );
  47. 				if ( !Path.GetExtension( fileIn ).Equals( ".docx", StringComparison.OrdinalIgnoreCase ) )
  48. 				{
  49. 					return ShowHelp( "Invalid output file type/extension" );
  50. 				}
  51. 				if ( !Directory.GetParent( fileOut ).Exists )
  52. 				{
  53. 					return ShowHelp( "Parent folder of output file does not exist" );
  54. 				}
  55. 			}
  56. 			else
  57. 			{
  58. 				fileOut = Path.Combine( Path.GetDirectoryName( fileIn ), Path.GetFileNameWithoutExtension( fileIn ) + ".docx" );
  59. 			}
  60.  
  61. 			#endregion Command Line Arguments
  62.  
  63.  
  64. 			#region Requirements
  65.  
  66. 			if ( typeof( XComponentContext ) == null )
  67. 			{
  68. 				return ShowHelp( "OpenOffice/LibreOffice SDK was not found." );
  69. 			}
  70.  
  71. 			#endregion Requirements
  72.  
  73.  
  74. 			bool success = SaveOdtAsDocx( fileIn, fileOut );
  75. 			if ( !success )
  76. 			{
  77. 				return ShowHelp( "Could not convert and save the file" );
  78. 			}
  79.  
  80. 			return 0;
  81. 		}
  82.  
  83.  
  84. 		static bool SaveOdtAsDocx( string fileIn, string fileOut )
  85. 		{
  86. 			//try
  87. 			{
  88. 				// The main functionality uses OpenOffice's UNO components
  89. 				// http://en.wikipedia.org/wiki/Universal_Network_Objects
  90. 				string urlIn = "file:///" + Path.GetFullPath( fileIn ).Replace( "\\", "/" );
  91. 				string urlOut = "file:///" + Path.GetFullPath( fileOut ).Replace( "\\", "/" );
  92. 				XComponentContext unoBootstrap = uno.util.Bootstrap.bootstrap( );
  93. 				XMultiServiceFactory unoServiceMan = (XMultiServiceFactory)unoBootstrap.getServiceManager( );
  94. 				XComponentLoader unoDesk = (XComponentLoader)unoServiceMan.createInstance( "com.sun.star.frame.Desktop" );
  95. 				PropertyValue[] inputProperties = new PropertyValue[1];
  96. 				inputProperties[0] = new PropertyValue( );
  97. 				inputProperties[0].Name = "Hidden";
  98. 				inputProperties[0].Value = new uno.Any( true );
  99. 				XComponent unoDoc = unoDesk.loadComponentFromURL( urlIn, "_blank", 0, inputProperties );
  100. 				PropertyValue[] outputProperties = new PropertyValue[1];
  101. 				outputProperties[0] = new PropertyValue( );
  102. 				outputProperties[0].Name = "FilterName";
  103. 				outputProperties[0].Value = new uno.Any( "MS Word 2007 XML" );
  104. 				( (XStorable)unoDoc ).storeToURL( urlOut, outputProperties );
  105. 				( (XComponent)unoDoc ).dispose( );
  106. 				unoDoc = null;
  107. 				return true;
  108. 			}/*
  109. 			catch ( unoidl.com.sun.star.uno.Exception )
  110. 			{
  111. 				return false;
  112. 			}*/
  113. 		}
  114.  
  115.  
  116. 		#region Error Handling
  117.  
  118. 		public static int ShowHelp( params string[] errmsg )
  119. 		{
  120. 			#region Error Message
  121.  
  122. 			if ( errmsg.Length > 0 )
  123. 			{
  124. 				List<string> errargs = new List<string>( errmsg );
  125. 				errargs.RemoveAt( 0 );
  126. 				Console.Error.WriteLine( );
  127. 				Console.ForegroundColor = ConsoleColor.Red;
  128. 				Console.Error.Write( "ERROR:\t" );
  129. 				Console.ForegroundColor = ConsoleColor.White;
  130. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  131. 				Console.ResetColor( );
  132. 			}
  133.  
  134. 			#endregion Error Message
  135.  
  136.  
  137. 			#region Help Text
  138.  
  139. 			/*
  140. 			ODT2Docx,  Version 1.00
  141. 			Save LibreOffice/OpenOffice Writer .odt files as Word .docx
  142.  
  143. 			Usage:    ODT2PDF  inputfile.odt  [ outputfile.docx ]
  144.  
  145. 			Where:    inputfile.odt    is the LibreOffice/OpenOffice file to be converted
  146. 			          outputfile.docx  is the path of the output .docx file
  147. 			                           (default: name and parent folder of inputfile)
  148.  
  149. 			Notes:    Requires LibreOffice or OpenOffice, its SDK and a Java runtime.
  150. 			          Tested with LibreOffice 24.8.0.3 and Java 22.0.1+8-16.
  151. 			          Return code ("Errorlevel") 1 in case of errors, otherwise 0.
  152.  
  153. 			Written by Rob van der Woude
  154. 			https://www.robvanderwoude.com
  155. 			*/
  156.  
  157. 			#endregion Help Text
  158.  
  159.  
  160. 			#region Display Help Text
  161.  
  162. 			Console.Error.WriteLine( );
  163.  
  164. 			Console.Error.WriteLine( "ODT2Docx,  Version {0}", progver );
  165.  
  166. 			Console.Error.WriteLine( "Save LibreOffice/OpenOffice Writer .odt files as Word .docx" );
  167.  
  168. 			Console.Error.WriteLine( );
  169.  
  170. 			Console.Error.Write( "Usage:    " );
  171. 			Console.ForegroundColor = ConsoleColor.White;
  172. 			Console.Error.WriteLine( "ODT2PDF  inputfile.odt  [ outputfile.docx ]" );
  173. 			Console.ResetColor( );
  174.  
  175. 			Console.Error.WriteLine( );
  176.  
  177. 			Console.Error.Write( "Where:    " );
  178. 			Console.ForegroundColor = ConsoleColor.White;
  179. 			Console.Error.Write( "inputfile.odt" );
  180. 			Console.ResetColor( );
  181. 			Console.Error.WriteLine( "    is the LibreOffice/OpenOffice file to be converted" );
  182.  
  183. 			Console.ForegroundColor = ConsoleColor.White;
  184. 			Console.Error.Write( "          outputfile.docx" );
  185. 			Console.ResetColor( );
  186. 			Console.Error.WriteLine( "  is the path of the output .docx file" );
  187.  
  188. 			Console.Error.WriteLine( "                           (default: name and parent folder of inputfile)" );
  189.  
  190. 			Console.Error.WriteLine( );
  191.  
  192. 			Console.Error.WriteLine( "Notes:    Requires LibreOffice or OpenOffice, its SDK and a Java runtime." );
  193.  
  194. 			Console.Error.WriteLine( "          Tested with LibreOffice 24.8.0.3 and Java 22.0.1+8-16." );
  195.  
  196. 			Console.Error.WriteLine( "          Return code (\"Errorlevel\") 1 in case of errors, otherwise 0." );
  197.  
  198. 			Console.Error.WriteLine( );
  199.  
  200. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  201.  
  202. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  203.  
  204. 			#endregion Display Help Text
  205.  
  206.  
  207. 			return 1;
  208. 		}
  209.  
  210. 		#endregion Error Handling
  211. 	}
  212. }

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