1

I am writing a program for using LibreOffice CLI in C#.

I wanna load LibreOffice Writer with some predefined saved file and then want to close the LibreOffice Writer.

I am able to load Writer with blank but didn't get how to open some specific file and after doing some work close the writer using the program.

Any help would be highly appreciated.

Here is the Code

using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using uno.util;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.util;

public static void Main()
{
    XComponentContext context = null;
    context = Bootstrap.bootstrap();
    if (context != null)
        Console.WriteLine("Connected");

    XTextDocument newDoc = openWriter(context);
}

private static XTextDocument openWriter(XComponentContext xContext)
{
    //define variables
    unoidl.com.sun.star.frame.XComponentLoader xCLoader;
    unoidl.com.sun.star.text.XTextDocument xDoc = null;
    unoidl.com.sun.star.lang.XComponent xComp = null;
    try
    {
        // get the remote office service manager
        unoidl.com.sun.star.lang.XMultiComponentFactory xMCF =
            xContext.getServiceManager();

        Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);

        xCLoader = ((XComponentLoader)oDesktop);
        //UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,oDesktop);
        unoidl.com.sun.star.beans.PropertyValue[] szEmptyArgs = new unoidl.com.sun.star.beans.PropertyValue[0];


        string strDoc = @"private:factory/swriter";

        xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
        xDoc = ((XTextDocument)xComp);

    }
    catch (System.Exception e) { }
    return xDoc;
}
2
  • add relevant code here Mar 25, 2015 at 0:04
  • @Piyush I have added the code too... Mar 25, 2015 at 16:49

2 Answers 2

0

Having a look at the OpenOffice documentation (https://www.openoffice.org/api/docs/common/ref/com/sun/star/frame/XComponentLoader.html) it seems to that the reason is on how you set the value of strDoc. If you want to open an already existing file, you have to set accordingly the value of strDoc. Honestly I don't know if an UNC specification is correct or not, but I'm going to check.

0

For Open, just use:

        string strDoc = @"file:///c:/Users/admin/file.doc"; // This is the file URI    
        xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
        xDoc = ((XTextDocument)xComp);

And for Close use something like this:

XCloseable xCloseable;
XModifiable xModifiable;
try
{
    xModifiable = (XModifiable)xComponent;
    xModifiable.setModified(false);
    xCloseable = (XCloseable)xComponent;
    xCloseable.close(true);

    // This closes all instances, even ones you didn't create
    // If you don't write this, you'll find 'soffice.bin' still lingering in taskmgr
    XDesktop xDesktop = (XDesktop)xCLoader;
    if(xDesktop != null)
        xDesktop.terminate();
}
catch(InvalidCastException)
{
    // Add handler
}
catch(CloseVetoException)
{
    // Add handler
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.