Inserting documents sets unexpected header on new section

There are 2 documents. I am inserting one into other.
First/destination document (FirstDocument.docx) has defined header and header with different first page.
Second/source document (SecondDocument.docx) has only defined header.

I am inserting second document into first with DocumentBuilder. Before insertion of second document I am adding one Section Break (Continuous). Result document has 2 sections.

Expected behavior in result document (Expected.docx): in first section header and footer should remain as they were in first document - header/footer with different first page. Second section has to have header/footer taken from second document.

Result document that is not good (Result.docx) : on both sections contains header/footer with different first page from first document.

Code snippet :

public static void HeadersIssue()
{
    Document destDoc = new Document("FirstDocument.docx");
    Document sourceDoc = new Document("SecondDocument.docx");
    destDoc.InsertContinuousSectionBreak();
    DocumentBuilder builder = new DocumentBuilder(destDoc);
    builder.MoveToDocumentEnd();
    builder.InsertDocument(sourceDoc, ImportFormatMode.UseDestinationStyles);
    string documentName = Guid.NewGuid() + ".docx";
    destDoc.Save(documentName, SaveFormat.Docx);
}

public static void InsertContinuousSectionBreak(this Document document)
{
    DocumentBuilder builder = new DocumentBuilder(document);
    builder.MoveToDocumentEnd();
    builder.InsertBreak(BreakType.SectionBreakContinuous);
}

Thanks,
Rastko

Hi Rastko,

Please use the following code to get the expected output:

Document destDoc = new Document(MyDir + "FirstDocument.docx");
Document sourceDoc = new Document(MyDir + "SecondDocument.docx");
sourceDoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = false;
sourceDoc.FirstSection.HeadersFooters.LinkToPrevious(false);
// Set the appended document to appear on the same page.
sourceDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
destDoc.AppendDocument(sourceDoc, ImportFormatMode.UseDestinationStyles);
destDoc.Save(MyDir + @"16.8.0.docx", SaveFormat.Docx);

Hope, this helps.

Best regards,

Thanks Awais for your answer.

I have to accomplish this with DocumentBuilder and InsertDocument. I can not use AppendDocument as I need previous approach for many reasons.

Could you give me sample for that as I can not find solution for it.

Thanks,

Hi Rastko,

Please try using the following code:

Document destDoc = new Document(MyDir + "FirstDocument.docx");
Document sourceDoc = new Document(MyDir + "SecondDocument.docx");
sourceDoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = false;
sourceDoc.FirstSection.HeadersFooters.LinkToPrevious(false);
InsertContinuousSectionBreak(destDoc);
DocumentBuilder builder = new DocumentBuilder(destDoc);
builder.MoveToDocumentEnd();
builder.InsertDocument(sourceDoc, ImportFormatMode.UseDestinationStyles);
foreach (HeaderFooter hf in sourceDoc.FirstSection.HeadersFooters)
{
    destDoc.LastSection.HeadersFooters.Add(destDoc.ImportNode(hf, true, 
    ImportFormatMode.UseDestinationStyles));
}
destDoc.Save(MyDir + @"16.8.0.docx", SaveFormat.Docx);

Hope, this helps.

Best regards,

Thanks Awais for provided sample.

I have found that should be changed as in result document I have again first header-footer.
So I have added additional line :

foreach (HeaderFooter hf in sourceDoc.FirstSection.HeadersFooters)
{
    destDoc.LastSection.HeadersFooters.Add(destDoc.ImportNode(hf, true, ImportFormatMode.UseDestinationStyles));
    destDoc.LastSection.PageSetup.DifferentFirstPageHeaderFooter = false;
}

Also this code snippet covers situation where source document is one-section. In multi-section document I have to add header-footers from the first section of inserted document. I guess that I should not do additional setting on other sections of source document ?

Thanks

Hi Rastko,

You need to import all headers/footers from all sections in documents being imported to destination document. You can then try linking or unlinking all headers and footers to the corresponding headers and footers in the previous section using HeaderFooterCollection.LinkToPrevious method to get the desired output.

Best regards,

Hi Awais,

what I have found in case when inserting document that is multi-section is that only on last section were not properly added/ migrated header-footers. Also only on that one (last) section should be applied DifferentFirstPageHeaderFooter. All other sections were added without any additional intervention.

For me this is very strange behavior - looks like a bug. Why simply it is not added everything - (headers, footers from all section) as they were in original document.

Thanks,
Rastko

Hi Rastko,

Thanks for your inquiry. Could you please attach your 1) input Word document, 2) Aspose.Words generated output document showing the undesired behavior, 3) source code and 4) expected document here for our reference? We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word. We will then provide you code to achieve the same using Aspose.Words.

Best regards,