2014-02-20 52 views
2

我有一個問題,我只是試圖添加新文本,然後對其應用LibreOffice樣式。我想添加文本並使其遵循特定的樣式(「標題1」,「標題2」等)。LibreOffice UNO:設置樣式(anwer可以使用UNO API以Java,VB,Python,C++,任何語言提供)

向文檔添加文本確實有效,但更改樣式確實有效,但最後的樣式集應用於整個文檔而不僅僅是最後一個字符串。我需要一些方法來限制選擇到字符串。我想我需要一個XTextRange,而Style屬於那個而不是光標......但不知道如何創建只包含我最新字符串的新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方法

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

回答

0

解決問題通過反覆試驗:

要點:

只有以下方法改爲:

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

嗯...我使用 「\ n」 沒有問題。 –