We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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


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.
<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /><o:p></o:p>



(i != 0)


Remove the copied section.



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”);<o:p></o:p>

// 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)





else if (section.PageSetup.SectionStart == SectionStart.NewPage && section != firstSection)


// Append paragraph with page break.







firstSection = section;



foreach (Section section in list)




doc.Save(MyDir + "Out.docx");