如何创建Apache的POI XWPF文档项目符号列表? [英] How to create a bulleted list in Apache POI XWPF Document?

查看:3365
本文介绍了如何创建Apache的POI XWPF文档项目符号列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Java的一个DOCX Word文档中的项目符号/编号列表。我使用Apache POI库3.10。如果我理解正确,步骤是这样的:

I want to create a bulleted/numbered list in a docx word document with Java. I am using the Apache POI 3.10 library. If I understand correctly, the steps would be like this:


  1. 创建编号 =编号doc.createNumbering

  2. 添加AbstractNum的编号,并得到相应的abstractNumId

  3. 添加民与AbstractNumId numId = numbering.addNum(abstractNumId)

  4. 现在,我可以添加numId使用段落 para.setNumID(numId);

  1. Create Numbering numbering = doc.createNumbering
  2. add AbstractNum to the Numbering and get the corresponding abstractNumId
  3. Add a Num with the AbstractNumId numId = numbering.addNum(abstractNumId)
  4. Now I can add numId to the paragraphs using para.setNumID(numId);

不过,我停留在第二个步骤。如何创建一个AbstractNum对象,我可以添加到编号?

However I am stuck in the second step. How do I create an AbstractNum object that I can add to the numbering?

推荐答案

我试图做类似的事情,打我的头反对它,直到它开始工作。

I was trying to do something similar and hit my head up against it until it started working.

下面是我的方法来增加AbstractNum到文档的编号对象。调用'addAbstractNum()原来是在我使用(3.10-FINAL)这样做的版本一个空的错误。因此,要解决它,你将需要生成AbstractNum在XML,解析它,手动分配它的ID号,并将其添加到文档的编号对象。

Here was my approach to adding AbstractNum to the document's numbering object. Calling 'addAbstractNum()' turns out to have a null bug in the version I am using (3.10-FINAL) for doing this. So to get around it, you will need to generate your AbstractNum in XML, parse it, manually assign it an id, and add it to the document's numbering object.

这是我做的:

protected XWPFDocument doc;     
private BigInteger addListStyle(String style)
{
    try
    {
        XWPFNumbering numbering = doc.getNumbering();
        // generate numbering style from XML
        CTAbstractNum abstractNum = CTAbstractNum.Factory.parse(style);
        XWPFAbstractNum abs = new XWPFAbstractNum(abstractNum, numbering);

        // find available id in document
        BigInteger id = BigInteger.valueOf(0);
        boolean found = false;
        while (!found)
        {
            Object o = numbering.getAbstractNum(id);
            found = (o == null);
            if (!found) id = id.add(BigInteger.ONE);
        }
        // assign id
        abs.getAbstractNum().setAbstractNumId(id);
        // add to numbering, should get back same id
        id = numbering.addAbstractNum(abs);
        // add to num list, result is numid
        return doc.getNumbering().addNum(id);           
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return null;
    }
}

在风格字符串的格式,传递给方法,需要XWPF文件的知识 - 这,我没有。所以,我创建了,我想要一个编号样式Word文档。其保存到的.docx文件。解压的.docx文件,以及复制从numbering.xml'的XML片段。它看起来是这样的:

The format of the 'style' string, that is passed to the method, requires knowledge of XWPF documents -- of which, I have none. So I created a word document with a numbering style that I wanted. Saved it to a '.docx' file. Unzipped the '.docx' file, and copied the XML fragment from 'numbering.xml'. It will look something like this:

<xml-fragment 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: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 wp14"><w:nsid w:val="1656060D" /><w:multiLevelType w:val="hybridMultilevel" /><w:tmpl w:val="99FCFC1A" /><w:lvl w:ilvl="0" w:tplc="0409000F"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%1." /><w:lvlJc w:val="left" /><w:pPr><w:ind w:left="720" w:hanging="360" /></w:pPr></w:lvl><w:lvl w:ilvl="1" w:tplc="04090019" w:tentative="1"><w:start w:val="1" /><w:numFmt w:val="lowerLetter" /><w:lvlText w:val="%2." /><w:lvlJc w:val="left" /><w:pPr><w:ind w:left="1440" w:hanging="360" /></w:pPr></w:lvl><w:lvl w:ilvl="2" w:tplc="0409001B" w:tentative="1"><w:start w:val="1" /><w:numFmt w:val="lowerRoman" /><w:lvlText w:val="%3." /><w:lvlJc w:val="right" /><w:pPr><w:ind w:left="2160" w:hanging="180" /></w:pPr></w:lvl><w:lvl w:ilvl="3" w:tplc="0409000F" w:tentative="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:ind w:left="2880" w:hanging="360" /></w:pPr></w:lvl><w:lvl w:ilvl="4" w:tplc="04090019" w:tentative="1"><w:start w:val="1" /><w:numFmt w:val="lowerLetter" /><w:lvlText w:val="%5." /><w:lvlJc w:val="left" /><w:pPr><w:ind w:left="3600" w:hanging="360" /></w:pPr></w:lvl><w:lvl w:ilvl="5" w:tplc="0409001B" w:tentative="1"><w:start w:val="1" /><w:numFmt w:val="lowerRoman" /><w:lvlText w:val="%6." /><w:lvlJc w:val="right" /><w:pPr><w:ind w:left="4320" w:hanging="180" /></w:pPr></w:lvl><w:lvl w:ilvl="6" w:tplc="0409000F" w:tentative="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%7." /><w:lvlJc w:val="left" /><w:pPr><w:ind w:left="5040" w:hanging="360" /></w:pPr></w:lvl><w:lvl w:ilvl="7" w:tplc="04090019" w:tentative="1"><w:start w:val="1" /><w:numFmt w:val="lowerLetter" /><w:lvlText w:val="%8." /><w:lvlJc w:val="left" /><w:pPr><w:ind w:left="5760" w:hanging="360" /></w:pPr></w:lvl><w:lvl w:ilvl="8" w:tplc="0409001B" w:tentative="1"><w:start w:val="1" /><w:numFmt w:val="lowerRoman" /><w:lvlText w:val="%9." /><w:lvlJc w:val="right" /><w:pPr><w:ind w:left="6480" w:hanging="180" /></w:pPr></w:lvl></xml-fragment>

取该字符串,将它传递给上述方法。现在你有一个numID,你可以罗列出的。

Take that string, pass it to the method above. Now you have a numID that you can make lists out of.

XWPFParagraph para = doc.createParagraph();
para.setStyle("ListParagraph");
para.setNumID(listType);
para.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(level));

好运。

这篇关于如何创建Apache的POI XWPF文档项目符号列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆