使用 apache poi 向 word 文档添加项目符号时出错

getting error when adding bullet to word document with apache poi

我正在使用 apache poi 向我的 word 文档添加项目符号。当我想通过 XDocReport 库将我的 word 文档转换为 pdf 文件时,我在 listContext 错误上收到此 nullpointerexception。

    11:46:28,364 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (default task-5) fr.opensagres.xdocreport.converter.XDocConverterException: fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
        at fr.opensagres.xdocreport.converter.docx.poi.itext.XWPF2PDFViaITextConverter.convert(XWPF2PDFViaITextConverter.java:72)
        at com.utc.pw.ui.TestWSV2500View.createTestWS(TestWSV2500View.java:6345)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181)
        at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
        at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
        
    Caused by: java.lang.NullPointerException
        at fr.opensagres.poi.xwpf.converter.core.ListContext.addItem(ListContext.java:48)
        at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitParagraph(XWPFDocumentVisitor.java:352)
        at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:231)
        at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTableCellBody(XWPFDocumentVisitor.java:1141)
        at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitCell(XWPFDocumentVisitor.java:1076)
        at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTableRow(XWPFDocumentVisitor.java:1024)
        at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTableBody(XWPFDocumentVisitor.java:918)
        at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTable(XWPFDocumentVisitor.java:900)
        at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:235)
        at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:183)
        at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:57)
        ... 83 more

我的子弹代码在这里

   //Bullet
        CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
        cTAbstractNum.setAbstractNumId(BigInteger.valueOf(0));
        CTLvl cTLvl = cTAbstractNum.addNewLvl();
        cTLvl.addNewNumFmt().setVal(STNumberFormat.BULLET);
        cTLvl.addNewLvlText().setVal("•");
        XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
        XWPFNumbering numbering = document.createNumbering();
        BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
        BigInteger numID = numbering.addNum(abstractNumID);
        ListContext list=new ListContext();
        list.createAndAddItem(cTLvl);
        
        XWPFParagraph paragraph_cell2_table17=cell1_table17.addParagraph();
        paragraph_cell2_table17.setSpacingBefore(10);
        paragraph_cell2_table17.setSpacingAfter(10);
        paragraph_cell2_table17.setIndentationLeft(500);
        paragraph_cell2_table17.setVerticalAlignment(TextAlignment.CENTER);
        paragraph_cell2_table17.setAlignment(ParagraphAlignment.LEFT);
        paragraph_cell2_table17.setNumID(numID);
        cell1_table17.setVerticalAlignment(XWPFVertAlign.CENTER);
        paragraph_cell2_table17.getCTP().getPPr().addNewRPr().addNewSz().setVal(BigInteger.valueOf(24));
        XWPFRun run_cell2_table17=paragraph_cell2_table17.createRun();
        run_cell2_table17.setText("If the Engine shows a stabilized (>3sec) Vibration Level above 1.0 ips: perform the necessary Troubleshootings to make sure it is an Engine related vibration. If Vibration Level is still above 1.0 ips after performed extensive Troubleshootings: DO NOT continue the Test.");
        run_cell2_table17.setFontFamily("Arial");
        run_cell2_table17.setFontSize(10);
        run_cell2_table17.setUnderline(UnderlinePatterns.SINGLE);
        

我是不是漏掉了什么?你们有什么想法吗?

您创建的项目符号列表只有一级缩进。这就是根本没有设置缩进级别的原因,Microsoft Word 就可以了。

但是 XDocReport 需要为每个编号级别设置缩进级别。因此,如果 XDocReport 有效,即使只有一个,也需要设置缩进级别。

所以:

    ...
    CTLvl cTLvl = cTAbstractNum.addNewLvl();
    cTLvl.setIlvl(BigInteger.valueOf(0)); // set indent level 0
    cTLvl.addNewNumFmt().setVal(STNumberFormat.BULLET);
    cTLvl.addNewLvlText().setVal("•");
    ...

顺便说一句:这里不需要 ListContextXDocReport 在解析文档时创建该上下文。所以应该删除代码 ListContext list=new ListContext(); list.createAndAddItem(cTLvl);