2016-03-09 158 views
-3

我想使用Java創建一個Word文檔,並且希望爲文檔添加一個項目符號列表。子彈必須是圓形或複選標記,而不是數字。我能夠使用XWPF創建數字的子彈列表,但不能創建圓形或複選標記項目符號。請分享一些示例,展示如何使用Java在word中創建圓形/複選標記類型的項目符號。Word文檔中的Apache POI圓形項目符號列表

回答

1

這將是如此難以解釋,但這裏有一個功能:

public static BigInteger addListStyle(XWPFDocument doc, char symbol) throws XmlException{ 

     String styleMaybe = "<w:numbering xmlns:wpc=\"http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\" xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\" xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\" xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" mc:Ignorable=\"w14 w15 wp14\">\n" + 
             "<w:abstractNum w:abstractNumId=\""+listStyleIDCounter+"\">\n" + 
               "<w:nsid w:val=\"6871722E\"/>\n" + 
               "<w:multiLevelType w:val=\"hybridMultilevel\"/>\n" + 
               "<w:tmpl w:val=\"8FE6E4C8\"/>\n" + 
               "<w:lvl w:ilvl=\"0\" w:tplc=\"0410000D\">\n" + 
                 "<w:start w:val=\"1\"/>\n" + 
                 "<w:numFmt w:val=\"bullet\"/>\n" + 
                 "<w:lvlText w:val=\""+symbol+"\"/>\n" + 
                 "<w:lvlJc w:val=\"left\"/>\n" + 
                 "<w:pPr>\n" + 
                   "<w:ind w:left=\"720\" w:hanging=\"360\"/>\n" + 
                 "</w:pPr>\n" + 
                   "<w:rPr>\n" + 
                   "<w:rFonts w:ascii=\"Webdings\" w:hAnsi=\"Webdings\" w:hint=\"default\"/>\n" + 
                 "</w:rPr>\n" + 
               "</w:lvl>\n" + 
             "</w:abstractNum>\n" + 
             "<w:num w:numId=\"1\">\n" + 
             "<w:abstractNumId w:val=\"0\"/>\n" + 
             "</w:num>\n" + 
           "</w:numbering>"; 



    XWPFNumbering numbering = doc.createNumbering(); 

    // genero il numbering style dall'XML 
    CTAbstractNum abstractNum = CTAbstractNum.Factory.parse(styleMaybe); 
    XWPFAbstractNum abs = new XWPFAbstractNum(abstractNum, numbering); 
    // gli imposto un ID univoco 
    BigInteger id = BigInteger.valueOf(listStyleIDCounter++); 

    // assegno l'id all'abs 
    abs.getAbstractNum().setAbstractNumId(id); 

    // ora aggiungo l'abs al CT del numbering, che mi dovrebbe ritornare lo stesso id 
    id = numbering.addAbstractNum(abs); 
    // ora lo aggiungo al numbering creato prima che mi restituirà ancora lo stesso id 
    return doc.getNumbering().addNum(id); 
} 

其中listStyleIDCounter是一個靜態的計數器從0

開始,你可以通過a進行檢查符號,=一個小圓和<爲小方塊,還有其他更多符號可以自己試試:D

+0

如何使用addListStyle方法? – hinotf

+0

調用它傳遞doc和一個符號,然後將返回的值指定爲元素的liststyleid –

相關問題