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

Free Support Forum - aspose.com

Cell setWidth() method issue

Hi support,

Please check the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startTable();
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.write("cel1");
builder.insertCell(); 
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.getCellFormat().setWidth(160);
builder.write("cel2"); 
builder.insertCell(); 
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.write("bbb1");
builder.endRow();
builder.endTable();
doc.save("C:\Temp\docCellWidth.doc");

Please note that the last 3rd cell has the same width as the second, even if the cell’s clearFormatting() method was used.

I’m using the last aspose.word for java release, from 19.10.2008.

Best regards,
Milan

Hi
Thanks for your inquiry. This is not a bug. When you set width of cell using builder.getCellFormat().setWidth(160); all cells you insert using builder.insertCell will have width=160.
You can try to set width of each cell in your table. Or set width of column after building table as shown in the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table tab = builder.startTable();
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.write("cel1");
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.write("cel2");
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.write("bbb1");
builder.endRow();
builder.endTable();
// Set width of the second column
for (int rowIndex = 0; rowIndex < tab.getRows().getCount(); rowIndex++)
{
     tab.getRows().get(rowIndex).getCells().get(1).getCellFormat().setWidth(160);
}
doc.save("C:\\Temp\\out.doc");

I highlighted my changes.
Best regards.

Hi Alexey,

Thank you for the reply. I have an observation :
If I save the 2nd cell in the 1st row : Cell cell2= builder.insertCell(), and write cell2.getCellFormat().setWidth(160), the output is not the same as the one resulted by running your version of code, even if this seem to handle same objects, but in different contexts.

Though, your solution solved my problem, but it could be easier if I could set the width on cell directly, not after finishing the table. Please confirm if this can be possible; if not, I’ll use your solution.

Best regards,
Milan

Hi
Thanks for your inquiry. As I told you earlier you can also set width of each cell in your table as shown in the following code:

builder.startTable();
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.getCellFormat().setWidth(50);
builder.write("cel1");
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.getCellFormat().setWidth(160);
builder.write("cel2");
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.getCellFormat().setWidth(50);
builder.write("bbb1");
builder.endRow();
builder.endTable();
doc.save("C:\\Temp\\out.doc");

But in this case you should know width of each cell in your table.
Best regards.

Thanks for pointing me to this thread, it certainly helped. However, now I have an element of right padding in each of my cells which causes something that should be one line to be four or five. I’ve tried setting the right padding on the cell and/or row to be zero, but it still causes an issue…
Thanks

Hi
Thanks for your inquiry. Could you please provide me your code and your document? I will try to reproduce the issue and provide you more information.
Best regards.