Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for odt2pdf.cs

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

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