Body Content Overlaps with Header After Merging Multiple Word Documents

I’m merging multiple Word documents using the code below . Each document may have different headers and footers—or none at all. I want the merged document to retain the original headers and footers from each source file, applied to the corresponding pages. However, after merging, the body content shifts upward and overlaps with the header area in the final document. How can I prevent the body content from overlapping with the header section during the merge?

Document document = null;
for (String fileName : wordFileList) {
	Document sourceDoc = new Document(fileName);
	sourceDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
	if (this.document == null) {
		this.document = sourceDoc;
	} else {
		sourceDoc.getFirstSection().getHeadersFooters().linkToPrevious(false);
		this.document.appendDocument(sourceDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
	}
}
this.document.updateFields();
this.document.save(filePath);

@spthakare02

To prevent body content from overlapping with the header section when merging multiple Word documents, you can adjust the page setup for each section in your documents. Specifically, you should ensure that the margins for the top of each section are set appropriately to accommodate the header height.

Here’s how you can modify your existing code to set the top margin for each section, which will help prevent the body content from overlapping with the header:

Document document = null;
for (String fileName : wordFileList) {
    Document sourceDoc = new Document(fileName);
    // Set the section to start on a new page
    sourceDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
    
    // Set the top margin to avoid overlap with the header
    double headerHeight = sourceDoc.getFirstSection().getHeadersFooters().getHeaderFooter(HeaderFooterType.HEADER_PRIMARY).getChildNodes(NodeType.SHAPE, true).getCount() > 0 ? 50 : 0; // Adjust this value based on your header height
    sourceDoc.getFirstSection().getPageSetup().setTopMargin(headerHeight + 10); // Add some extra space

    if (this.document == null) {
        this.document = sourceDoc;
    } else {
        sourceDoc.getFirstSection().getHeadersFooters().linkToPrevious(false);
        this.document.appendDocument(sourceDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
    }
}
this.document.updateFields();
this.document.save(filePath);

In this example, the headerHeight variable is calculated based on whether the primary header contains any shapes (or other content). You may need to adjust the value used for headerHeight based on your specific header content and desired spacing. The setTopMargin method is then used to set the top margin of the section, ensuring that the body content does not overlap with the header.

By configuring the margins appropriately, you should be able to retain the original headers and footers from each source document while preventing any overlap with the body content in the merged document.

For more detailed information on merging documents and managing headers and footers, you can refer to the Aspose documentation on joining and appending documents here.

Why do we need to set the top margin in the source file? In the original document, I can see the header correctly, and the body content starts right below the header. When merging, shouldn’t the header height and spacing be preserved automatically?
I tried the solution you provided, but it didn’t work — the body content still overlaps with the header in the merged document.

@spthakare02 Please try using Merger class to merge documents:

Document doc1 = new Document("C:\\Temp\\in1.docx");
Document doc2 = new Document("C:\\Temp\\in2.docx");
    
Document merged = Merger.merge(new Document[] { doc1, doc2 }, MergeFormatMode.KEEP_SOURCE_LAYOUT);
    
merged.save("C:\\Temp\\out.docx");

Apologies, I found that the issue was due to how the input documents were created. If the following line is used while creating the document:

document.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);

then the document is generated correctly, and the body content does not overlap with the header. However, if this line is not included, the body content ends up overlapping with the header section. So the problem wasn’t with the merge functionality, but with how the input files were originally generated.

Why does omitting this line cause the body content to overlap with the header? What changes in layout behavior when optimizeFor(MsWordVersion.WORD_2019) is not set?

@spthakare02 When you optimize for MsWordVersion.WORD_2019 Aspose.Words sets the appropriate compatibility options in the document. These compatibility options might affect the document’s content layout, because different versions of MS Word have different document layout rules.

@alexey.noskov Sorry again, I have corrected my previous question. Please check that.

Apologies, I found that the issue was due to how the input documents were created. If the following line is used while creating the document:

document.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);

then the document is generated correctly, and the body content does not overlap with the header. However, if this line is not included, the body content ends up overlapping with the header section. So the problem wasn’t with the merge functionality, but with how the input files were originally generated.

Why does omitting this line cause the body content to overlap with the header? What changes in layout behavior when optimizeFor(MsWordVersion.WORD_2019) is not set?

@spthakare02 Please see my previous answer. MS Word documents created by different MS Word versions have different set of compatibility options to make them look the same in different versions of MS Word. but when you merge documents, compatibility options of the first document are applied to the whole document. So there might be difference in layout.
You can use Merger class to merge documents with MergeFormatMode.KEEP_SOURCE_LAYOUT in this case Aspose.Words tries to take in account compatibility options to keep the documents original layout after merging.

@alexey.noskov Thanks for the quick response. It turns out the issue isn’t with the merging process, but with the input files themselves, which are also generated using Aspose.Words. When creating a Word document with both header and body content, if I don’t use the following line:

document.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);

the body content ends up overlapping with the header. However, when I do include this line, the document is generated correctly, and the content layout appears as expected.

Why does excluding this line cause the body content to overlap with the header? What layout behavior does this API call influence to prevent that?

@spthakare02 Unfortunately, I have nothing to add to the answers provided above. In short, setting different compatibility options affects document layout.