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

Free Support Forum - aspose.com

Table width in Aspose.Word (Java)

I tried setting each individual cell’s width by using table.getCellFormat().setWidth(45) but it is not working. My table still stretches across the full width of the document. As this is a nested table inside a large table, my width of the larger (outside) table gets enlarged as well.

Any solution to this? Sample code as follows:

// db is document builder object
for(int i=0; i<myArray.length; i++)

Please make sure that AllowAutoFit property of RowFormat of DocumentBuilder is set to false while inserting cells.

Hi, i’ve just tried but it doesnt work.

// db is my DocumentBuilder object…

I have this line immediately after creating the document builder object, no luck. Any other suggestion?

Ok, then please send me a small test project, or code snippet and document templates, so that I could reproduce the problem on my side.

                DocumentBuilder db = new DocumentBuilder(docOriginal);<br>                    db.getRowFormat().setAllowAutoFit(false);<br>                    db.moveToMergeField("media_forecast");<br>                    db.startTable();<br>                    db.getRowFormat().setAllowAutoFit(false);<br>                    db.insertCell();<br>                    db.getRowFormat().setAllowAutoFit(false);<br>                    db.write("Name of Media");<br>                    db.getCellFormat().setWidth(90);<br>                    db.insertCell();<br>                    db.write("Budget");<br>                    db.getCellFormat().setWidth(45);<br>                    db.insertCell();<br>                    db.write("Sales");<br>                    db.getCellFormat().setWidth(45);<br>                    db.insertCell();<br>                    db.write("FYP");<br>                    db.getCellFormat().setWidth(45);<br>                    db.insertCell();<br>                    db.write("AARP");<br>                    db.getCellFormat().setWidth(45);<br>                    db.endRow();<br>                    for(int iLoop=0;iLoop<vMediaForecast.size();iLoop++)<br>                    {<br>                        Media_forecast mediaForecast = (Media_forecast)vMediaForecast.get(iLoop);    <br>                        String[] mediaValue = {"","","",""};<br>                        if(mediaForecast.getAttribute("media_value")!=null)<br>                        {<br>                            db.getRowFormat().setAllowAutoFit(false);<br>                            db.insertCell();<br>                            db.getRowFormat().setAllowAutoFit(false);<br>                            db.write((String)mediaForecast.getAttribute("media_name"));<br>                            db.getCellFormat().setWidth(90);      <br>                            db.getRowFormat().setAllowAutoFit(false);                      <br>                            String mediaTmp[] = mediaForecast.getAttribute("media_value").toString().split(";");<br>                            for(int iLoop2=0;iLoop2<mediaTmp.length;iLoop2++)<br>                            {<br>                                     mediaValue[iLoop2]=mediaTmp[iLoop2];<br>                                     db.insertCell();<br>                                     db.write(mediaValue[iLoop2]);<br>                                    db.getCellFormat().setWidth(45);<br>                            }<br>                             db.endRow();<br>                        }<br>                    }<br>                    db.endTable();<br><br>attach is part of my large document... the codes will look for a mailmerge field in MS Word, then build a table there..<br>

Try a simpler example first. One table in an empty document. Use code from the demo project (included in the download) that is called DocumentBuilderDemo.java.

Creating table with cells of a specified width certainly works there. Here is the demo online:


Select "Document Builder" and click Generate. Scroll down and you will see a chess-like table with cells of a certain width.

We have reproduced your problem in both .NET and Java version using your document and code. I have logged this problem to our defect base as issue #3562. We will try to fix it as soon as possible, but it can still can take a sufficient amount of time as we will fix in .NET version first and then propagate changes to Java version.

Best regards,

Ok thanks. I guess the table width doesnt work with inner table… Do keep us updated :slight_smile:

Dear sir, madam,
Is there any news on the progress of this issue? I seem to be having the same problem, setting the cellwidth’s individually still gives me a table with a predefined width (about the size of a standard paragraph). Any idea if I’m doing something wrong or if it’s a problem in Aspose?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Could you please provide me code example that will allow me to reproduce your problem. Also tell me which version of Aspose.Words you are using. I will investigate the issue and provide you more information.

Best regards.


This problem is fixed in the upcoming Aspose.Words 10.5 release.

The problem was due to the “cursor-like” behavior of DocumentBuilder. This behavior is good for run and paragraph formatting, but obviously not very good with row and cell formatting. When you moved the insertion cursor into an existing table, your DocumentBuilder.RowFormat and CellFormat properties took on formatting available at that point. Then creating a new nested table at that point applied all the parent’s table formatting to it, which made it appear ugly.

So we changed the behavior so it will appear more “natural” to the user, but this will be available at end of September.

In the meantime if you need to proceed you can workaround in two ways:
1. Call DocumentBuilder.RowFormat.ClearFormatting() and DocumentBuilder.CellFormat.ClearFormatting() after you start a nested table.


2. Create table, row and cell using nodes without using DocumentBuilder.

But I think it is best to wait for Aspose.Words 10.5 that makes working with tables easier.

The issues you have found earlier (filed as WORDSNET-1339) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.