Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for fixwordunreadablecontent.cs

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

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

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