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

Free Support Forum - aspose.com

How to replace ControlChar.SECTION_BREAK to ControlChar.PAGE_BREAK

Hi supporter,

I need to replace ControlChar.SECTION_BREAK to ControlChar.PAGE_BREAK.

This is my old code. In this below code section breack are removed from docment. Now, we want to replace section break to page break.

for (int i = doc.getSections().getCount() - 2; i >= 0; i--) {
doc.getLastSection().prependContent(interimdoc.getSections().get((i)));
doc.getSections().get(i).remove();
doc.save(interim);

Thanks in advance.

@Mahesh39 You can use code like the following to achieve what you need:

Document doc = new Document("C:\\Temp\\in.docx");

Run pageBreak = new Run(doc, ControlChar.PAGE_BREAK);
while (doc.getSections().getCount() > 1)
{
    Section last = doc.getSections().get(doc.getSections().getCount() - 1);
    Section prev = doc.getSections().get(doc.getSections().getCount() - 2);
    prev.getBody().getLastParagraph().appendChild(pageBreak.deepClone(true));
    last.prependContent(prev);
    prev.remove();
}

doc.save("C:\\Temp\\out.docx");