javalibreofficeuno

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.

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?

Update: I am now looking for an anchor for my XShape, but XShape don't have a getAnchor() method.

But If I use XPropertySet prop=UnoRuntime.queryInterface(XPropertySet.class,shape);

I get the prop class.

And I call prop.getPropertyValue("AnchorType") which gives me an ancher type of 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

----- Update 2 ----- I managed to make it work. It turns out that my XShape also implements XTextContent (Thank you MRI), so all I had to do was:

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. So this println call will print the 50 chars following the XShape.


Solution

  • 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?

    Abridged comments

    Anchors pin objects to a specific location. Does the shape have a method getAnchor() or property AnchorType? I would use an introspection tool such as MRI to determine this. Download MRI 1.3.4 from https://github.com/hanya/MRI/releases.

    As far as a cursor, maybe it is similar to tables:

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

    Code solution given by 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());