打开libreofficewriter并使用C#CLI加载/关闭文档

打开libreofficewriter并使用C#CLI加载/关闭文档,c#,libreoffice,writer,openoffice-writer,C#,Libreoffice,Writer,Openoffice Writer,我正在编写一个在C#中使用libreofficecli的程序 我想用一些预定义的保存文件加载LibreOfficeWriter,然后关闭LibreOffice Writer 我能够加载空白的Writer,但不知道如何打开某些特定文件,在完成一些工作后,使用该程序关闭Writer 任何帮助都将不胜感激 这是密码 using unoidl.com.sun.star.lang; using unoidl.com.sun.star.uno; using unoidl.com.sun.star.bridg

我正在编写一个在C#中使用
libreofficecli
的程序

我想用一些预定义的保存文件加载
LibreOffice
Writer,然后关闭LibreOffice Writer

我能够加载空白的Writer,但不知道如何打开某些特定文件,在完成一些工作后,使用该程序关闭Writer

任何帮助都将不胜感激

这是密码

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;
}

看一下OpenOffice文档(),原因似乎在于如何设置strDoc的值。 如果要打开已存在的文件,则必须相应地设置strDoc的值。
老实说,我不知道UNC规范是否正确,但我要检查一下

对于
打开
,只需使用:

        string strDoc = @"file:///c:/Users/admin/file.doc"; // This is the file URI    
        xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
        xDoc = ((XTextDocument)xComp);
对于
Close
,请使用以下内容:

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
}

添加相关代码here@Piyush我也添加了代码。。。