我正在编写java代码来访问在Libre Office中打开的文档。
现在,我需要编写一些迭代整个文档的代码,希望按照编辑器中显示的顺序进行迭代。
我可以使用这段代码来遍历所有的普通文本:
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.
}
但问题是,这不包括任何图表对象。
然后我可以用
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.
}
这给了我所有的图表(和我猜的其他数字),但问题是:如何找出这些图表相对于模型中文本的位置。我如何得到一个光标,它代表每一个图表?
更新:我现在正在为我的XShape寻找一个锚,但是XShape没有一个getAnchor()方法。
但如果我使用prop=UnoRuntime.queryInterface(XPropertySet.class,形状的XPropertySet );
我拿到道具课了。
我称prop.getPropertyValue("AnchorType")为ancher类型的TextContentAnchorType.AS_CHARACTER
但我就是找不到锚本身。没有锚或斜纹属性。
顺便说一句:我试着为libre office安装"MRI“,但我找到的唯一版本是hav libreoffice 3.3作为支持的版本,它不会安装在7.1版上。
-更新2-我设法让它运转起来原来我的XShape也实现了XTextContent (谢谢核磁共振),所以我所要做的就是:
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());
这给了我一个指向段落的光标,然后我可以向前/向后移动,找出形状在哪里。因此,这个println调用将打印XShape后面的50个字符。
发布于 2021-10-09 23:01:31
如何找出这些图表相对于模型中文本的位置。我如何得到一个光标,它代表每一个图表?
摘要注释
锚定对象到特定位置。该形状是否有一个方法getAnchor()
或属性AnchorType
?我会使用像MRI这样的内省工具来确定这一点。从https://github.com/hanya/MRI/releases下载MRI 1.3.4。
就光标而言,它可能类似于表:
oText = oTable.getAnchor().getText()
oCurs = oText.createTextCursor()
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());
https://stackoverflow.com/questions/69500141
复制相似问题