我有一个问题,我只是尝试添加新文本,然后应用LibreOffice样式 . 我想添加文本并使其遵循特定的样式(“ Headers 1”,“ Headers 2”等) .
向文档添加文本确实有效,更改样式确实有效,但最后一个样式集应用于整个文档而不仅仅是最后一个String . 我需要一些方法来限制对String的选择 . 我想我需要一个XTextRange而且Style属于那个而不是光标...但是不知道如何创建只包含我最新的String的新XTextRanges ......显然不确定,建议最受欢迎 .
EDIT :虽然下面的代码是用Java编写的,但我更愿意接受使用任何编程语言的解决方案,因此UNO API足够类似于我可以从另一种语言转换解决方案 . 我觉得OOo / LO的VB宏编写器比Java开发人员多,然后可能C或Python开发人员也有解决方案 . 我应该考虑写出一个改变样式的文件将是一个非常基本的要求!
com.sun.star.text.XText xText = this.xDoc.getText();
//create a cursor object
com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor();
this.writeResume(xText, xTCursor);
写简历的方法...你会看到我尝试用changeStyle方法改变样式的地方
private void writeResume(com.sun.star.text.XText xText, com.sun.star.text.XTextCursor xTCursor) {
TestData resume = new TestData();
List<Company> companies = resume.getCompanies();
this.changeStyle(xTCursor, "Heading 1");
xText.insertString(xTCursor, "Professional Experience\n", false);
xTCursor.collapseToEnd();
this.changeStyle(xTCursor, "Heading 2");
Company company = companies.get(0);
String date = dformat.format(company.getStartDate().getTime()) + " - " + dformat.format(company.getEndDate().getTime());
xText.insertString(xTCursor, company.getName() + "," + company.getLocation() + "\t" + date + "\n", false);
xTCursor.collapseToEnd();
this.changeStyle(xTCursor, "Heading 3");
xText.insertString(xTCursor, "Test Point 1\n", false);
xText.insertString(xTCursor, "Test Point 2\n", false);
xText.insertString(xTCursor, "Test Point 3\n", false);
}
changeStyle方法
public void changeStyle(com.sun.star.text.XTextCursor xTCursor, String styleName) {
XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xTCursor);
try {
xCursorProps.setPropertyValue("ParaStyleName", styleName);
} catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
Logger.getLogger(ResumeWriter.class.getName()).log(Level.SEVERE, null, ex);
}
}
1 回答
通过反复试验解决了这个问题:
要点:
需要使用"\r"而不是"\n"
需要比预期更频繁地调用我的set样式方法
在对此页面进行了另一次审核后发现了以上内容:https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Editing_Text
只更改了以下方法: