LibreOffice UNO: Setting Styles (anwer can be provided in Java, VB, Python, C++, any language using UNO API)

StackOverflow https://stackoverflow.com/questions/21898500

Question

I have an issue where I'm simply trying to add new text and then apply a LibreOffice style to it. I want to add text and have it follow a particular style ("Heading 1", "Heading 2", etc).

Adding text to the document does work, changing the style does work however the last style set is applied to the entire document and not just the last String. I need some way to limit the selection to the String. I think I need an XTextRange and the Style belongs to that and not the cursor... but don't know how to create new XTextRanges containing only my latest String... clearly not certain, and advice would be most welcome.

EDIT: Although the following code is in Java I am more than willing at accept a solution using any programming language, the UNO APIs are similar enough that I could convert a solution from another language. I have the feeling there are more VB macro writers for OOo/LO than Java developers, then again maybe a C++ or Python developer has a solution. I should think to write out a document changing styles would be a pretty basic requirement!

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

method to write resume... you will see where I attempt to change the style with the changeStyle method

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

The changeStyle method

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);
    }
}
Was it helpful?

Solution

Solved the issue through trial and error:

Main points:

Only the following method was changed:

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top