You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache POI 5.2.3自定义Word标题样式:加粗及办公软件样式列表显示

Apache POI 5.2.3 自定义Word标题样式问题解决

问题1:让应用样式的段落文本自动加粗

要实现样式级别的全局加粗,无需逐个设置Run,只需在样式的字符属性(CTRPr)中添加加粗配置即可。同时注意修复原代码中字号设置的错误(CTHpsMeasure的val为半点数,需用传入字号乘以2):

在CTRPr创建代码块中添加:

// 全局设置文本加粗
rpr.addNewB().setVal(STOnOff.TRUE);

并修正字号设置逻辑:

// 半点数 = 字号 × 2,确保字号与传入参数一致
int halfPointSize = pointSize * 2;
rpr.addNewSz().setVal(new BigInteger(String.valueOf(halfPointSize)));
rpr.addNewSzCs().setVal(new BigInteger(String.valueOf(halfPointSize)));

问题2:让自定义样式显示在Office/Google Docs的样式面板中

要让自定义样式被办公软件识别并显示在样式选项里,需调整几个关键属性,确保兼容性:

  1. 绑定默认标题基样式:让系统识别为标题类样式,提升Office和Google Docs的兼容性:
ctStyle.addNewBasedOn().setVal("Heading" + headingLevel);
  1. 配置样式可见性与优先级:添加排序优先级、快速样式标记,确保样式出现在面板中:
// 样式面板排序优先级(数值越小越靠前)
CTDecimalNumber sortPriority = CTDecimalNumber.Factory.newInstance();
sortPriority.setVal(BigInteger.valueOf(headingLevel));
ctStyle.setStylePaneSortPriority(sortPriority);

// 设置为快速样式,强制显示在样式面板
CTOnOff isQuickStyle = CTOnOff.Factory.newInstance();
isQuickStyle.setVal(STOnOff.TRUE);
ctStyle.setIsQuickStyle(isQuickStyle);
  1. 保留原代码中QFormat、UnhideWhenUsed的启用状态,确保样式可见。

修正后的完整addCustomHeadingStyle方法

private 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);

    // 绑定默认标题样式,提升兼容性
    ctStyle.addNewBasedOn().setVal("Heading" + headingLevel);

    // 样式在面板中的优先级(数值越小越突出)
    CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
    indentNumber.setVal(BigInteger.valueOf(headingLevel));
    ctStyle.setUiPriority(indentNumber);

    // 样式面板排序优先级
    CTDecimalNumber sortPriority = CTDecimalNumber.Factory.newInstance();
    sortPriority.setVal(BigInteger.valueOf(headingLevel));
    ctStyle.setStylePaneSortPriority(sortPriority);

    // 设置样式可见性
    CTOnOff onOff = CTOnOff.Factory.newInstance();
    ctStyle.setUnhideWhenUsed(onOff);
    ctStyle.setQFormat(onOff);

    // 设置为快速样式,显示在样式面板
    CTOnOff isQuickStyle = CTOnOff.Factory.newInstance();
    isQuickStyle.setVal(STOnOff.TRUE);
    ctStyle.setIsQuickStyle(isQuickStyle);

    // 设置大纲级别,对应导航栏
    CTPPr ppr = CTPPr.Factory.newInstance();
    ppr.setOutlineLvl(indentNumber);
    ctStyle.setPPr(ppr);

    XWPFStyle style = new XWPFStyle(ctStyle);

    // 配置字符样式:加粗、字号、字体、颜色
    CTRPr rpr = CTRPr.Factory.newInstance();
    rpr.addNewB().setVal(STOnOff.TRUE);
    int halfPointSize = pointSize * 2;
    rpr.addNewSz().setVal(new BigInteger(String.valueOf(halfPointSize)));
    rpr.addNewSzCs().setVal(new BigInteger(String.valueOf(halfPointSize)));
    rpr.addNewRFonts().setAscii("Arial");
    
    CTColor color = CTColor.Factory.newInstance();
    color.setVal(hexToBytes(hexColor));
    rpr.setColor(color);

    style.getCTStyle().setRPr(rpr);
    style.setType(STStyleType.PARAGRAPH);
    styles.addStyle(style);
}

调用示例

保持原有调用逻辑即可,设置后的样式会自动加粗,且在Microsoft Office和Google Docs的样式面板中显示:

String heading1 = "My Heading 1";
String heading2 = "My Heading 2";
String heading3 = "My Heading 3";
String heading4 = "My Heading 4";
addCustomHeadingStyle(document, styles, heading1, 1, 36, "4288BC");
addCustomHeadingStyle(document, styles, heading2, 2, 28, "4288BC");
addCustomHeadingStyle(document, styles, heading3, 3, 24, "4288BC");
addCustomHeadingStyle(document, styles, heading4, 4, 20, "000000");

内容的提问来源于stack exchange,提问作者hido

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 00:07:00