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.IO;
  3. using unoidl.com.sun.star.beans;
  4. using unoidl.com.sun.star.frame;
  5. using unoidl.com.sun.star.lang;
  6. using unoidl.com.sun.star.uno;
  7.  
  8.  
  9. namespace RobvanderWoude
  10. {
  11. 	class ODT2PDF
  12. 	{
  13. 		static int Main( string[] args )
  14. 		{
  15. 			try
  16. 			{
  17. 				if ( args.Length == 0 )
  18. 				{
  19. 					return WriteError( null );
  20. 				}
  21. 				if ( args.Length == 1 || args.Length == 2 )
  22. 				{
  23. 					if ( args[0] == "/?" )
  24. 					{
  25. 						return WriteError( null );
  26. 					}
  27. 					string fileIn = Path.GetFullPath( args[0] );
  28. 					if ( !File.Exists( fileIn ) )
  29. 					{
  30. 						return WriteError( "Input file does not exist" );
  31. 					}
  32. 					string fileOut;
  33. 					if ( args.Length == 2 )
  34. 					{
  35. 						fileOut = Path.GetFullPath( args[1] );
  36. 					}
  37. 					else
  38. 					{
  39. 						string parent = Directory.GetParent( fileIn ).FullName;
  40. 						if ( parent.EndsWith( "\\" ) )
  41. 						{
  42. 							fileOut = parent + Path.GetFileNameWithoutExtension( fileIn ) + ".pdf";
  43. 						}
  44. 						else
  45. 						{
  46. 							fileOut = parent + "\\" + Path.GetFileNameWithoutExtension( fileIn ) + ".pdf";
  47. 						}
  48.  
  49. 					}
  50. 					if ( !Directory.GetParent( fileOut ).Exists )
  51. 					{
  52. 						return WriteError( "Parent folder of output file does not exist" );
  53. 					}
  54. 					string extIn = Path.GetExtension( fileIn ).ToLower( );
  55. 					if ( extIn == ".odt" )
  56. 					{
  57. 						if ( SaveOdtAsPdf( fileIn, fileOut ) )
  58. 						{
  59. 							return 0;
  60. 						}
  61. 						else
  62. 						{
  63. 							return WriteError( "Could not convert and save the file" );
  64. 						}
  65. 					}
  66. 					else
  67. 					{
  68. 						return WriteError( "Invalid input file type/extension" );
  69. 					}
  70. 				}
  71. 				else
  72. 				{
  73. 					return WriteError( null );
  74. 				}
  75. 			}
  76. 			catch ( System.Exception e )
  77. 			{
  78. 				return WriteError( e.Message );
  79. 			}
  80. 		}
  81.  
  82. 		static bool SaveOdtAsPdf( string fileIn, string fileOut )
  83. 		{
  84. 			try
  85. 			{
  86. 				// The main functionality uses OpenOffice's UNO components
  87. 				// http://en.wikipedia.org/wiki/Universal_Network_Objects
  88. 				string urlIn = "file:///" + Path.GetFullPath( fileIn ).Replace( "\\", "/" );
  89. 				string urlOut = "file:///" + Path.GetFullPath( fileOut ).Replace( "\\", "/" );
  90. 				XComponentContext unoBootstrap = uno.util.Bootstrap.bootstrap( );
  91. 				XMultiServiceFactory unoServiceMan = (XMultiServiceFactory)unoBootstrap.getServiceManager( );
  92. 				XComponentLoader unoDesk = (XComponentLoader)unoServiceMan.createInstance( "com.sun.star.frame.Desktop" );
  93. 				PropertyValue[] inputProperties = new PropertyValue[1];
  94. 				inputProperties[0] = new PropertyValue( );
  95. 				inputProperties[0].Name = "Hidden";
  96. 				inputProperties[0].Value = new uno.Any( true );
  97. 				XComponent unoDoc = unoDesk.loadComponentFromURL( urlIn, "_blank", 0, inputProperties );
  98. 				PropertyValue[] outputProperties = new PropertyValue[1];
  99. 				outputProperties[0] = new PropertyValue( );
  100. 				outputProperties[0].Name = "FilterName";
  101. 				outputProperties[0].Value = new uno.Any( "writer_pdf_Export" );
  102. 				( (XStorable)unoDoc ).storeToURL( urlOut, outputProperties );
  103. 				( (XComponent)unoDoc ).dispose( );
  104. 				unoDoc = null;
  105. 				return true;
  106. 			}
  107. 			catch ( unoidl.com.sun.star.uno.Exception )
  108. 			{
  109. 				return false;
  110. 			}
  111. 		}
  112.  
  113. 		#region Error Handling
  114.  
  115. 		public static int WriteError( string errorMessage )
  116. 		{
  117. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  118. 			{
  119. 				Console.Error.WriteLine( );
  120. 				Console.ForegroundColor = ConsoleColor.Red;
  121. 				Console.Error.Write( "ERROR: " );
  122. 				Console.ForegroundColor = ConsoleColor.White;
  123. 				Console.Error.WriteLine( errorMessage );
  124. 				Console.ResetColor( );
  125. 			}
  126.  
  127. 			/*
  128. 			ODT2PDF,  Version 1.01
  129. 			Save LibreOffice/OpenOffice Writer files as PDF
  130.  
  131. 			Usage:    ODT2PDF  inputfile.odt  [ outputfile.pdf ]
  132.  
  133. 			Where:    inputfile.odt    is the LibreOffice file to be saved as PDF
  134. 			          outputfile.pdf   is the path of the output PDF file
  135. 			                           (default: name and parent folder of inputfile)
  136.  
  137. 			Notes:    Requires LibreOffice, its SDK and a Java runtime.
  138. 			          Tested with LibreOffice 4.1.4.2 and Java 1.7.0_51-b13.
  139. 			          Should also work with OpenOffice.
  140.  
  141. 			Written by Rob van der Woude
  142. 			http://www.robvanderwoude.com
  143. 			 */
  144.  
  145. 			Console.Error.WriteLine( );
  146. 			Console.Error.WriteLine( "ODT2PDF,  Version 1.01" );
  147. 			Console.Error.WriteLine( "Save LibreOffice/OpenOffice Writer files as PDF" );
  148. 			Console.Error.WriteLine( );
  149. 			Console.Error.Write( "Usage:    " );
  150. 			Console.ForegroundColor = ConsoleColor.White;
  151. 			Console.Error.WriteLine( "ODT2PDF  inputfile.odt  [ outputfile.pdf ]" );
  152. 			Console.ResetColor( );
  153. 			Console.Error.WriteLine( );
  154. 			Console.Error.Write( "Where:    " );
  155. 			Console.ForegroundColor = ConsoleColor.White;
  156. 			Console.Error.Write( "inputfile.odt" );
  157. 			Console.ResetColor( );
  158. 			Console.Error.WriteLine( "    is the LibreOffice file to be saved as PDF" );
  159. 			Console.ForegroundColor = ConsoleColor.White;
  160. 			Console.Error.Write( "          outputfile.pdf" );
  161. 			Console.ResetColor( );
  162. 			Console.Error.WriteLine( "   is the path of the output PDF file" );
  163. 			Console.Error.WriteLine( "                           (default: name and parent folder of inputfile)" );
  164. 			Console.Error.WriteLine( );
  165. 			Console.Error.WriteLine( "Notes:    Requires LibreOffice, its SDK and a Java runtime." );
  166. 			Console.Error.WriteLine( "          Tested with LibreOffice 4.1.4.2 and Java 1.7.0_51-b13." );
  167. 			Console.Error.WriteLine( "          Should also work with OpenOffice." );
  168. 			Console.Error.WriteLine( );
  169. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  170. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  171. 			Console.OpenStandardOutput( );
  172. 			return 1;
  173. 		}
  174.  
  175. 		#endregion Error Handling
  176. 	}
  177. }
  178.  

page last modified: 2024-02-26; loaded in 0.0240 seconds