如何使用UNO获取打开的文档? [英] How to get the opened document using UNO?

查看:153
本文介绍了如何使用UNO获取打开的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个打开对话框的附加组件,我需要访问当前打开的文本文档,但我不知道如何获取它。



我在NetBeans中使用OpenOffice插件,我从一个Add-on项目开始。它创建了一个类,它给了我一个XComponentContext实例,但我不知道如何使用它来获取当前文档的OfficeDocument实例。



我一直在谷歌搜索一段时间以来,我找不到任何使用现有的已打开文档的示例。它们都是从一个新文档或首先加载的文档开始的,因此它们有一个URL。



我试了一下基于OpenOffice的wiki( https://wiki.openoffice.org/wiki/API/Samples/Java/Office / DocumentHandling )这就是我想出的:

  private OfficeDocument getDocument(){
if(this.officeDocument == null){
try {
//这导致错误
XMultiComponentFactory xMultiComponentFactory = this.xComponentContext.getServiceManager();

Object oDesktop = xMultiComponentFactory.createInstanceWithContext(com.sun.star.frame.Desktop,this.xComponentContext);
XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class,oDesktop);

String url =private:factory / swriter;
String targetFrameName =_ self;
int searchFlags = FrameSearchFlag.SELF;
PropertyValue [] propertyValues = new PropertyValue [1];
propertyValues [0] = new PropertyValue();
propertyValues [0] .Name =隐藏;
propertyValues [0] .Value = Boolean.TRUE;

XComponent xComponent = xComponentLoader.loadComponentFromURL(url,targetFrameName,searchFlags,propertyValues);

XModel xModel = UnoRuntime.queryInterface(XModel.class,xComponent);
this.officeDocument = new OfficeDocument(xModel);
} catch(com.sun.star.uno.Exception ex){
throw new RuntimeException(ex);
}
}
返回this.officeDocument;
}

但是有一些奇怪的事情发生了。只是在我的班级中使用这种方法,即使它从未在任何地方被调用,在添加附加组件时会导致错误。

 (com.sun.star.depoyment.DeploymentDescription){{Message =激活时出错:VaphAddOn.jar,Context =(com.sun.star.uno.XInterface)@ 6ce03e0},原因=(任何){(com.sun.star.registry.CannotRegisterImplementationException){{Message =,Context =(com.sun.star.uno.XInterface)@ 0}}}} 

此行似乎导致错误:

  XMultiComponentFactory xMultiComponentFactory = this.xComponentContext.getServiceManager(); 

我不知道如何先行。



<我在 OpenOffice论坛上发布了这个问题但我没有得到回应。我现在在这里试试运气。

解决方案

在代码中使用它来获取当前文档:

  import com.sun.star.frame.XDesktop; 
...
XDesktop xDesktop =(XDesktop)UnoRuntime.queryInterface(XDesktop.class,oDesktop);
XComponent xComponent = xDesktop.getCurrentComponent();

我打开 BookmarkInsertion示例,并添加了此代码以使用当前文档而不是加载新文档。



就错误而言,可能存在如何构建错误的问题。有几件事要检查:




  • Office SDK版本是否与Office版本匹配?检查版本号以及它是32位还是64位。

  • 确保显示4 .jar文件(juh.jar,jurt.jar,unoil.jar,ridl.jar)在NetBeans中的库下,因为它们需要与附加组件一起包含。



如果您对尝试获取构建正确,然后您可能会发现使用python更容易,因为它不需要编译。 python也不需要queryInterface()。


I'm writing an add-on that opens a dialog and I need to access the currently opened text document but I don't know how get it.

I'm using the OpenOffice plug-in in NetBeans and I started from an Add-on project. It created a class that gives me a XComponentContext instance but I don't know how to use it to get a OfficeDocument instance of the current document.

I've been googling for some time and I can't find any example that uses an existing, opened, document. They all start from a new document or a document that is loaded first so they have an URL for it.

I gave it a try based on the OpenOffice wiki (https://wiki.openoffice.org/wiki/API/Samples/Java/Office/DocumentHandling) and this is what I came up with:

private OfficeDocument getDocument() {
  if (this.officeDocument == null) {
    try {
        // this causes the error
        XMultiComponentFactory xMultiComponentFactory = this.xComponentContext.getServiceManager();

        Object oDesktop = xMultiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", this.xComponentContext);
        XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);

        String url = "private:factory/swriter";
        String targetFrameName = "_self";
        int searchFlags = FrameSearchFlag.SELF;
        PropertyValue[] propertyValues = new PropertyValue[1];
        propertyValues[0] = new PropertyValue();
        propertyValues[0].Name = "Hidden";
        propertyValues[0].Value = Boolean.TRUE;

        XComponent xComponent = xComponentLoader.loadComponentFromURL(url, targetFrameName, searchFlags, propertyValues);

        XModel xModel = UnoRuntime.queryInterface(XModel.class, xComponent);
        this.officeDocument = new OfficeDocument(xModel);
    } catch (com.sun.star.uno.Exception ex) {
        throw new RuntimeException(ex);
    }
  }
  return this.officeDocument;
}

But there is something strange going on. Just having this method in my class, even if it's never been called anywhere, causes an error when adding the add-on.

(com.sun.star.depoyment.DeploymentDescription){{ Message = "Error during activation of: VaphAddOn.jar", Context = (com.sun.star.uno.XInterface) @6ce03e0 }, Cause = (any) {(com.sun.star.registry.CannotRegisterImplementationException){{ Message = "", Context = (com.sun.star.uno.XInterface) @0 }}}}

It seems this line causes the error:

XMultiComponentFactory xMultiComponentFactory = this.xComponentContext.getServiceManager();

I have no idea how to preceed.

I posted this question on the OpenOffice forum but I haven't got a response there. I'm trying my luck here now.

解决方案

Use this in your code to get the current document:

import com.sun.star.frame.XDesktop;
...
XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, oDesktop);
XComponent xComponent = xDesktop.getCurrentComponent();

I opened the BookmarkInsertion sample in NetBeans and added this code to use the current document instead of loading a new document.

As far as the error, there may be a problem with how it is getting built. A couple of things to check:

  • Does the Office SDK version match the Office version? Check version number and whether it's 32- or 64-bit.
  • Make sure that 4 .jar files (juh.jar, jurt.jar, unoil.jar, ridl.jar) are shown under Libraries in NetBeans, because they need to be included along with the add-on.

If you get frustrated with trying to get the build set up correctly, then you might find it easier to use python, since it doesn't need to be compiled. Also python does not require queryInterface().

这篇关于如何使用UNO获取打开的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆