LibreOffice UNO:设置样式(可以用Java、VB、Python、C+;+;,任何使用UNO API的语言提供)

LibreOffice UNO:设置样式(可以用Java、VB、Python、C+;+;,任何使用UNO API的语言提供),java,vba,openoffice.org,libreoffice,uno,Java,Vba,Openoffice.org,Libreoffice,Uno,我有一个问题,我只是尝试添加新文本,然后应用LibreOffice样式。我想添加文本并使其遵循特定的样式(“标题1”、“标题2”等) 向文档中添加文本有效,更改样式有效。但是,最后一个样式集应用于整个文档,而不仅仅是最后一个字符串。我需要一些方法来限制对字符串的选择。我想我需要一个XTextRange,样式属于它,而不是光标。。。但不知道如何创建只包含我最新字符串的新XTextRanges。。。显然不确定,建议将是最受欢迎的 编辑:尽管下面的代码是用Java编写的,但我非常愿意接受使用任何编程语

我有一个问题,我只是尝试添加新文本,然后应用LibreOffice样式。我想添加文本并使其遵循特定的样式(“标题1”、“标题2”等)

向文档中添加文本有效,更改样式有效。但是,最后一个样式集应用于整个文档,而不仅仅是最后一个字符串。我需要一些方法来限制对字符串的选择。我想我需要一个XTextRange,样式属于它,而不是光标。。。但不知道如何创建只包含我最新字符串的新XTextRanges。。。显然不确定,建议将是最受欢迎的

编辑:尽管下面的代码是用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方法更改样式的位置

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);
    }
}

通过反复试验解决了问题:

要点:

  • 需要使用“\r”而不是“\n”
  • 需要比预期更频繁地调用我的集合样式方法
  • 在再次查看本页后,了解到上述情况:
仅更改了以下方法:

private void writeResume(com.sun.star.text.XText xText, com.sun.star.text.XTextCursor xTCursor) {
    TestData resume = new TestData();
    List<Company> companies = resume.getCompanies();
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    this.changeStyle(xTCursor, "Heading 1");//if this line is NOT here then will default to a custom style
    xText.insertString(xTCursor, "Professional Experience\n\r", false);
    xTCursor.collapseToEnd();
    //xText.insertControlCharacter(xText, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
    this.changeStyle(xTCursor, "Heading 2");//if this line is NOT here then will default to a custom style
    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 + "\r", false);
    xText.insertString(xTCursor,"Title\r", false);

    this.changeStyle(xTCursor, "Heading 3");//if this line is NOT here then will default to a custom style
    xText.insertString(xTCursor, "Test Point 1\r", false);
    this.changeStyle(xTCursor, "Heading 3");
    xText.insertString(xTCursor, "Test Point 2\r", false);
    this.changeStyle(xTCursor, "Heading 3");
    xText.insertString(xTCursor, "Test Point 3\r", false);
}
private void writeResume(com.sun.star.text.XText,com.sun.star.text.xTCursor){
TestData resume=新的TestData();
列出公司=resume.getcompanys();
//抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
this.changeStyle(xTCursor,“标题1”);//如果此行不在此处,则默认为自定义样式
xText.insertString(xTCursor,“专业经验\n\r”,false);
xTCursor.collapseToEnd();
//xText.insertControlCharacter(xText,com.sun.star.text.ControlCharacter.paragration\u BREAK,false);
this.changeStyle(xTCursor,“标题2”);//如果此行不在此处,则默认为自定义样式
公司=公司。获取(0);
字符串日期=dformat.format(company.getStartDate().getTime())+“-”+dformat.format(company.getEndDate().getTime());
xText.insertString(xTCursor,company.getName()+”,“+company.getLocation()+”\t“+date+”\r”,false);
插入字符串(xTCursor,“Title\r”,false);
this.changeStyle(xTCursor,“标题3”);//如果此行不在此处,则默认为自定义样式
插入字符串(xTCursor,“测试点1\r”,false);
本.变更样式(下称“标题3”);
插入字符串(xTCursor,“测试点2\r”,false);
本.变更样式(下称“标题3”);
插入字符串(xTCursor,“测试点3\r”,false);
}
Hm…我使用“\n”没有问题。
private void writeResume(com.sun.star.text.XText xText, com.sun.star.text.XTextCursor xTCursor) {
    TestData resume = new TestData();
    List<Company> companies = resume.getCompanies();
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    this.changeStyle(xTCursor, "Heading 1");//if this line is NOT here then will default to a custom style
    xText.insertString(xTCursor, "Professional Experience\n\r", false);
    xTCursor.collapseToEnd();
    //xText.insertControlCharacter(xText, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
    this.changeStyle(xTCursor, "Heading 2");//if this line is NOT here then will default to a custom style
    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 + "\r", false);
    xText.insertString(xTCursor,"Title\r", false);

    this.changeStyle(xTCursor, "Heading 3");//if this line is NOT here then will default to a custom style
    xText.insertString(xTCursor, "Test Point 1\r", false);
    this.changeStyle(xTCursor, "Heading 3");
    xText.insertString(xTCursor, "Test Point 2\r", false);
    this.changeStyle(xTCursor, "Heading 3");
    xText.insertString(xTCursor, "Test Point 3\r", false);
}