(view source code of odt2pdf.cs as plain text)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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 readonly string progver = "1.02";
static int Main( string[] args )
{string fileIn, fileOut;
#region Command Line Argumentsif ( args.Length == 0 || args.Length > 2 || args.Contains( "/?" ) )
{return ShowHelp( );
}fileIn = Path.GetFullPath( args[0] );
if ( !Path.GetExtension( fileIn ).Equals( ".odt", StringComparison.OrdinalIgnoreCase ) )
{return ShowHelp( "Invalid input file type/extension" );
}if ( !File.Exists( fileIn ) )
{return ShowHelp( "Input file does not exist" );
}if ( !Directory.GetParent( fileIn ).Exists )
{return ShowHelp( "Parent folder of input file does not exist" );
}if ( args.Length == 2 )
{fileOut = Path.GetFullPath( args[1] );
if ( !Directory.GetParent( fileOut ).Exists )
{return ShowHelp( "Parent folder of output file does not exist" );
} } else {fileOut = Path.Combine( Path.GetDirectoryName( fileIn ), Path.GetFileNameWithoutExtension( fileIn ) + ".pdf" );
} #endregion Command Line Arguments #region Requirementsif ( typeof( XComponentContext ) == null )
{return ShowHelp( "OpenOffice/LibreOffice SDK was not found." );
} #endregion Requirementsbool success = SaveOdtAsPdf( fileIn, fileOut );
if ( !success )
{return ShowHelp( "Could not convert and save the file" );
}return 0;
}static bool SaveOdtAsPdf( string fileIn, string fileOut )
{ try { // The main functionality uses OpenOffice's UNO components // http://en.wikipedia.org/wiki/Universal_Network_Objectsstring 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 Handlingpublic static int ShowHelp( params string[] errmsg )
{ #region Error Messageif ( errmsg.Length > 0 )
{List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
} #endregion Error Message #region Help Text /* ODT2PDF, Version 1.02 Save LibreOffice/OpenOffice Writer files as PDF Usage: ODT2PDF inputfile.odt [ outputfile.pdf ] Where: inputfile.odt is the LibreOffice/OpenOffice 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 or OpenOffice, its SDK and a Java runtime. Tested with LibreOffice 24.8.0.3 and Java 22.0.1+8-16. Return code ("Errorlevel") 1 in case of errors, otherwise 0. Written by Rob van der Woude https://www.robvanderwoude.com */ #endregion Help Text #region Display Help TextConsole.Error.WriteLine( );
Console.Error.WriteLine( "ODT2PDF, Version {0}", progver );
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/OpenOffice 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 or OpenOffice, its SDK and a Java runtime." );
Console.Error.WriteLine( " Tested with LibreOffice 24.8.0.3 and Java 22.0.1+8-16." );
Console.Error.WriteLine( " Return code (\"Errorlevel\") 1 in case of errors, otherwise 0." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help Textreturn 1;
} #endregion Error Handling }}page last modified: 2025-10-11; loaded in 0.0069 seconds