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

Free Support Forum - aspose.com

Replace Section Break by Page Break

Hi,

We have a word document with section breaks. All we need to do is replace all the section break with page break. Can you please assist?

Thank you.

Hi,

Can you please respond?

Hi Komal,


Thanks for your inquiry. Please use the following code snippet to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

while (doc.FirstSection.NextSibling != null)

{

//Get second section

Section nextSect = (Section)doc.FirstSection.NextSibling;

//Move DocumentBuilder cursor to the end of first section

builder.MoveTo(doc.FirstSection.Body.LastParagraph);

//if SectionStart of second section is NewPage, EvenPage or OddPage

//we should replace such break with page break

//If SectionStart is Continouse then just appent content of next section to first section

//If SectionStart is NewColumn then replace it with Column break

if (nextSect.PageSetup.SectionStart.Equals(SectionStart.NewColumn))

builder.InsertBreak(BreakType.ColumnBreak);

else if (nextSect.PageSetup.SectionStart.Equals(SectionStart.Continuous))

{

//do nothing

}

else

builder.InsertBreak(BreakType.PageBreak);

//Append content of second section to first section

doc.FirstSection.AppendContent(nextSect);

//Remove next section

nextSect.Remove();

}

//Save document

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


Thanks. I'll try this but looks like I missed this on my initial post.

Our Document has page break added in all the places except ONE place. Only one place in the document will have a section break. We need to find that place and replace the section break with page break.

I am not sure whether the above code will work for this. However I'll try once.

Alos why do we need to add BreakType.ColumnBreak? our doument does not need that. All we need is replace that ONE section break to page break.

Hi Komal,


Thanks for your inquiry. Yes, the shared code works in your case. If your documents have not any section which starts from a new column, you can exclude code related to BreakType.ColumnBreak. If you face any issue, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.