Free Support Forum - aspose.com

Setting KeepWithNext on a row

I'm currently building a table up using rows and cells, which is different to the way ive done it in the past of simply calling builder.InsertCell(). I need to do it this way as after it is build I need to reprocess the table changing merges and borders.

The problem i now have is the ParagraphFormat.KeepWithNext property no longer works. Where as before I would simply call builder.ParagraphFormat.KeepWithNext = true every time I called InertCell(), as I'm now building the table object this option doesn't work. Is there a similar way for me to do this for an individual Aspose.Words.Row, my code example is below and any help would be grateful:

Aspose.Words.Row row = new Aspose.Words.Row(doc);

//is there something like:

row.RowFormat.KeepWithNext = true;

Hi

Thank you for your interest in Aspose products. I think that you can try using AllowBreakAcrossPages property. See the following link for more information.

http://www.aspose.com/Products/Aspose.Words/Api/Aspose.Words.RowFormat.AllowBreakAcrossPages.html

I hope that this will help you. Please let me know if you would like to know something else.

Best regards.

Hi,

Yes that helps me to stop the text in each row from splitting onto a new page, I'm also looking to keep the whole of the table together. The way I do it manually when the document is open in Word is to select every row apart from the last one, then select:

Format -> Paragraph -> Line and Page Breaks -> Keep with next.

This then keeps all the rows within that table together and if moves the table to a new page if nexessary. Is there a way this can be done in Aspose?

Thanks

Hi

Thanks for your inquiry. I think that you can use ParagraphFormat.KeepWithNext property. For example see the following code.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.ParagraphFormat.KeepWithNext = true;

builder.RowFormat.AllowBreakAcrossPages = false;

builder.CellFormat.Width = 100;

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

{

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

{

builder.InsertCell();

}

builder.EndRow();

}

builder.EndTable();

doc.Save(@"378_103895_chrishop6\out.doc");

I hope that this will help you.

Best regards.

Hi,

Yes that would work but I'm creating a table using the Aspose.Word.Table object, and then populating this table with rows and cells. so for example:

Aspose.Words.Table table = builder.StartTable();

Aspose.Words.Row row = new Aspose.Words.Row(doc);

row.RowFormat.AllowAutoFit = true;

//this doesn't work

builder.ParagraphFormat.KeepWithNext = true;

row.RowFormat.AllowBreakAcrossPages = false;

Aspose.Words.Cell cell = new Aspose.Words.Cell(doc);

Aspose.Words.Paragraph p1 = new Aspose.Words.Paragraph(doc);

p1.ParagraphFormat.Alignment = paragraphAlignment;

Run run = new Run(doc, cellText);

p1.AppendChild(run);

cell.AppendChild(p1);

row.AppendChild(cell);

table.Rows.Add(row);

builder.EndTable();

Hi

Thanks for your inquiry. This will work if you create paragraphs using DocumentBuilder class. I modified your code. See the following.

builder.InsertParagraph();

Aspose.Words.Table table = builder.StartTable();

Aspose.Words.Row row = new Aspose.Words.Row(doc);

row.RowFormat.AllowAutoFit = true;

row.RowFormat.AllowBreakAcrossPages = false;

Aspose.Words.Cell cell = new Aspose.Words.Cell(doc);

cell.CellFormat.Width = 100;

Aspose.Words.Paragraph p1 = new Aspose.Words.Paragraph(doc);

p1.ParagraphFormat.Alignment = ParagraphAlignment.Center;

p1.ParagraphFormat.KeepWithNext = true;

Run run = new Run(doc, "cellText");

p1.AppendChild(run);

cell.AppendChild(p1);

row.Cells.Add(cell);

table.Rows.Add(row);

builder.EndTable();

I hope that this will help you.

Best regards.

That worked, thanks a lot for all your help with this issue