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

Free Support Forum - aspose.com

Creating a Table after inserting an HTML Table adds the row to the existing table

We are creating a document using document builder. We are adding tables from HTML and also using builder.startTable();

PROBLEM: When we user builder.startTable AFTER inserting and HTML table the new table row is added to the previously inserted HTML table which throw off the formatting.

Here is a code sample;

			System.out.println("Read HTML File");
			java.io.File file = new java.io.File("C:\\temp\\table.html");
			System.out.println("Create File Stream");
			java.io.FileInputStream fis = new java.io.FileInputStream(file);
			StringBuilder inputhtml = new StringBuilder();
			writeInputStream(fis, inputhtml);
			
			
			com.aspose.words.Document doc = new com.aspose.words.Document();
			DocumentBuilder builder = new DocumentBuilder(doc);		// TODO Auto-generated method stub
			
			builder.insertHtml(inputhtml.toString());
			
			builder.startTable();
			builder.insertCell();
    		//Set margin and autofit

			builder.getCellFormat().setWidth(400);
			
    		builder.getRowFormat().setHeight(25);

    		builder.endRow();
    		
    		builder.endTable();

    		System.out.println("Save Word Document");
    		doc.save("C:\\temp\\aspose_Word_Table.docx", SaveFormat.DOCX);

The html looks like this.

'<p dir="ltr">&nbsp;</p>

'<table border="0" cellpadding="1" cellspacing="1" dir="ltr">
'	<tbody>
'		<tr>
'			<td style="width: 30px; vertical-align: top;">&nbsp;</td>
'			<td style="width: 30px; vertical-align: top;">&nbsp;</td>
'			<td style="vertical-align: top;">E=3columns</td>
'		</tr>
'	</tbody>
'</table>

The resulting Word document looks like this;

image.png (4.7 KB)

The CREATED table is part of the inserted HTML table. I can fix it by inserting a break, but then the break adds a new line to the output which throws the spacing off.

So is this a bug or am I not coding it correctly?

@paul.calhoun,

Thanks for your inquiry. The problem occurs because Microsoft Word merges two or more consecutive Tables into one big Table. To prevent Microsoft Word from doing this you could have inserted an empty Paragraph in between those Tables as follows:

...
...
DocumentBuilder builder = new DocumentBuilder(doc);		// TODO Auto-generated method stub
builder.insertHtml(inputhtml.toString());

builder.getCurrentParagraph().getParagraphFormat().setSpaceAfter(0);
builder.getCurrentParagraph().getParagraphFormat().setSpaceBefore(0);
builder.getCurrentParagraph().getParagraphBreakFont().setSize(1);
builder.insertParagraph();

builder.startTable();
builder.insertCell();
//Set margin and autofit

builder.getCellFormat().setWidth(400);
builder.getRowFormat().setHeight(25);
builder.endRow();
builder.endTable();
...
...