We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Header and Footer format different from first page

I have a use case where a “master” template should be used to generate a document. The header and footer of this template should be used in the generated document.
I am able to do that, however the format is different from the original.

Aspose version: 16.8.0

This is the code I’m using:

public void generateForumExample() throws Exception {

final javax.xml.parsers.DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final org.w3c.dom.Document data = db.parse(dataDir + “example_data.xml”);

Locale.setDefault(new Locale(“en”, “US”));
final Document srcDoc = new Document(dataDir + “example_template.docx”);
final Document dstDoc = new Document(dataDir + “example_doc_header_footer.docx”);


dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dstDoc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(data));

dstDoc.save(dataDir + “example_result.docx”);


Can you give me a hint about what I am not doing correctly?

Hi there,

Thanks for your inquiry.

Could you please share some more detail about incorrect formatting in output document with screenshots of problematic section? Please also share your expected output document. We will investigate the issue on our side and provide you more information.

Hi Tahir,

Please see the attached files. Let me know if more information is necessary.


Thanks for your inquiry.

While using the latest version of Aspose.Words i.e. 17.2.0, we managed to reproduce this issue on our end. We have logged this issue in our bug tracking system. The ID of this issue is WORDSNET-14986. Your thread has also been linked to the appropriate issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best regards,


Regarding WORDSNET-14986, our product team has completed the analysis of your issue and has come to a conclusion that this issue is not a bug in Aspose.Words but an expected behavior. Your issue (WORDSNET-14986) will be closed with ‘Not a Bug’ resolution.

The source and destination documents have different page setups including margins.

Note, that AppendDocument() method always inserts document preserving source section, so you can see ‘SectionBreak’ inside resultant document.

The analogue in MS Word is first insert section break into the end of destination document (‘Layout’ -> ‘Breaks’ -> ‘Section Breaks’ -> ‘Next Page’) and then copy-paste source document. In that case, you will see the same result as in Aspose.Words.

However, to achieve desired result, you should either additionally before adding document set appropriate margin:

<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>srcDoc.FirstSection.PageSetup.LeftMargin = dstDoc.FirstSection.PageSetup.LeftMargin;

or use DocumentBuilder.InsertDocument() method instead:

<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>DocumentBuilder builder = new DocumentBuilder(dstDoc);
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>dstDoc.FirstSection.Body.AppendParagraph("");
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>builder.MoveToDocumentEnd();
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>builder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);

Best regards,