private static void ConvertToPDF(string serverHost, int serverPort, Uri inputFile, Uri outputFile) { // FIX: Workaround for a known bug: XUnoUrlResolver forgets to call WSAStartup. We can use dummy Socket for that: using (var dummySocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)) { // First, initialize local service manager (IoC container of some kind): XComponentContext componentContext = Bootstrap.bootstrap(); XMultiComponentFactory localServiceManager = componentContext.getServiceManager(); XUnoUrlResolver urlResolver = (XUnoUrlResolver)localServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", componentContext); // Connect to LibreOffice server // URL format explained here: https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Starting_OpenOffice.org_in_Listening_Mode string connectionString = string.Format("uno:socket,host={0},port={1};urp;StarOffice.ComponentContext", serverHost, serverPort); object initialObject = urlResolver.resolve(connectionString); // Retrieve remote service manager: XComponentContext remoteContext = (XComponentContext)initialObject; XMultiComponentFactory remoteComponentFactory = remoteContext.getServiceManager(); // Request our own instance of LibreOffice Writer from the server: object oDesktop = remoteComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", remoteContext); XComponentLoader xCLoader = (XComponentLoader)oDesktop; var loaderArgs = new PropertyValue[1]; loaderArgs[0] = new PropertyValue(); loaderArgs[0].Name = "Hidden"; // No GUI loaderArgs[0].Value = new Any(true); string inputFileUrl = inputFile.AbsoluteUri; // WARNING: LibreOffice .NET API uses .NET Remoting and does NOT throw clean and actionable errors, // so, if server crashes or input file is locked or whatever, you will get nulls after casting. // For example, `(XStorable)xCLoader` cast may produce null. // This is counter-intuitive, I know; be more careful in production-grade code and check for nulls after every cast. XStorable xStorable = (XStorable)xCLoader.loadComponentFromURL(inputFileUrl, "_blank", 0, loaderArgs); var writerArgs = new PropertyValue[2]; writerArgs[0] = new PropertyValue(); writerArgs[0].Name = "Overwrite"; // Overwrite outputFile if it already exists writerArgs[0].Value = new Any(true); writerArgs[1] = new PropertyValue(); writerArgs[1].Name = "FilterName"; writerArgs[1].Value = new Any("writer_pdf_Export"); // Important! This filter is doing all PDF stuff. string outputFileUrl = outputFile.AbsoluteUri; xStorable.storeToURL(outputFileUrl, writerArgs); XCloseable xCloseable = (XCloseable)xStorable; if (xCloseable != null) { xCloseable.close(false); } } }
static void Main(string[] args) { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP); string sUnoIni = "file:///c:/OpenOffice.org%203/URE/bin/uno.ini"; XComponentContext //xLocalContext = uno.util.Bootstrap.defaultBootstrap_InitialComponentContext(sUnoIni, null); xLocalContext = uno.util.Bootstrap.defaultBootstrap_InitialComponentContext(); XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager(); XUnoUrlResolver xUrlResolver = (XUnoUrlResolver)xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", xLocalContext); XMultiServiceFactory xRemoteServiceManager = (XMultiServiceFactory)xUrlResolver.resolve("uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager"); XDesktop Desktop = (XDesktop)xRemoteServiceManager.createInstance("com.sun.star.frame.Desktop"); /* * XDesktop * remoteDesktop = (XDesktop)xRemoteServiceManager.createInstance("com.sun.star.frame.Desktop"); * * XComponentLoader * Desktop = (XComponentLoader)remoteDesktop; // (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop"); * * XComponent * Document = Desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, new unoidl.com.sun.star.beans.PropertyValue[0]); * * XText * Text = ((XTextDocument)Document).getText(); * * Text.setString("Hello I'm the first text!"); * * XTextCursor * Cursor = Text.createTextCursor(); * * Text.insertString(Cursor, "Line# 1\n", false); * Text.insertString(Cursor, "Line# 2", false); * * XTextTable * Table; * * if ((Table = (XTextTable)multiServiceFactory.createInstance("com.sun.star.text.TextTable")) != null) * { * Table.initialize(2, 2); * Text.insertTextContent(Text.getEnd(), Table, false); * } * * * unoidl.com.sun.star.beans.PropertyValue[] * Params = new unoidl.com.sun.star.beans.PropertyValue[2]; * * unoidl.com.sun.star.beans.PropertyValue * Param = new unoidl.com.sun.star.beans.PropertyValue(); * * Param.Name = "FilterName"; * Param.Value = new uno.Any("writer_pdf_Export"); * Params[0] = Param; * * Param = new unoidl.com.sun.star.beans.PropertyValue(); * Param.Name = "CompressionMode"; * Param.Value = new uno.Any("1"); * Params[1] = Param; * * string * CurrentDirectory = System.IO.Directory.GetCurrentDirectory(), * DocumentDestName; * * CurrentDirectory = CurrentDirectory.Substring(0, CurrentDirectory.LastIndexOf("bin", CurrentDirectory.Length - 1)); * * if (File.Exists(DocumentDestName = (CurrentDirectory + "test_out.pdf"))) * File.Delete(DocumentDestName); * * DocumentDestName = DocumentDestName.Replace(Path.DirectorySeparatorChar, '/').Replace("#", "%23"); * * ((XStorable)Document).storeToURL("file:///" + DocumentDestName, Params); */ }