How to Insert Page Break inside Table Cell during Mail Merge using Java

Hi,

I’m using mail merge to put html content in a table cell but both of the page break in html or the manual page break didn’t work in the cell.

Could you pls explain how it work? or the aspose does not support the page break in this way.

You can see the template doc and code in the attachment.Support test.zip (15.7 KB)

Thanks,
Shang

@windpine

Please note that you cannot insert page break inside a table. If you need to achieve this, you should split table into two separate tables and then insert page break between these tables.

@windpine

You can split the table and insert page break into document as shown below. Hope this helps you.

class HandleHtmlMergeField implements IFieldMergingCallback {
    /**
     * This handler is called for every mail merge field found in the document,
     * for every record found in the data source.
     */
    public void fieldMerging(FieldMergingArgs e) throws Exception {
        if (mBuilder == null)
            mBuilder = new DocumentBuilder(e.getDocument());

        // We want to insert html during mail merge.
        if ("RN_COMMENTS".equals(e.getFieldName())) {
        	
        	Row row = (Row)e.getField().getStart().getAncestor(NodeType.ROW);
        	if(row != null)
        	{
        		Table table = (Table)row.getParentTable().deepClone(true);
        		table.removeAllChildren();
        		table.getRows().add(row.deepClone(true));
        		
        		mBuilder.moveToMergeField(e.getFieldName());
                mBuilder.insertHtml((String) e.getFieldValue());
                mBuilder.moveTo(row.getParentTable().getNextSibling());
                System.out.println();
                mBuilder.writeln("");
                mBuilder.insertBreak(BreakType.PAGE_BREAK);
                
                mBuilder.getCurrentSection().getBody().insertAfter(table, mBuilder.getCurrentParagraph());
                
                mBuilder.moveTo(table.getFirstRow().getFirstCell().getFirstParagraph());
                mBuilder.insertHtml((String) e.getFieldValue());
        	}
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
        // Do nothing.
    }

    private DocumentBuilder mBuilder;
	
}