DocumentBuilder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS) starts new section on new page

Hi all,

I’m trying to use Aspose.Words for Java to output first a section with two columns, then a section on the same page with one column. However, the new section starts on a new page, rather than immediately below the two column section. Any help would be much appreciated (please see code below),

Cheers,
Alasdair

DocumentBuilder builder = new DocumentBuilder();
TextColumnCollection columns = builder.getPageSetup().getTextColumns();
columns.setLineBetween(false);
columns.setCount(2);
columns.setEvenlySpaced(false);
TextColumn col0 = columns.get(0);
col0.setWidth(100);
col0.setSpaceAfter(20);
PageSetup ps = builder.getPageSetup();
columns.get(1).setWidth(ps.getPageWidth() - (ps.getLeftMargin() + ps.getRightMargin() + col0.getWidth() + col0.getSpaceAfter()));

builder.writeln("Narrow column 0");
builder.insertBreak(BreakType.COLUMN_BREAK);
builder.writeln("Wide column 1");

builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
builder.getPageSetup().clearFormatting();
builder.writeln("This is text in a new section");
builder.getDocument().save(OUTPUT_FILE);

Hi

Thanks for your inquiry. This occurs, because you use clearFormating. You should use code like the following, in your case:

// Create document and DocuemntBuidler
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Get original width of the section.
// It is needed to reset widht of the column after inserting break.
double originalWidht = builder.getPageSetup().getTextColumns().get(0).getWidth();
// Insert multicolumn section.
TextColumnCollection columns = builder.getPageSetup().getTextColumns();
columns.setCount(2);
TextColumn col0 = columns.get(0);
col0.setWidth(100);
col0.setSpaceAfter(20);
PageSetup ps = builder.getPageSetup();
columns.get(1).setWidth(ps.getPageWidth() - (ps.getLeftMargin() + ps.getRightMargin() + col0.getWidth() + col0.getSpaceAfter()));
builder.writeln("Narrow column 0");
builder.insertBreak(BreakType.COLUMN_BREAK);
builder.writeln("Wide column 1");
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
// Reset columns formating.
builder.getPageSetup().getTextColumns().setCount(1);
builder.getPageSetup().getTextColumns().get(0).setWidth(originalWidht);
builder.writeln("This is text in a new section. This is text in a new section. This is text in a new section. This is text in a new section. ");
// Save output docuemnt
doc.save("C:\\Temp\\out.doc");

I highlighted my modifications. Hope this helps.
Best regards.

Hi Alexey,

that’s exactly what I needed, thanks

Could I suggest updating the PageSetup documentation as I couldn’t see any mention of this behaviour when I looked,

Cheers,
Alasdair

Hi

Thank you for your suggestion. We will consider adding this information into the documentation.
Actually, PageSetup.clearFormating method resets page setup of the current section to its defaults. And the default section start is new page. That is why after calling this method sections starts from the new page.
Best regards.

Ah, I see now - I hadn’t realised the Section Start was a property of the PageSetup. Can I run the below solution past you? I think it achieves the same effect but eliminates the need to track the width of the section,

Cheers,
Alasdair

// Create document and DocuemntBuidler
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert multicolumn section.
TextColumnCollection columns = builder.getPageSetup().getTextColumns();
columns.setCount(2);
TextColumn col0 = columns.get(0);
col0.setWidth(100);
col0.setSpaceAfter(20);
PageSetup ps = builder.getPageSetup();
columns.get(1).setWidth(ps.getPageWidth() - (ps.getLeftMargin() + ps.getRightMargin() + col0.getWidth() + col0.getSpaceAfter()));
builder.writeln("Narrow column 0");
builder.insertBreak(BreakType.COLUMN_BREAK);
builder.writeln("Wide column 1");
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
// Reset columns formating.
builder.getPageSetup().clearFormatting();
builder.getPageSetup().setSectionStart(SectionStart.CONTINUOUS)
builder.writeln("This is text in a new section. This is text in a new section. This is text in a new section. This is text in a new section. ");
// Save output docuemnt
doc.save("C:\\Temp\\out.doc");

Hi

Yes, your code is correct and it will work as expected.
Best regards.

Great, cheers Alexey,

Alasdair