Aspose.word 如何自定义标题编号

使用aspose 生成word内容的时候,会根据对应的嵌套内容的标题生成word中对应的标题样式,但是如何自定义每级标题的编号样式?比如一级标题编号是: 一,二级标题编号是:1,三级标题编号是: a 如下示例
测试.docx (18.1 KB)

@wangyan 在这种情况下,您需要用列表修改样式:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Define list format for Heading 1
List list1 = doc.getLists().add(ListTemplate.NUMBER_DEFAULT);

// Define list format for Heading 2
List list2 = doc.getLists().add(ListTemplate.NUMBER_DEFAULT);
list2.getListLevels().get(0).setNumberFormat("一,");

List list3 = doc.getLists().add(ListTemplate.NUMBER_LOWERCASE_LETTER_DOT);

// Create styles for the document that include these list formats
Style style1 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_1);
style1.getListFormat().setList(list1);

Style style2 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_2);
style2.getListFormat().setList(list2);

Style style3 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_3);
style3.getListFormat().setList(list3);

// Apply styles to paragraphs
builder.getParagraphFormat().setStyle(style1);
builder.writeln("Chapter 1: Introduction");

builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
builder.writeln("This is an introductory text.");

builder.getParagraphFormat().setStyle(style2);
builder.writeln("Section I: Background");

builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
builder.writeln("Details about the background.");

builder.getParagraphFormat().setStyle(style3);
builder.writeln("Section II: Background");

builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
builder.writeln("Details about the background.");

doc.save("output.docx");

请检查: