How Insert page break?

Hi ,

I am using aspose.words evaluation version 13.1 for .NET

I am inserting <HTML> by using builder.inserthtml("")

if my HTML is like

<table>1</table>
<table>2</table>
<table>3</table>

if I want to insert pagebrak between tables 1 2 3 so that each table should be on new page

how to inset page break ?

please help me …

Regards ,
Jeevan Joshi.

Hi Jeevan,

Thanks for your inquiry. Please use the following code snippet to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.doc");

DocumentBuilder builder = new DocumentBuilder(doc); 
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    // Create new paragraph and insert it after table
    Paragraph par = new Paragraph(doc);
    table.ParentNode.InsertAfter(par, table);
    // move documentBuilder cursor to the paragraph
    builder.MoveTo(par);
    // Insert page break
    builder.InsertBreak(BreakType.PageBreak);
}
doc.Save(MyDir + "out.docx");

Hi Tahir ,

It seems ok ,

but most of the time html will not contain only tables and their are many nested tables in Main table, and their are N number of Main tables . so how to insert page break after Main table ?

builder.inserthtml("<HTml>
    <body>
    <tableMain1>
    	<tr><td><tableChild></tableChild></td></tr>
    <tableMain1>
    <tableMain2>
    	<tr><td><tableChild></tableChild></td></tr>
    <tableMain2>
    <tableMain3>
    	<tr><td><tableChild></tableChild></td></tr>
    <tableMain3>
    </body>
    </HTml>");

in this case how to insert pagebreak after tableMain1, tableMain2, tableMain3

Regards ,
Jeevan Joshi.

Hi Jeevan,

Thanks for your inquiry. In this case, please use the Node.GetAncestor method to check either table has any outer table or not. The method gets the first ancestor of the specified NodeType.

Please use the following code snippet to achieve your requirements.

Document doc = new Document(MyDir + "table.html");

DocumentBuilder builder = new DocumentBuilder(doc);
foreach(Table table in doc.GetChildNodes(NodeType.Table, true))
{
    Node parentNode = table.GetAncestor(NodeType.Table);
    if (parentNode == null)
    {
        // Create new paragraph and insert it after table
        Paragraph par = new Paragraph(doc);
        table.ParentNode.InsertAfter(par, table);
        // move documentBuilder cursor to the paragraph
        builder.MoveTo(par);
        // Insert page break
        builder.InsertBreak(BreakType.PageBreak);
    }
}
doc.Save(MyDir + "out.docx");