首页 > 解决方案 > 使用 apache poi 将项目符号添加到 word 文档时出现错误

问题描述

我正在使用 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);
        

我错过了什么吗?你们有什么想法吗?

标签: apache-poixdocreport

解决方案


您创建的项目符号列表只有一个缩进级别。这就是为什么根本没有设置缩进级别的原因,这样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("•");
    ...

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


推荐阅读