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

Free Support Forum - aspose.com

How to place the table on another page- if there is no enough room on previous page

Hi, I am creating a table with in another table. Sometimes if there is no enough room on the first page, the child table was wrapped in two pages. If there is no enough room in one page to fit entire table contents, I need it to start the whole table on another page. Can you help me out on this? I have attached my output document also. Here is the code I was using to generate the sample table:

protected void Page_Load(object sender, EventArgs e)
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Aspose.Words.Tables.Table table = new Aspose.Words.Tables.Table(doc);

for (int i = 0; i < 50; i += 2)
{
Row row = new Row(doc);

Cell cell1 = new Cell(doc);
Cell cell2 = new Cell(doc);

cell1.AppendChild(GetTable(doc));
row.Cells.Add(cell1);

cell2.AppendChild(GetTable(doc));
row.Cells.Add(cell2);

table.Rows.Add(row);
}

doc.FirstSection.Body.AppendChild(table);

doc.Save(“E:\tables.docx”, SaveFormat.Docx);
}

Node GetTable(Document doc)
{
Aspose.Words.Tables.Table table = new Aspose.Words.Tables.Table(doc);

Row row = new Row(doc);

Cell cell1 = new Cell(doc);
Cell cell2 = new Cell(doc);

Paragraph p = new Paragraph(doc);
Run r = new Run(doc, “first column”);
r.Font.Bold = true;
p.Runs.Add®;
cell1.AppendChild§;

row.Cells.Add(cell1);

p = new Paragraph(doc);
r = new Run(doc, “second column”);
r.Font.Bold = true;
p.Runs.Add®;
cell2.AppendChild§;
row.Cells.Add(cell2);

table.Rows.Add(row);

for (int i = 0; i < 10; i += 2)
{
row = new Row(doc);

if (i == 0) row.RowFormat.HeadingFormat = true;

cell1 = new Cell(doc);
cell2 = new Cell(doc);

p = new Paragraph(doc);
p.Runs.Add(new Run(doc, string.Format(“Row: {0}, Column 0”, i)));
cell1.AppendChild§;
row.Cells.Add(cell1);

p = new Paragraph(doc);
p.Runs.Add(new Run(doc, string.Format(“Row: {0}, Column 1”, i)));
cell2.AppendChild§;
row.Cells.Add(cell2);

table.Rows.Add(row);
}

return table;
}

Edit: I have found the solution with below code:

foreach (Row row in table)
row.RowFormat.AllowBreakAcrossPages = false;

But still I have one more issue, If the child table has more content than one page data, content is breaking within the page and it is not continuing on to other page. (see tables_Edit.docx)

Hi Srinu,


Thanks for your inquiry. Please see the following addition to your code to fix this issue:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

Aspose.Words.Tables.Table table = new Aspose.Words.Tables.Table(doc);

for (int i = 0; i < 50; i += 2)

{

Row row = new Row(doc);

row.RowFormat.AllowBreakAcrossPages = false;

Cell cell1 = new Cell(doc);

Cell cell2 = new Cell(doc);

cell1.AppendChild(GetTable(doc));

row.Cells.Add(cell1);

cell2.AppendChild(GetTable(doc));

row.Cells.Add(cell2);

table.Rows.Add(row);

}

doc.FirstSection.Body.AppendChild(table);

...


I hope, this helps.

Best regards,

Hi,

Thanks for your response. I have already tried it, working well and the same was mentioned in my previous edited post. After using this, I was facing with another issue where if the child table is having more content than the page height, then excess content is not visible. You can observe the same in my attached files. Here is the updated code:

protected void Page_Load(object sender, EventArgs e)
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Aspose.Words.Tables.Table table = new Aspose.Words.Tables.Table(doc);

for (int i = 0; i < 50; i += 2)
{
Row row = new Row(doc);
row.RowFormat.AllowBreakAcrossPages = false;

Cell cell1 = new Cell(doc);
Cell cell2 = new Cell(doc);

cell1.AppendChild(GetTable(doc));
row.Cells.Add(cell1);

cell2.AppendChild(GetTable(doc));
row.Cells.Add(cell2);

table.Rows.Add(row);
}

doc.FirstSection.Body.AppendChild(table);

doc.Save(“E:\tables.docx”, SaveFormat.Docx);
}

Node GetTable(Document doc)
{
Aspose.Words.Tables.Table table = new Aspose.Words.Tables.Table(doc);

Row row = new Row(doc);

Cell cell1 = new Cell(doc);
Cell cell2 = new Cell(doc);

Paragraph p = new Paragraph(doc);
Run r = new Run(doc, “first column”);
r.Font.Bold = true;
p.Runs.Add®;
cell1.AppendChild§;

row.Cells.Add(cell1);

p = new Paragraph(doc);
r = new Run(doc, “second column”);
r.Font.Bold = true;
p.Runs.Add®;
cell2.AppendChild§;
row.Cells.Add(cell2);

table.Rows.Add(row);

for (int i = 0; i < 70; i += 2)
{
row = new Row(doc);

cell1 = new Cell(doc);
cell2 = new Cell(doc);

p = new Paragraph(doc);
p.Runs.Add(new Run(doc, string.Format(“Row: {0}, Column 0”, i)));
cell1.AppendChild§;
row.Cells.Add(cell1);

p = new Paragraph(doc);
p.Runs.Add(new Run(doc, string.Format(“Row: {0}, Column 1”, i)));
cell2.AppendChild§;
row.Cells.Add(cell2);

table.Rows.Add(row);
}

return table;
}

Hi Srinu,


Thanks for your inquiry. You can fix this issue by increasing the page height as follows:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.PageSetup.PageHeight = 72 * 16;

Aspose.Words.Tables.Table table = new Aspose.Words.Tables.Table(doc);


I hope, this helps.

Best regards,

Hi, Thanks for your response. That works great. But we have a concern on changing the page height as we are using A4 paper size. Is there any way we can wrap the excess content on next page?

Basically what I need is,

  • If any table is coming in two pages, the same table should start on next page.
  • If the table content is more than page height, then the excess content to be displayed on next page instead of increasing page height.

Thanks you for your effort on finding the solution for my problem…

Hi Srinu,


Thanks for your inquiry. Well, if you can meet these requirements using MS Word 2013, then you should be able to achieve the same using Aspose.Words. Please create your expected Word document covering these two scenarios using MS Word and attach it here for our reference. Also, please list steps that you used to generate this expected document using MS Word. We will investigate the issue on our end and provide you more information.

Best regards,