Copying all sections content into a single sectin in such a way that no of pages dont vary in source and destination documents

Hi,

I want to copy all sections content in to a single section. For instance if there are 2 sections in source document then in destination document there should be only one section. The code what i am using now is

for (int i = doc.Sections.Count - 2; i >= 0; i--)
{
    // Copy the content of the current section to the beginning of the last section.
    doc.LastSection.PrependContent(doc.Sections[i]);
    // doc.LastChild.PrependContent(doc.Sections[i]);
    if (i != 0)
        CopyHeadersFootersFromPreviousSection(doc.LastSection);
    // Remove the copied section.
    doc.Sections[i].Remove();
}

But i am experiencing a problem with the above code. For example in source document there are two sections each section with three pages then after copying all sections content in to one section my destination document is having 5 pages only(this happens because in section two in the second page only half page content is full remaining half is empty but with the help of above code whats happening is ,it is copying third page and second page content in one page only).

I want to copy all sections content of a document in to one section in such a way that number of pages dont vary in source document and destination document even if in source document few pages have only half page filled.

Please help me to resolve this issue.

Hi there,

Thanks for your inquiry.

Please use the following code snippet to achieve your requirements. Hope this helps you.

If you still face problem, please share your input and expected output document here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Document doc = new  Document(MyDir + "in.docx");
// Create a paragraph with page break.
// We will insert this paragraph at the end of section and then append content of the next section ot the current.
Paragraph breakParagraph = new Paragraph(doc);
breakParagraph.AppendChild(new Run(doc, ControlChar.PageBreak));
breakParagraph.ParagraphBreakFont.Size = 1;
breakParagraph.ParagraphFormat.SpaceAfter = 0;
breakParagraph.ParagraphFormat.SpaceBefore = 0;
ArrayList list = new ArrayList();
Section firstSection = doc.FirstSection;
foreach (Section section in doc.Sections)
{
    if (section.PageSetup.SectionStart == SectionStart.Continuous && section != firstSection)
    {
        list.Add(section);
        firstSection.AppendContent(section);
    }
    else if (section.PageSetup.SectionStart == SectionStart.NewPage && section != firstSection)
    {
        // Append paragraph with page break.
        firstSection.Body.AppendChild(breakParagraph.Clone(true));
        list.Add(section);
        firstSection.AppendContent(section);
    }
    else
    {
        firstSection = section;
    }
}
foreach (Section section in list)
{
    doc.RemoveChild(section);
}
doc.Save(MyDir + "Out.docx");