Strange Behavior with appended documents

I was doing a test with appended documents and a different header on the first page and I ran into a strange issue.


I have a base document where I selected the option in the header design menu “Different First Page”. This allows you to provide 2 headers for a word document 1 that shows up on the first page and one that shows up on all subsequent pages. I then appended some documents to the first document using the appendDocument method and passing the option continuous.

doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);

If you then delete the first section break in the document you will notice that doing that turns off the Different First Page header selection. I am sure this isnt totatlly clear so I attached a sample document generated by Aspose v 11.3. If you turn on show all formatting marks you will see the section breaks (Continous) delete the first one on the page and look at the header and you will see what I am referring to.

Here is the code I used to assemble the documents.
public class AppendDocuments {
public static void main(String[] args) {
try {
Document doc1 = new Document(“Template1.docx”);
Document doc2 = new Document(“Template2.docx”);
Document doc3 = new Document(“Template3.docx”);
doc2.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
doc3.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc1.appendDocument(doc3, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc1.save("./OutputDoc.docx");
} catch (Exception e) {
e.printStackTrace();
}
}
Hi Cameron,

Thanks for the inquiry. Could you please share some credentials i.e. Aspose.Words version, MS office, JDK and OS, input word documents etc. We'll take a closer look and guide you accordingly.

Hi I attached the project I made to test the assembling of a document. This includes the code I used the little template files I that I assembled and a normal word document that I used to confirm that it wasn't a Microsoft Word Issue (at least I dont think it is a Microsoft Word issue). I left out the aspose library to reduce the size of the zip file.

I am using

  • Aspose Word v 11.3
  • JDK 6
  • Windows 7 but I did test on a linux enviroment as well and I got the same behavior

Please let me know if I can provide anything else.

Hi Cameron,


Thanks for your inquiry.

Please note that there can be a maximum of only one HeaderFooter of each HeaderFooterType perSection. Talking about headers, there are of three types i.e. HeaderFirst, HeaderPrimary and HeaderEven; so up to three different headers are possible in a section (i.e. for first, even and odd pages).

What I understand, you have HeaderFirst and HeaderPrimary headers in your Template1.docx and removing the First Section break removes both of these headers as well. In this case, in your resultant document, you can try appending all sections to the first section and remove them afterwards. Please see the following code snippet:
Document doc1 = new Document(“c:\test\Doc1.docx”);
Document doc2 = new Document(“c:\test\Doc2.docx”);
Document doc3 = new Document(“c:\test\Doc3.docx”);

doc2.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
doc3.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);

doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc1.appendDocument(doc3, ImportFormatMode.KEEP_SOURCE_FORMATTING);

int secCount = doc1.getSections().getCount();
for (int i = 1; i < secCount; i++)
{
doc1.getFirstSection().appendContent(doc1.getSections().get(1));
doc1.getSections().get(1).remove();
}

doc1.save(“c:\test\OutputDoc.docx”);

I hope, this will help.

Best Regards,

Yep that worked, thanks!!!