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:
https://docs.aspose.com/words/net/working-with-columns-and-rows/

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("<table width=100% border=0 cellpadding=0 cellspacing=0>");
finalhtml.Append("<tr>");
finalhtml.Append("<td>line 1</td>");
finalhtml.Append("</tr>");
finalhtml.Append("</table>");

DocumentBuilder builder = new DocumentBuilder(mDoc);
builder.InsertHtml(finalhtml.ToString());

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