Free Support Forum - aspose.com

Tables with no page breaks

Hello.

I need to use Aspose.Words to create some variable length text (coming from a database) and then a table having no page breaks in the middle of the table. Is there a way to calculate how much space is left on the page and calculate how much space my table will take so I will know if I have to force a page break?

Thanks.

Denise

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. MS Word document is flow document and does not contain any information about its layout into lines and pages. So there is no way to determine where page starts or ends and determine how much free space is available on the page.

However, to achieve what you need you do not need to know how much space is available on page and how much space a table will take. I think, it will be enough to use “Keep With Next” option of paragraphs within a table. In this case, if there is not enough space on page for a table, the whole table will go to the next page. For instance, see the attached document.

Best regards.

Thank you for your response.

Can you possibly show me how to do this programmatically?

I do not know if I should use the DocumentBuilder feature (such as builder.InsertCell()) or if I should use the Document, Section, Body, Paragraph, Run DOM features.

Can you please explain this?

Thank you.

Denise

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Here is a simple example:

// Create empty docuemnt and DocumentBuilder

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Insert few paragraphs at the beggining of the document

for (int i = 0; i < 20; i++)

builder.Writeln("This is paragraphs for testing");

// Enable "Keep with Next option"

builder.ParagraphFormat.KeepWithNext = true;

// Build some table

builder.CellFormat.Width = 200;

builder.CellFormat.Borders.LineStyle = LineStyle.Single;

builder.CellFormat.Borders.Color = Color.Black;

for (int i = 0; i < 30; i++)

{

for (int j = 0; j < 2; j++)

{

builder.InsertCell();

builder.Write("This is test content of the table");

}

builder.EndRow();

}

builder.EndTable();

// Save output document

doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards.

Thank you very much for your response. If the table needs to take more than one page, how can I repeat the header columns? I have tried this, following your example, but I can't get it working.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Insert few paragraphs at the beggining of the document

for (int i = 0; i < 20; i++)

builder.Writeln("This is paragraphs for testing");

// Enable "Keep with Next option"

builder.ParagraphFormat.KeepWithNext = true;

// Build some table

builder.CellFormat.Width = 200;

builder.CellFormat.Borders.LineStyle = LineStyle.Single;

builder.CellFormat.Borders.Color = Color.Black;

builder.InsertCell();

builder.Write("Header Col1");

builder.InsertCell();

builder.Write("Header Col2");

builder.EndRow();

for (int i = 0; i < 100; i++)

{

for (int j = 0; j < 2; j++)

{

builder.InsertCell();

builder.Write("This is test content of the table");

}

builder.EndRow();

}

if (builder.ParagraphFormat.PageBreakBefore == true)

{

builder.InsertCell();

builder.Write("Header Col1");

builder.InsertCell();

builder.Write("Header Col2");

builder.EndRow();

}

builder.EndTable();

Thanks.

Denise

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You should use HeadingFormat property. Please see the following link for more information,

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.tables.rowformat.headingformat.html

Also here is code example:

//Create document and DocumentBuilder

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//Create heading row that will be repeated on each page

builder.RowFormat.HeadingFormat = true;

builder.Bold = true;

for (int i = 0; i < 5; i++)

{

builder.InsertCell();

builder.Write(string.Format("heading_{0}", i));

}

builder.EndRow();

builder.RowFormat.HeadingFormat = false;

builder.Bold = false;

//Generate body of table

for (int rowIdx = 0; rowIdx < 100; rowIdx++)

{

for (int colIdx = 0; colIdx < 5; colIdx++)

{

builder.InsertCell();

builder.Write(string.Format("value_of_{0}_{1}", rowIdx, colIdx));

}

builder.EndRow();

}

builder.EndTable();

//Save output document

doc.Save("out.doc");

I hope this could help you.

Best regards.