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++)
{
    db.insertCell();
    db.write(myArray[i]);
    db.getCellFormat().setWidth(45);
}

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…
db.getRowFormat().setAllowAutoFit(false);

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);
db.getRowFormat().setAllowAutoFit(false);
db.moveToMergeField("media_forecast");
db.startTable();
db.getRowFormat().setAllowAutoFit(false);
db.insertCell();
db.getRowFormat().setAllowAutoFit(false);
db.write("Name of Media");
db.getCellFormat().setWidth(90);
db.insertCell();
db.write("Budget");
db.getCellFormat().setWidth(45);
db.insertCell();
db.write("Sales");
db.getCellFormat().setWidth(45);
db.insertCell();
db.write("FYP");
db.getCellFormat().setWidth(45);
db.insertCell();
db.write("AARP");
db.getCellFormat().setWidth(45);
db.endRow();
for(int iLoop=0;iLoop<vMediaForecast.size();iLoop++)
{
    Media_forecast mediaForecast = (Media_forecast)vMediaForecast.get(iLoop);    
    String[] mediaValue = {"","","",""};
    if(mediaForecast.getAttribute("media_value")!=null)
    {
        db.getRowFormat().setAllowAutoFit(false);
        db.insertCell();
        db.getRowFormat().setAllowAutoFit(false);
        db.write((String)mediaForecast.getAttribute("media_name"));
        db.getCellFormat().setWidth(90);      
        db.getRowFormat().setAllowAutoFit(false);                      
        String mediaTmp[] = mediaForecast.getAttribute("media_value").toString().split(";");
        for(int iLoop2=0;iLoop2<mediaTmp.length;iLoop2++)
        {
                    mediaValue[iLoop2]=mediaTmp[iLoop2];
                    db.insertCell();
                    db.write(mediaValue[iLoop2]);
                db.getCellFormat().setWidth(45);
        }
            db.endRow();
    }
}
db.endTable();

attach is part of my large document… the codes will look for a mailmerge field in MS Word, then build a table there…

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:
https://demos.aspose.com/
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

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
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.

Hi,

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.

or

  1. 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.