删除FIELD后导致后面段落的样式发生变化

目 次.docx (30.5 KB)
请问删除目次的域代码后,前言段落的样式发生变化,段前分页设置也被删除了,是怎么回事

for (Field field : document.getRange().getFields()) {
    int type = field.getType();
    boolean isDeleteField = (type == FieldType.FIELD_TOC || type == FieldType.FIELD_TOC_ENTRY);
    if (isDeleteField) {
        field.remove();
    }
}

您好,能帮忙回答下问题嘛,比较着急,谢谢

@ouchli 问题在于 TOC 字段结尾位于第二页的标题段落中。这导致了问题。您可以像这样修改代码以避免出现此问题:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

for (Field field : doc.getRange().getFields())
{
    int type = field.getType();
    boolean isDeleteField = (type == FieldType.FIELD_TOC || type == FieldType.FIELD_TOC_ENTRY);
    if (isDeleteField)
    {
        builder.moveToField(field, true);
        builder.writeln();
        field.remove();
    }
}

doc.save("C:\\Temp\\out.docx");

out.docx (28.1 KB)