Free Support Forum - aspose.com

Create tables with non-aligned columns

Hi,


I’m trying to upgrade from Aspose.Words 15.7 to the latest version.

I have issues creating/modifying tables with non-align columns.

A simplified repro is the following :

I need to create a table with the following exact cell widths, cell merges and table preferred width:

Document d = new Document();

Table t = new Table(d);
d.getFirstSection().getBody().appendChild(t);

Row r = new Row(d);
t.appendChild®;

Cell c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(123.9));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(123.9));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(123.95));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(81.35));
r.appendChild©;
c.getCellFormat().setVerticalMerge(CellMerge.FIRST);

r = new Row(d);
t.appendChild®;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(123.9));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(123.9));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(123.95));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(81.35));
r.appendChild©;
c.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);

r = new Row(d);
t.appendChild®;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(123.9));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(123.9));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(123.95));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(81.35));
r.appendChild©;
c.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);

r = new Row(d);
t.appendChild®;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(107.75));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(264.0));
r.appendChild©;

c = new Cell(d);
c.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(81.35));
r.appendChild©;


t.setAllowAutoFit(true);
t.setPreferredWidth(PreferredWidth.fromPoints(300));

My issue is that I hand up with a odd looking table.

I didn’t use a document builder on purpose since my issues are more with processing existing tables.

How can I create a normal looking table with these exact dimensions?

Thank you very much.

Best regards,

David



Hi David,


Thanks for your inquiry. Please use setWidth() method instead of setPreferredWidth() to set Cell width. Please check following code snippet for the reference. It will help you to accomplish the task.

com.aspose.words.Document d = new
com.aspose.words.Document();<o:p></o:p>

com.aspose.words.Table t = new com.aspose.words.Table(d);

d.getFirstSection().getBody().appendChild(t);

com.aspose.words.Row r = new com.aspose.words.Row(d);

t.appendChild(r);

com.aspose.words.Cell c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(123.9);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(123.9);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(123.95);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(81.35);

r.appendChild(c);

c.getCellFormat().setVerticalMerge(CellMerge.FIRST);

r = new com.aspose.words.Row(d);

t.appendChild(r);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(123.9);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(123.9);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(123.95);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(81.35);

r.appendChild(c);

c.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);

r = new com.aspose.words.Row(d);

t.appendChild(r);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(123.9);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(123.9);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(123.95);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(81.35);

r.appendChild(c);

c.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);

r = new com.aspose.words.Row(d);

t.appendChild(r);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(107.75);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(264.0);

r.appendChild(c);

c = new com.aspose.words.Cell(d);

c.getCellFormat().setWidth(81.35);

r.appendChild(c);

t.setAllowAutoFit(true);

t.setPreferredWidth(PreferredWidth.fromPoints(300));

d.save("testtable.docx");


Best Regards,