Header and Footer Issue While appending two document

HI,

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.

template.docx (32.1 KB)
Main_File.docx (79.5 KB)
output.docx (81.8 KB)

expected: appended document header in the output document should repeat on each page. Please help to fix this issue.

Thank you

@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);

Hi,

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.

Thank you

@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.

Hi,

I have updated the package and tried the above solution. I am still getting the same error.

Thank you

@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.

Hi,

I can reproduce the issue by using the sample code below.

String mainFilePath = "./src/main/resources/document/Main_File.docx";
String sourceFilePath = "./src/main/resources/document/source.docx";

InputStream noticeInputStream = Files.newInputStream(Paths.get(mainFilePath));
InputStream inputStream = Files.newInputStream(Paths.get(sourceFilePath));


Document mainDocument = new Document(inputStream);
Document srcDocument = new Document(noticeInputStream);
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(docOutputPath);

Can you please help me to figure out the solution for it? I have used the same sample file uploaded above and have tested with version 23.8 as well.

Thank you

@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);

Hi,

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?

Thank you

@Gptrnt Different 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.

Hi Alexey,

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)

Main_File.docx (34.5 KB)

Sample code:-
Main.zip (884 Bytes)
output :-1:
output.docx (82.7 KB)

Expected output;-
output.docx (94.6 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:

String docOutputPath = "C:\\Temp\\output.docx";
String mainFilePath = "C:\\Temp\\Main_File.docx";
String sourceFilePath = "C:\\Temp\\source.docx";

InputStream noticeInputStream = Files.newInputStream(Paths.get(mainFilePath));
InputStream inputStream = Files.newInputStream(Paths.get(sourceFilePath));

Document mainDocument = new Document(inputStream);
Document srcDocument = new Document(noticeInputStream);

mainDocument.appendDocument(srcDocument, ImportFormatMode.KEEP_SOURCE_FORMATTING);
mainDocument.save(docOutputPath);

Hi Alexey,

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.

Thank you

@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:

mainDocument.getFirstSection().getPageSetup().setOddAndEvenPagesHeaderFooter(srcDocument.getFirstSection().getPageSetup().getOddAndEvenPagesHeaderFooter());

Hi Alexey,

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.

Thank you

@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.

Hi Alexey,

Is there any way I can keep both document headers setting as it is in the documents after appending it together?

@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.