Free Support Forum - aspose.com

table.KeepTogether ? (table with paragraph).KeepTogether?

hi Guys i got questions,

- i’m looking for a way to keep (small enough) tables on a single page.
- usually the tables have a paragraph above them (title), and i want to be sure that they are both on the same page.


ideally i’m looking for a kind of generic node so that i can append(as in group) other nodes and apply the KeepTogether property that ive only seen in paragraphs.

i was thinking about using the Section node but it doesn’t have that property.
anyone have any suggestions on how i can go about this?

greetings,

Simon

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

Thanks for your request. I think that you should use KeepWithNext property in this case. For example see the following code:

//Open document

Document doc = new Document(@"Test158\in.doc");

//Get collection of tables

NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);

//Loop though all tables

foreach (Table tab in tables)

{

Node previouseNode = tab.PreviousSibling;

if (previouseNode != null)

{

if (previouseNode.NodeType == NodeType.Paragraph)

{

(previouseNode as Paragraph).ParagraphFormat.KeepWithNext = true;

}

}

}

//Save output

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

Hope this helps.

Best regards.

hey alexey,

thanks for the response :smiley:

I tried to test your example by using 3 paragraphs to almost fill a page followed by one table with a couple of rows appended to it.

i tried setting the KeepWithNext property of the of the last paragraph (before the table) to true and also tried just leaving it false. Unfortunately the table was spanned across both pages in each case. The KeepWithNext property of paragraphs might not work on table nodes.

Simon

EDIT: i’ll give some code as an example in a few minutes

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

Thanks for your request. Could you please also attach your document for testing? I will try to help you.

Best regards.

//most of the code below is just the table


//new document

Document Document = new Document();

Document.RemoveAllChildren();

Section Section = new Section(Document);

Document.AppendChild(Section);

Body Body = new Body(Document);

Section.AppendChild(Body);

//create a Paragraph to almost fill up the page

Paragraph Paragraph1 = new Paragraph(Document);

Body.AppendChild(Paragraph1);

//add a big letter A to the Paragraph as a filler

Run Run1 = new Run(Document);

Run1.Font.Name = "Times New Roman";

Run1.Font.Size = 460;

Run1.Text = "A";

Paragraph1.AppendChild(Run1);

//title of the table at the bottom of the page

Paragraph Paragraph2 = new Paragraph(Document);

Paragraph2.ParagraphFormat.KeepWithNext = true; //KeepWithNext is set to true

Body.AppendChild(Paragraph2);

//text of the table title ***that should stay with the upcomming table***

Run Run2 = new Run(Document);

Run2.Font.Name = "Times New Roman";

Run2.Font.Size = 12;

Run2.Text = "This is a test title\v";

Paragraph2.AppendChild(Run2);

//the rest below is the table ***that shouldn't span and should stay with its title***

Aspose.Words.Tables.Table Table = new Aspose.Words.Tables.Table(Document);

Body.AppendChild(Table);

//first table row

Aspose.Words.Tables.Row Row1 = new Aspose.Words.Tables.Row(Document);

Row1.RowFormat.Height = 20;

Row1.RowFormat.Borders.Color = Color.Purple;

Row1.RowFormat.Borders.LineStyle = LineStyle.Single;

Table.AppendChild(Row1);

Aspose.Words.Tables.Cell Cell1 = new Aspose.Words.Tables.Cell(Document);

Cell1.CellFormat.Width = 100;

Row1.AppendChild(Cell1);

Paragraph Paragraph3 = new Paragraph(Document);

Cell1.AppendChild(Paragraph3);

Run Run3 = new Run(Document);

Run3.Text = "Please don't span me";

Paragraph3.AppendChild(Run3);

//second table row

Aspose.Words.Tables.Row Row2 = new Aspose.Words.Tables.Row(Document);

Row2.RowFormat.Height = 20;

Row2.RowFormat.Borders.Color = Color.Purple;

Row2.RowFormat.Borders.LineStyle = LineStyle.Single;

Table.AppendChild(Row2);

Aspose.Words.Tables.Cell Cell2 = new Aspose.Words.Tables.Cell(Document);

Cell2.CellFormat.Width = 100;

Row2.AppendChild(Cell2);

//third table row

Aspose.Words.Tables.Row Row3 = new Aspose.Words.Tables.Row(Document);

Row3.RowFormat.Height = 20;

Row3.RowFormat.Borders.Color = Color.Purple;

Row3.RowFormat.Borders.LineStyle = LineStyle.Single;

Table.AppendChild(Row3);

Aspose.Words.Tables.Cell Cell3 = new Aspose.Words.Tables.Cell(Document);

Cell3.CellFormat.Width = 100;

Row3.AppendChild(Cell3);

//fourth table row

Aspose.Words.Tables.Row Row4 = new Aspose.Words.Tables.Row(Document);

Row4.RowFormat.Height = 20;

Row4.RowFormat.Borders.Color = Color.Purple;

Row4.RowFormat.Borders.LineStyle = LineStyle.Single;

Table.AppendChild(Row4);

Aspose.Words.Tables.Cell Cell4 = new Aspose.Words.Tables.Cell(Document);

Cell4.CellFormat.Width = 100;

Row4.AppendChild(Cell4);

//fifth table row

Aspose.Words.Tables.Row Row5 = new Aspose.Words.Tables.Row(Document);

Row5.RowFormat.Height = 20;

Row5.RowFormat.Borders.Color = Color.Purple;

Row5.RowFormat.Borders.LineStyle = LineStyle.Single;

Table.AppendChild(Row5);

Aspose.Words.Tables.Cell Cell5 = new Aspose.Words.Tables.Cell(Document);

Cell5.CellFormat.Width = 100;

Row5.AppendChild(Cell5);

//sixth table row

Aspose.Words.Tables.Row Row6 = new Aspose.Words.Tables.Row(Document);

Row6.RowFormat.Height = 20;

Row6.RowFormat.Borders.Color = Color.Purple;

Row6.RowFormat.Borders.LineStyle = LineStyle.Single;

Table.AppendChild(Row6);

Aspose.Words.Tables.Cell Cell6 = new Aspose.Words.Tables.Cell(Document);

Cell6.CellFormat.Width = 100;

Row6.AppendChild(Cell6);

//save it

Document.Save(@"Test158\test - faultyDoc.doc");

hi alexey sorry its a bit long. didnt have time to loop the creation of the table rows. i attached the document i it creates.

thanks for the help man

Greetings,

Simon

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

Thanks for additional information. Maybe you should set KeepWithNext to true for each paragraph in the table. For example see the following code:

//new document

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//create a Paragraph to almost fill up the page

builder.Font.Name = "Times New Roman";

builder.Font.Size = 460;

builder.Writeln("A");

//Clear formating

builder.Font.ClearFormatting();

builder.CurrentParagraph.ParagraphBreakFont.ClearFormatting();

builder.ParagraphFormat.KeepWithNext = true;

//Insert title

builder.Writeln("This is a test title\v");

//Create table

builder.RowFormat.Height = 20;

builder.CellFormat.Width = 100;

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

{

builder.InsertCell();

builder.Write("Please don't span me");

builder.EndRow();

}

builder.EndTable();

//save it

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

Hope this helps.

Best regards.

thanks i get it. and i think its working :smiley: