How to remove Section Break while joining Documents using .NET

Hello,

I have Aspose Total.NET enterprise license (Aspose.Word v20.12.0)

I am joining 3 word docs together.
Each doc goes through some mail merge and then they are joined together.

  1. Header doc - is only header, no footer
  2. Summary doc - no header, no footer
  3. PayBill doc - no header, no footer

the problem is that there is a Section Break created on first page - I don’t know why.
I do not want this Section break.
How can i prevent it, or how do i remove it after the fact.

here is the code…
Document docHeader = new Document(“Header.docx”);
Document docSummary = new Document(“Summary.docx”);
Document docPayBill = new Document(“PayBill.docx”);

docHeader.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

docHeader.AppendDocument(docSummary, ImportFormatMode.KeepSourceFormatting);

// Start on New Page…
docPayBill.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
docHeader.AppendDocument(docPayBill, ImportFormatMode.KeepSourceFormatting);

docHeader.Save(“finalDoc.docx”);
see attached sample docs.

word section break issue.zip (206.3 KB)
Thanks

@tony.woods.bell.ca

In your case, we suggest you please use DocumentBuilder.InsertDocument method to insert the document as shown below. Hope this helps you.

Document docHeader = new Document("Header.docx");
Document docSummary = new Document("Summary.docx");
Document docPayBill = new Document("PayBill.docx");

DocumentBuilder builder = new DocumentBuilder(docHeader);
builder.InsertDocument(docSummary, ImportFormatMode.KeepSourceFormatting);
builder.InsertDocument(docPayBill, ImportFormatMode.KeepSourceFormatting);

docHeader.Save("finalDoc.docx");

If you do not want to use DocumentBuilder.InsertDocument method, you can remove the section breaks from the final document using following method.

private static void RemoveSectionBreaks(Document doc)
{
    for (int i = doc.Sections.Count - 2; i >= 0; i--)
    {
        doc.LastSection.PrependContent(doc.Sections[i]);
        doc.Sections[i].Remove();
    }
}

In documentBuilder…

What if I want to insert a document but it MUST start on a new page.
How do i do that?

Nitin

Hi tahir,

i tried the DocumentBuilder code,
but no headers show on any of the pages.

@tony.woods.bell.ca

You can use DocumentBuilder.InsertBreak method to insert page break before inserting document.

builder.InsertBreak(BreakType.PageBreak);

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hello Tahir,

I created the sample project and when I ran it, I did not see the issue anymore.
So I checked my the code in my real project and identified the error in a particular
section (which had a section break) and was able to remove that.

Thank you once again for a great product and support.