No headers when generate DOCX based on template with ReportingEngine

Hello, we generate a DOCX based on a simple one-page template (several variables in the body and header).
When the source document contains 1 section - generation works without issues; yet, if there are 2+ sections, the headers for the first section are ignored and appear only started from the second one. The variables in the body are populated as expected.
This can be reproduced in the latest 23.8 Aspose Words Java version.

Header sample:
Some text <<[variable1()]>> <<[variable2()]>>
---------------------------------------------- (line)
Some text <<[variable3()]>> <<[variable4()]>>

We would highly appreciate it, if you could assist with a workaround, so the headers could be added for all the sections.

@starkmarc Could you please provide a sample template, output and expected output here for our reference? We will check the issue and provide you more information.

Headers_issue.zip (91.8 КБ)
@alexey.noskov enclosed, please, find the archive with out template, expected, actual output, and the source file.
There are 3 sections in the source, yet, the headers are added only in the last one.

@starkmarc First of all in your source document section has their own empty headers/footers. So to make the document to inherit header footer from the previous section, you shod remove existing headers/footers and reset DifferentFirstPageHeaderFooter and OddAndEvenPagesHeaderFooter:

for (Section s : src.getSections())
{
    s.getPageSetup().setDifferentFirstPageHeaderFooter(false);
    s.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
    s.getHeadersFooters().clear();
}

Also, it is required to insert section break before the document placeholder. Please try using the following modified template: template.docx (18.9 KB)

test_after_adjustments_1.docx (22.5 КБ)
@alexey.noskov Great, this helps and now the headers are on all the pages, thank you! However, there is a new issue: all the content before from the template is on one page (please, see the attached file).
Could you guide how to remove this gap, so the content can be placed right after , not on the next page?

@starkmarc First of all your source document has different page orientation than the target document. If sections has different page setup MS Word starts the second section from a new page. Also, the first section of the source document has section start new page. You can reset it to continuous. In this case if source and target documents have the same page setup there will no be page break.

@alexey.noskov Thank you, with the following workaround it is possible to add headers on all the pages:

  1. Add a page break to the template

  2. Remove the headers and footers in the source document

for (Section s : src.getSections())
{
    s.getPageSetup().setDifferentFirstPageHeaderFooter(false);
    s.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
    s.getHeadersFooters().clear();
}
  1. After generation, append the content of the second section to the first one and remove the second section. This removes the page break after the first section.

  2. s.getPageSetup().setHeaderDistance(firstSection.getPageSetup().getHeaderDistance()) for every section after generation to allign the margins in the headers.

Could you advice how to programmatically check if the headers are available on all the pages of the generated document?
We tried with

section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN)
section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST)
section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY)

for every section; yet, they are null started from the second section, but the headers can be seen in Word.

@starkmarc In MS Word headers/footers are inherited from the previous section if they are not defined in the current section. So if header/footer in the current section is null and the same header/footer is defined in the previous section, it will be displayed in the current section. If you need to make sure header/footer is displayed on all pages, you can copy headers/footers to all sections in your document. and make sure DifferentFirstPageHeaderFooter and OddAndEvenPagesHeaderFooter have the same value in all sections.