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.
NOTE 1: 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!
NOTE 2: This message has been posted on StackOverflow with users with accounts there I should mention this question is there with a 200 pt bounty. After answering here post your answer there so I can avoid having the points soon expire. (Here: http://stackoverflow.com/q/21898500/514065)
Code: Select all
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);Code: Select all
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);
}Code: Select all
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);
}
}