How to create a bulleted list in Apache POI XWPF Document?

后端 未结 5 455
鱼传尺愫
鱼传尺愫 2021-01-14 09:15

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:

5条回答
  •  悲&欢浪女
    2021-01-14 09:38

    static void addCustomHeadingStyle(XWPFDocument docxDocument, XWPFStyles styles, String strStyleId, int headingLevel, int pointSize, String hexColor) {
            CTStyle ctStyle = CTStyle.Factory.newInstance();
            ctStyle.setStyleId(strStyleId);
    
            CTString styleName = CTString.Factory.newInstance();
            styleName.setVal(strStyleId);
            ctStyle.setName(styleName);
    
            CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
            indentNumber.setVal(BigInteger.valueOf(headingLevel));
    
            // lower number > style is more prominent in the formats bar
            ctStyle.setUiPriority(indentNumber);
    
            CTOnOff onoffnull = CTOnOff.Factory.newInstance();
            ctStyle.setUnhideWhenUsed(onoffnull);
    
            // style shows up in the formats bar
            ctStyle.setQFormat(onoffnull);
    
            // style defines a heading of the given level
            CTPPr ppr = CTPPr.Factory.newInstance();
            ppr.setOutlineLvl(indentNumber);
            ppr.addNewNumPr().addNewNumId().setVal(BigInteger.ONE);
            ctStyle.setPPr(ppr);
    
            XWPFStyle style = new XWPFStyle(ctStyle);
    
            CTHpsMeasure size = CTHpsMeasure.Factory.newInstance();
            size.setVal(new BigInteger(String.valueOf(pointSize)));
            CTHpsMeasure size2 = CTHpsMeasure.Factory.newInstance();
            size2.setVal(new BigInteger("24"));
    
            CTFonts fonts = CTFonts.Factory.newInstance();
            fonts.setAscii("Calibri Light");
    
            CTRPr rpr = CTRPr.Factory.newInstance();
            rpr.setRFonts(fonts);
            rpr.setSz(size);
            rpr.setSzCs(size2);
    
            style.setType(STStyleType.PARAGRAPH);
            styles.addStyle(style);
        }
    }
    

    it is possible to add custom styles, for creating titles and index, this answer is not mine just some changes to add the number

提交回复
热议问题