Section with multiples columns

Hello,

I would like to create a section, with 3 columns and 3 paragraphs inside, one paragraph by column.
After that, I want to create a new section (with SectionStart.CONTINUOUS for example)

Here is my code :

Section section= new Section(doc);
doc.appendChild(section);
Body body = new Body(doc);
section.appendChild(body);
section.getPageSetup().getTextColumns().setCount(3);
Paragraph paragraph1 = new Paragraph(doc);
body.appendChild(paragraph1);
Paragraph paragraph2 = new Paragraph(doc);
body.appendChild(paragraph2);
Paragraph paragraph3 = new Paragraph(doc);
body.appendChild(paragraph3);
Run run1 = new Run(doc);
paragraph1.appendChild(run1);
Run run2 = new Run(doc);
paragraph1.appendChild(run2);
Run run3 = new Run(doc);
paragraph1.appendChild(run3);
run1.setText(“Paragraph1”);
run2.setText(“Paragraph2”);
run2.setText(“Paragraph3”);

Section newSection = new Section(doc);
doc.appendChild(newSection);
newSection.getPageSetup().setSectionStart(SectionStart.CONTINUOUS);


However, when saving, all three paragraph are in the same left column and the middle and right columns are empty.What must have change to get them in the different columns ?

Thank you in advance
Jean-Pascal Lim

Hi Jean,


Thanks for your inquiry.
Jean:
I would like to create a section, with 3 columns and 3 paragraphs inside, one paragraph by column.
After that, I want to create a new section (with SectionStart.CONTINUOUS for example)
Please try run the following code snippet which achieves what you’re looking for:
DocumentBuilder builder = new DocumentBuilder();

TextColumnCollection columns = builder.getPageSetup().getTextColumns();
columns.setLineBetween(true);
columns.setEvenlySpaced(true);
columns.setCount(3);

builder.writeln(“column 1.”);
builder.insertBreak(BreakType.COLUMN_BREAK);
builder.writeln(“column 2.”);
builder.insertBreak(BreakType.COLUMN_BREAK);
builder.writeln(“column 3.”);

builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);

columns = builder.getPageSetup().getTextColumns();
columns.setCount(1);

builder.writeln(“paragraph in new section”);

builder.getDocument().save(“C:\Temp\out.docx”);

I hope, this helps.

Best regards,

Hello,

Thank you for your answer.
The problem with this solution is that you must know where you want to change of columns.
Usually, I don’t know where I want to switch of columns. I just want my text to be displayed on 3 columns.

The exact thing I want to do is the same that in Microsoft Word :

- Write my paragraph, texts, etc.
- Set the columns number of the current section to 3
- Insert a new Section Continuous

When I do this, Microsoft Word calculates where to change of columns and then distributes the texts and paragraphs amongst the 3 columns of the same height.
That’s exactly what I want to do.
How could I obtain such a result ?

Thank you in advance

Jean-Pascal Lim

Hi Jean,


Thanks for your inquiry. Sure, you can still achieve the same using Aspose.Words. Please try run the following code snippet:
DocumentBuilder builder = new DocumentBuilder();

for (int i = 0; i < 15; i++)
builder.writeln("this is a long text. this is a long text. this is a long text. this is a long text. this is a long text. this is a long text. this is a long text. this is a long text. this is a long text. this is a long text. ");

TextColumnCollection columns = builder.getPageSetup().getTextColumns();
columns.setLineBetween(true);
columns.setEvenlySpaced(true);
columns.setCount(3);

builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);

columns = builder.getPageSetup().getTextColumns();
columns.setCount(1);

builder.writeln(“paragraph in new section”);

builder.getDocument().save(“C:\Temp\out.docx”);

I hope, this helps.

Best regards,