How could i keep the original header of certain section , while changing other sections?

This is the code that I change header of a certain section. however, it could also changed some header of sections that i don’t want to. seems like it remove the original one and continue

private void generateHeader(Section section, String text)
    throws Exception {

    Document document = (Document) section.getDocument();
    HeaderFooter headerFirst = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST);
    if (headerFirst == null) {
        headerFirst = new HeaderFooter(document, HeaderFooterType.HEADER_FIRST);
        section.getHeadersFooters().add(headerFirst);
    } else {
        // todo  把header删除
        headerFirst.getChildNodes(NodeType.PARAGRAPH, true).clear();
    }

    Paragraph headerParagraph = new Paragraph(document);
    if (text != null) {
        Run runFirst = new Run(document, text);
        headerParagraph.appendChild(runFirst);
    }

    FieldStyleRef fieldStyleRefFirst = (FieldStyleRef) headerParagraph.appendField(FieldType.FIELD_STYLE_REF, false);
    fieldStyleRefFirst.setStyleName("customHeading_1");
    // 将段落添加到第一页的页眉
    headerFirst.appendChild(headerParagraph);

    // 处理其他页的主要页眉
    HeaderFooter headerPrimary = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
    if (headerPrimary == null) {
        headerPrimary = new HeaderFooter(document, HeaderFooterType.HEADER_PRIMARY);
        section.getHeadersFooters().add(headerPrimary);
    } else {
        headerPrimary.getChildNodes(NodeType.PARAGRAPH, true).clear();
    }

    // 创建新的段落和 StyleRef 字段
    Paragraph paraPrimary = new Paragraph(document);

    // 插入指定的文字,例如 "第一章"
    if (text != null) {
        Run runPrimary = new Run(document, text);
        paraPrimary.appendChild(runPrimary);
    }


    // 创建 StyleRef 字段并插入到段落中
    FieldStyleRef fieldStyleRefPrimary = (FieldStyleRef) paraPrimary.appendField(FieldType.FIELD_STYLE_REF, false);
    fieldStyleRefPrimary.setStyleName("customHeading_1");

    // 将段落添加到主要页眉
    headerPrimary.appendChild(paraPrimary);

    ParagraphUtils.setHeaderParaFormat(headerParagraph);
    ParagraphUtils.setHeaderParaFormat(paraPrimary);
}

like this
original:


changed one :

the ‘符号和缩略语说明’ is the header of the last section.
how can i fix it ?

@Madecho Most likely the problem occurs because the section inherit headers/footers from the previous section. You can try using code like the following before processing document using your code:

private static void copyHeadersFooters(Document doc)
{
    int[] headerFooterTypes = new int[]{
            HeaderFooterType.HEADER_FIRST,
            HeaderFooterType.HEADER_PRIMARY,
            HeaderFooterType.HEADER_EVEN,
            HeaderFooterType.FOOTER_FIRST,
            HeaderFooterType.FOOTER_PRIMARY,
            HeaderFooterType.FOOTER_EVEN
    };
        
    // Loop through all sections
    for(Section sect : doc.getSections()) {
        // Do not process the first section
        if (sect.getPreviousSibling() == null)
            continue;
    
        Section prevSection = (Section) sect.getPreviousSibling();
        for (int hfType : headerFooterTypes) {
            if (prevSection.getHeadersFooters().getByHeaderFooterType(hfType) != null &&
                    sect.getHeadersFooters().getByHeaderFooterType(hfType) == null)
                sect.getHeadersFooters().add(prevSection.getHeadersFooters().getByHeaderFooterType(hfType).deepClone(true));
        }
    }
}