简体   繁体   English

如何使用 UNO 遍历 OpenOffice/LibreOffice 中的整个文档

[英]How do I iterate over an entire document in OpenOffice/LibreOffice with UNO

I am writing java code to access a document open in Libre Office.我正在编写 java 代码来访问在 Libre Office 中打开的文档。

I now need to write some code which iterate over the entire document, hopefully in the same order it is shown in the editor.我现在需要编写一些代码来遍历整个文档,希望以与编辑器中显示的顺序相同的顺序。

I can use this code to iterate over all the normal text:我可以使用此代码遍历所有普通文本:

XComponent writerComponent=xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps);
XTextDocument mxDoc=UnoRuntime.queryInterface(XTextDocument.class, writerComponent);
XText mxDocText=mxDoc.getText();
XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, mxDocText);
XEnumeration xParaEnum = xParaAccess.createEnumeration();
Object element = xParaEnum.nextElement();
while (xParaEnum.hasMoreElements()) {
   XEnumerationAccess inlineAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, element);
   XEnumeration inline = inlineAccess.createEnumeration();
   // And I can then iterate over this inline element and get all the text and formatting.
}

But the problem is that this does not include any chart objects.但问题是这不包括任何图表对象。

I can then use然后我可以使用

XDrawPagesSupplier drawSupplier=UnoRuntime.queryInterface(XDrawPagesSupplier.class, writerComponent);
XDrawPages pages=drawSupplier.getDrawPages();
XDrawPage drawPage=UnoRuntime.queryInterface(XDrawPage.class,page);
            
for(int j=0;j!=drawPage.getCount();j++) {
   Object sub=drawPage.getByIndex(j);
   XShape subShape=UnoRuntime.queryInterface(XShape.class,sub);
   // Now I got my subShape, but how do I know its position, relative to the text.
}

And this gives me all charts (And other figures I guess), but the problem is: How do I find out where these charts are positioned in relation to the text in the model. And how do I get a cursor which represent each chart?这给了我所有图表(以及我猜的其他数字),但问题是:我如何找出这些图表相对于 model 中文本的位置。我如何获得代表每个图表的 cursor?

Update: I am now looking for an anchor for my XShape, but XShape don't have a getAnchor() method.更新:我现在正在为我的 XShape 寻找锚点,但 XShape 没有 getAnchor() 方法。

But If I use XPropertySet prop=UnoRuntime.queryInterface(XPropertySet.class,shape);但是如果我使用 XPropertySet prop=UnoRuntime.queryInterface(XPropertySet.class,shape);

I get the prop class.我得到道具 class。

And I call prop.getPropertyValue("AnchorType") which gives me an ancher type of TextContentAnchorType.AS_CHARACTER然后我调用 prop.getPropertyValue("AnchorType") 这给了我一个 TextContentAnchorType.AS_CHARACTER 类型

but I just can't get the anchor itself.但我就是无法获得锚点本身。 There are no anchor or textrange property.没有锚点或文本范围属性。

btw: I tried looking into installing "MRI" for libre office, but the only version I could find hav libreoffice 3.3 as supported version, and it would not install on version 7.1顺便说一句:我试着为 libre office 安装“MRI”,但我能找到的唯一版本是 libreoffice 3.3 作为受支持的版本,它不会安装在 7.1 版上

----- Update 2 ----- I managed to make it work. ----- 更新 2 ----- 我成功了。 It turns out that my XShape also implements XTextContent (Thank you MRI), so all I had to do was:事实证明,我的 XShape 也实现了 XTextContent(感谢 MRI),所以我所要做的就是:

XTextContent subContent=UnoRuntime.queryInterface(XTextContent.class,subShape);
XTextRange anchor=subContent.getAnchor();
XTextCursor cursor = anchor.getText().createTextCursorByRange(anchor.getStart());
cursor.goRight((short)50,true);
System.out.println("String=" + cursor.getString());

This gives me a cursor which point to the paragraph, which I can then move forward/backward to find out where the shape is.这给了我一个指向该段落的 cursor,然后我可以向前/向后移动以找出形状的位置。 So this println call will print the 50 chars following the XShape.所以这个 println 调用将打印 XShape 之后的 50 个字符。

How do I find out where these charts are positioned in relation to the text in the model. And how do I get a cursor which represent each chart?我如何找出这些图表相对于 model 中文本的位置。我如何获得代表每个图表的 cursor?

Abridged comments删节评论

Anchors pin objects to a specific location.锚将对象固定到特定位置。 Does the shape have a method getAnchor() or property AnchorType ?形状是否有方法getAnchor()或属性AnchorType I would use an introspection tool such as MRI to determine this.我会使用 MRI 等内省工具来确定这一点。 Download MRI 1.3.4 from https://github.com/hanya/MRI/releases .https://github.com/hanya/MRI/releases下载 MRI 1.3.4。

As far as a cursor, maybe it is similar to tables:至于 cursor,可能类似于表格:

oText = oTable.getAnchor().getText()
oCurs = oText.createTextCursor()

Code solution given by OP OP给出的代码解决方案

XTextContent subContent=UnoRuntime.queryInterface(XTextContent.class,subShape);
XTextRange anchor=subContent.getAnchor();
XTextCursor cursor = anchor.getText().createTextCursorByRange(anchor.getStart());
cursor.goRight((short)50,true);
System.out.println("String=" + cursor.getString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM