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