I am generating a document by appending two documents together. using below code
Document mainDocument = new Document(mainFilePath);
Document srcDocument = new Document(sourceFilePath);
mainDocument.appendDocument(srcDocument, ImportFormatMode.KEEP_SOURCE_FORMATTING);
mainDocument.save(docOutputPath, SaveFormat.DOCX);
My main document’s last header is selected as Different Odd & Even pages. My appended document also has a header, which is repeated on every page. But in the resulting document appended document header is repeated alternatively. I am attaching the input files and output.
@Gptrnt This occurs because even header/footer is inherited from the from the previous section, since the appended document does not have explicitly specified even header/footer. You can work this around by creating a copy of primary header/footer to even header/footer to prevent inheriting:
Document mainDocument = new Document("C:\\Temp\\Main_File.docx");
Document srcDocument = new Document("C:\\Temp\\template.docx");
// Copy primary header/footer to even header/footer
HeaderFooter primaryHeader = srcDocument.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
HeaderFooter primaryFooter = srcDocument.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
HeaderFooter evenHeader = new HeaderFooter(srcDocument, HeaderFooterType.HEADER_EVEN);
HeaderFooter evenFooter = new HeaderFooter(srcDocument, HeaderFooterType.FOOTER_EVEN);
for (Node n : (Iterable<Node>)primaryHeader.getChildNodes(NodeType.ANY, false))
evenHeader.appendChild(n.deepClone(true));
for (Node n : (Iterable<Node>)primaryFooter.getChildNodes(NodeType.ANY, false))
evenFooter.appendChild(n.deepClone(true));
srcDocument.getFirstSection().getHeadersFooters().add(evenHeader);
srcDocument.getFirstSection().getHeadersFooters().add(evenFooter);
mainDocument.appendDocument(srcDocument, ImportFormatMode.KEEP_SOURCE_FORMATTING);
mainDocument.save("C:\\Temp\\out.docx", SaveFormat.DOCX);
I am using the above solution but getting the below error,
java.lang.IllegalArgumentException: Cannot insert a node of this type at this location.
at com.aspose.words.CompositeNode.zzWGp(Unknown Source)
at com.aspose.words.CompositeNode.insertAfter(Unknown Source)
at com.aspose.words.CompositeNode.appendChild(Unknown Source)
at com.aspose.words.NodeCollection.add(Unknown Source)
I am creating both documents using the inputstream from the Amazon s3 bucket.
@Gptrnt As I can see you are using old 21.1 version of Aspose.Words for java. Please try using the latest 23.8 version and let us know if the problem still persists on your side.
@Gptrnt Could you please attach the documents you get the issue with? I have tested with the document you have attached earlier and the code works as expected.
@Gptrnt You should remove existing even header and footer before inserting newly created ones. Please modify your code like this:
// Remove existing even header and footer.
if (srcDocument.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN) != null)
srcDocument.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN).remove();
if (srcDocument.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN) != null)
srcDocument.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN).remove();
srcDocument.getFirstSection().getHeadersFooters().add(evenHeader);
srcDocument.getFirstSection().getHeadersFooters().add(evenFooter);
I have updated my code with the above solution, creating another issue. I have uploaded the main document file Main_File.docx (34.5 KB) and srcDocument as source.docx (68.3 KB). In the main document, I have selected the header as Different Odd & Even pages. But the result document after appended shows the header on all the pages. Can you please help me to also fix this issue?
@GptrntDifferent Odd & Even pages is set per section. When you append the document, all section from the source document are copied into the destination document keeping their settings. So Different Odd & Even pages set in the main document does not affect the appended sections.
If I try the below sample input document with the attached sample code I get the output with all the pages having a header icon instead of consecutive pages.
Inputs : source.docx (68.3 KB)
If you see in the uploaded input Main_File.docx has a header with Different Odd & Even Pages selected which is coming only on the consecutive page. But after the document is appended with the source document, the output document all pages contain the header instead of consecutive pages.
@Gptrnt In your code you copy primary header/footer into even header/footer so both odd and even header/footer have the same content. To get the expected output you should use the following code:
Those copying the primary header/footer into even header/footer code are given by you for the first issue mentioned at the top of this ticket. If I remove those codes then that issue will trigger again. I want the solution for working in both scenarios.
@Gptrnt I have investigated the issue a bit deeper and evenAndOddHeaders is stored in settings.xml, so this options is set per document, not per section. So for the case from your initial post the expected output can be achieve using the following code:
Document mainDocument = new Document("C:\\Temp\\Main_File.docx");
Document srcDocument = new Document("C:\\Temp\\template.docx");
mainDocument.getFirstSection().getPageSetup().setOddAndEvenPagesHeaderFooter(false);
mainDocument.appendDocument(srcDocument, ImportFormatMode.KEEP_SOURCE_FORMATTING);
mainDocument.save("C:\\Temp\\out.docx", SaveFormat.DOCX);
So you can try using the following code if it is required to have header/footer settings from the appended document:
The above solution is working fine with the issues mentioned earlier. But it creates another issue in the second-page header of the Main_File.docx (79.5 KB) document if template.docx (32.1 KB) document is attached along with it. In the Main_File.docx document that header is selected as Different Odd & Even Pages, but in an appended document it will be showing not selected output.docx (81.8 KB). Please check this case as well.
@Gptrnt If the option will be selected as in the main document, it will affect whole document and the header/footer will be switched on even and odd pages.
@Gptrnt I am afraid there is no other way to preserve both “Different Odd & Even Pages” setting and header footer on each page in the appended document other than copying content of the primary header/footer into even header/footer as suggested earlier. But such approach causes issues with other your documents.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.