[英]OpenOffice API - OptimalWidth option for table(all columns)
I am working on Java API which interacts with OpenOffice(swriter) through UNO. 我正在研究通过UNO与OpenOffice(swriter)进行交互的Java API。 For TextTable, I am having hard time setting TableColumn's "OptimalWidth" property. 对于TextTable,我很难设置TableColumn的“ OptimalWidth”属性。
I have tried the following code and it seems that getColumns() method cannot take me to TableColumn's property and let you only insert and remove columns. 我已经尝试了以下代码,并且似乎getColumns()方法无法将我带到TableColumn的属性,而只允许您插入和删除列。
XTableColumns xColumns = xTextTable.getColumns();
XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
XIndexAccess.class, xColumns);
for (int i = 0; i < xIndexAccess.getCount(); i++) {
XPropertySet xColumnProps = (XPropertySet) UnoRuntime
.queryInterface(XPropertySet.class,
(Any) xIndexAccess.getByIndex(i));
if (xColumnProps != null) {
xColumn.setPropertyValue("OptimalWidth", new Boolean(true));
}
}
Can anyone help me out or give me any tips setting OptimalWidth property for a table? 任何人都可以帮我或给我一些技巧来设置表格的OptimalWidth属性吗? Thank you very much in advance! 提前非常感谢您!
It was quite an odyssey to find out, but I finally managed to create a function which does exactly that: 找出它是一段艰难的旅程,但我最终设法创建了一个功能完全符合此要求的函数:
private void optimizeTableColumnWidths(XTextTable table)
throws Exception
{
XTextViewCursorSupplier cursorSupplier = (XTextViewCursorSupplier)
UnoRuntime.queryInterface(XTextViewCursorSupplier.class,
document.getCurrentController());
XTextViewCursor viewCursor = cursorSupplier.getViewCursor();
String cellName = "A1";
XText cellText = (XText)UnoRuntime.queryInterface(
XText.class, table.getCellByName(cellName));
XTextCursor cursor = cellText.createTextCursor();
viewCursor.gotoRange(cursor, false);
viewCursor.gotoEnd(true);
viewCursor.gotoEnd(true);
XController controller = document.getCurrentController();
XFrame frame = controller.getFrame();
XDispatchProvider dispatchProvider = (XDispatchProvider)
UnoRuntime.queryInterface(XDispatchProvider.class, frame);
String unoAction = ".uno:SetOptimalColumnWidth";
String targetFrameName = "";
int searchFlags = 0;
PropertyValue[] properties = new PropertyValue[0];
dispatchHelper.executeDispatch(
dispatchProvider,
unoAction,
targetFrameName,
searchFlags,
properties);
}
It is important that the dispatchHelper comes from the OpenOffice context, and not from the current document. 重要的是dispatchHelper来自OpenOffice上下文,而不是当前文档。 I retrieved the dispatchHelper this way: 我以这种方式检索了dispatchHelper:
XComponentContext context = Bootstrap.bootstrap();
XMultiComponentFactory factory =
context.getServiceManager();
Object dispatchHelperObject = factory.createInstanceWithContext(
"com.sun.star.frame.DispatchHelper", ooContext);
this.dispatchHelper = (XDispatchHelper)UnoRuntime.queryInterface(
XDispatchHelper.class, dispatchHelperObject);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.