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

查看:95
本文介绍了如何在 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. 创建编号numbering = doc.createNumbering
  2. 将 AbstractNum 添加到 Numbering 中并获得对应的 abstractNumId
  3. 添加一个带有 AbstractNumId 的数字 numId = numbering.addNum(abstractNumId)
  4. 现在我可以使用 para.setNumID(numId) 将 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) 中有一个空错误.因此,要绕过它,您需要在 XML 中生成 AbstractNum,对其进行解析,手动为其分配一个 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天全站免登陆