using System; using System.IO; using unoidl.com.sun.star.beans; using unoidl.com.sun.star.frame; using unoidl.com.sun.star.lang; using unoidl.com.sun.star.uno; namespace RobvanderWoude { class ODT2PDF { static int Main( string[] args ) { try { if ( args.Length == 0 ) { return WriteError( null ); } if ( args.Length == 1 || args.Length == 2 ) { if ( args[0] == "/?" ) { return WriteError( null ); } string fileIn = Path.GetFullPath( args[0] ); if ( !File.Exists( fileIn ) ) { return WriteError( "Input file does not exist" ); } string fileOut; if ( args.Length == 2 ) { fileOut = Path.GetFullPath( args[1] ); } else { string parent = Directory.GetParent( fileIn ).FullName; if ( parent.EndsWith( "\\" ) ) { fileOut = parent + Path.GetFileNameWithoutExtension( fileIn ) + ".pdf"; } else { fileOut = parent + "\\" + Path.GetFileNameWithoutExtension( fileIn ) + ".pdf"; } } if ( !Directory.GetParent( fileOut ).Exists ) { return WriteError( "Parent folder of output file does not exist" ); } string extIn = Path.GetExtension( fileIn ).ToLower( ); if ( extIn == ".odt" ) { if ( SaveOdtAsPdf( fileIn, fileOut ) ) { return 0; } else { return WriteError( "Could not convert and save the file" ); } } else { return WriteError( "Invalid input file type/extension" ); } } else { return WriteError( null ); } } catch ( System.Exception e ) { return WriteError( e.Message ); } } static bool SaveOdtAsPdf( string fileIn, string fileOut ) { try { // The main functionality uses OpenOffice's UNO components // http://en.wikipedia.org/wiki/Universal_Network_Objects string urlIn = "file:///" + Path.GetFullPath( fileIn ).Replace( "\\", "/" ); string urlOut = "file:///" + Path.GetFullPath( fileOut ).Replace( "\\", "/" ); XComponentContext unoBootstrap = uno.util.Bootstrap.bootstrap( ); XMultiServiceFactory unoServiceMan = (XMultiServiceFactory)unoBootstrap.getServiceManager( ); XComponentLoader unoDesk = (XComponentLoader)unoServiceMan.createInstance( "com.sun.star.frame.Desktop" ); PropertyValue[] inputProperties = new PropertyValue[1]; inputProperties[0] = new PropertyValue( ); inputProperties[0].Name = "Hidden"; inputProperties[0].Value = new uno.Any( true ); XComponent unoDoc = unoDesk.loadComponentFromURL( urlIn, "_blank", 0, inputProperties ); PropertyValue[] outputProperties = new PropertyValue[1]; outputProperties[0] = new PropertyValue( ); outputProperties[0].Name = "FilterName"; outputProperties[0].Value = new uno.Any( "writer_pdf_Export" ); ( (XStorable)unoDoc ).storeToURL( urlOut, outputProperties ); ( (XComponent)unoDoc ).dispose( ); unoDoc = null; return true; } catch ( unoidl.com.sun.star.uno.Exception ) { return false; } } #region Error Handling public static int WriteError( string errorMessage ) { if ( string.IsNullOrEmpty( errorMessage ) == false ) { Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errorMessage ); Console.ResetColor( ); } /* ODT2PDF, Version 1.01 Save LibreOffice/OpenOffice Writer files as PDF Usage: ODT2PDF inputfile.odt [ outputfile.pdf ] Where: inputfile.odt is the LibreOffice file to be saved as PDF outputfile.pdf is the path of the output PDF file (default: name and parent folder of inputfile) Notes: Requires LibreOffice, its SDK and a Java runtime. Tested with LibreOffice 4.1.4.2 and Java 1.7.0_51-b13. Should also work with OpenOffice. Written by Rob van der Woude http://www.robvanderwoude.com */ Console.Error.WriteLine( ); Console.Error.WriteLine( "ODT2PDF, Version 1.01" ); Console.Error.WriteLine( "Save LibreOffice/OpenOffice Writer files as PDF" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "ODT2PDF inputfile.odt [ outputfile.pdf ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "inputfile.odt" ); Console.ResetColor( ); Console.Error.WriteLine( " is the LibreOffice file to be saved as PDF" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " outputfile.pdf" ); Console.ResetColor( ); Console.Error.WriteLine( " is the path of the output PDF file" ); Console.Error.WriteLine( " (default: name and parent folder of inputfile)" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Notes: Requires LibreOffice, its SDK and a Java runtime." ); Console.Error.WriteLine( " Tested with LibreOffice 4.1.4.2 and Java 1.7.0_51-b13." ); Console.Error.WriteLine( " Should also work with OpenOffice." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "http://www.robvanderwoude.com" ); Console.OpenStandardOutput( ); return 1; } #endregion Error Handling } }