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

Free Support Forum - aspose.com

Keep a html table together

I'm using html to create my reports. At the end of the generation, I use the insertHtml-function to create the report.
My htmlcode contains a few tables. Assume i have 3 tables in my report and I want to keep each table together. So I want to avoid that one of the tables is splitted over 2 pages.

I'm aware of the property ParagraphFormat.KeepTogether = true;

But I can't see how I do this when generating my report with html. Could someone provide me with an example of keeping a html-table together ?

Hi Bart,


Thanks for your inquiry.

You can restrict content inside the Cells of a Row from being split across a page. In Microsoft Word this can be found under Table Properties as the option ā€˜Allow Row to break across Pagesā€™. In Aspose.Words this is found under the RowFormat object of a Row as the property RowFormat.AllowBreakAcrossPages. Please read the following article for more details:
http://www.aspose.com/docs/display/wordsnet/Keeping+Tables+and+Rows+from+Breaking+across+Pages

I hope, this helps.

Best regards,

Hi,
Thank you for the info. I am aware of this functionality, but in my case, Iā€™m using html. Something like this. So assume that this table exists at the end of a page. How can i prevent that is being splitted accross the pages ?

            finalhtml.Append("");
finalhtml.Append("");
finalhtml.Append(ā€œā€);
finalhtml.Append("");
finalhtml.Append("
line 1
"
);
        <span style="color:#2b91af;">DocumentBuilder</span> builder = <span style="color:blue;">new</span> <span style="color:#2b91af;">DocumentBuilder</span>(mDoc);
        builder.InsertHtml(finalhtml.ToString());<br><br> </pre><br>

Hi Bart,


Thanks for the additional information. I think, you need to implement INodeChangingCallback interface if you want to change values of the RowFormat object of a Row i.e. being inserted via InsertHtml method. Please try run the following code snippet:

Document doc = new
Document();

DocumentBuilder builder = new DocumentBuilder(doc);

HandleNodeChanging handler = new HandleNodeChanging();

doc.NodeChangingCallback = handler;

builder.InsertHtml(finalhtml.ToString());

foreach (Table table in handler.InsertedTables)

foreach (Row row in table.Rows)

row.RowFormat.AllowBreakAcrossPages = false;

doc.Save(@"C:\Temp\out.docx");


public class HandleNodeChanging : INodeChangingCallback

{

void INodeChangingCallback.NodeInserted(NodeChangingArgs args)

{

if (args.Node.NodeType == NodeType.Table)

mInsertedTables.Add(args.Node);

}

void INodeChangingCallback.NodeInserting(NodeChangingArgs args)

{

// Do Nothing

}

void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)

{

// Do Nothing

}

void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)

{

// Do Nothing

}

public List<Node> InsertedTables

{

get { return mInsertedTables; }

}

private readonly List<Node> mInsertedTables = new List<Node>();

}


I hope, this helps.

Best regards,
1 Like