Table cell wrapping

Hi,

I’m having some troubles with table cell wrapping when the value is too long and has no spaces inside, the table is expanded instead. Please check the attached example.
Is there anyway to make it work?

Thanks in advance for any help.
Mariusz

Hi Mariusz,

Thanks for your inquiry. Please find attached an output document i.e. produced on my side with Aspose.Words for Java 14.4.0 using your code snippet; so, I suggest you please upgrade to the latest version from the following link:
https://releases.aspose.com/words/java

In case the problem still remains, please attach your input document (Test Document.docx) here for testing. We will investigate the issue on our end and provide you more information.

Best regards,

Hi,

OK, one issue is fixed indeed! But there is another one.
The first two cells are supposed to be 20% each, but the attribute column has expanded much bigger.
Is it a bug or is it a way to make it work correctly?

Thank you,
Mariusz

BTW, the original issue still persists in 14.4.1 although it is fixed in 14.4.0

Hi Mariusz,

Thanks for your inquiry. Please see the following changes to achieve what you’re looking for.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Calibri");
builder.getFont().setSize(12);
for (int i = 1; i <= 3; i++)
{
    builder.writeln();
    builder.getCellFormat().clearFormatting();
    Table tbl = builder.startTable();
    builder.insertCell();
    //builder.getCellFormat 1().setPreferredWidth(PreferredWidth.fromPercent(100));
    builder.getCellFormat().getShading().setBackgroundPatternColor(Color.LIGHT_GRAY);
    builder.write("View Name: " + i);
    builder.endRow();
    builder.endTable();
    builder.getCellFormat().clearFormatting();
    builder.startTable();
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(25));
    builder.write("Name");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(75));
    builder.write("1");
    builder.endRow();
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(25));
    builder.write("Type");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(75));
    builder.write("2");
    builder.endRow();
    builder.endTable();
    builder.getCellFormat().clearFormatting();
    builder.startTable();
    builder.insertCell();
    builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(240, 240, 240));
    builder.write("Section: Columns");
    builder.endRow();
    builder.endTable();
    builder.getCellFormat().clearFormatting();
    builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(240, 240, 240));
    builder.startTable();
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));
    builder.write("Attribute");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));
    builder.write("Label");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(10));
    builder.write("Align");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(10));
    builder.write("Width");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(25));
    builder.write("Format");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(15));
    builder.write("Visible");
    builder.endRow();
    builder.getCellFormat().getShading().clearFormatting();
    for (int j = 1;
    j <= 5;
    j++)
    {
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));
        builder.write("abcdefghijklmnopqrstuvwxyz: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));
        builder.write("Column: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(10));
        builder.write("Column: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(10));
        builder.write("Column: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(25));
        builder.write("Column: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(15));
        builder.write("Column: " + j);
        builder.endRow();
    }
    builder.endTable();
    tbl.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);
}

I hope, this helps.

Secondly, regarding the usage of ‘interim build’, the hotfix (14.4.1) released during the month was only meant for a particular few customers who required it. We strongly recommend you use the official 14.4.0 release until the next official release at the end of the month. We apologise for any inconvenience.

Best regards,

Hi,
Its still not right - the attribute and label cells are both supposed to be 20%, but they don’t come out the same size. Also the first cell doesn’t span the width of the table now.
Thanks,
Mariusz

Hi Mariusz,

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:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Calibri");
builder.getFont().setSize(12);
for (int i = 1; i <= 3; i++) { builder.writeln();
    builder.getCellFormat().clearFormatting();
    builder.startTable();
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100));
    builder.getCellFormat().getShading().setBackgroundPatternColor(Color.LIGHT_GRAY);
    builder.write("View Name: " + i);
    builder.endRow();
    builder.endTable();
    builder.getCellFormat().clearFormatting();
    builder.startTable();
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(25));
    builder.write("Name");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(75));
    builder.write("1");
    builder.endRow();
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(25));
    builder.write("Type");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(75));
    builder.write("2");
    builder.endRow();
    builder.endTable();
    builder.getCellFormat().clearFormatting();
    builder.startTable();
    builder.insertCell();
    builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(240, 240, 240));
    builder.write("Section: Columns");
    builder.endRow();
    builder.endTable();
    builder.getCellFormat().clearFormatting();
    builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(240, 240, 240));
    builder.getCurrentParagraph().getParagraphFormat().setSpaceAfter(0);
    builder.getCurrentParagraph().getParagraphFormat().setSpaceBefore(0);
    builder.getCurrentParagraph().getParagraphBreakFont().setSize(1);
    builder.insertParagraph();
    builder.getParagraphFormat().clearFormatting();
    Table tbl = builder.startTable();
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));
    builder.write("Attribute");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));
    builder.write("Label");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(10));
    builder.write("Align");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(10));
    builder.write("Width");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(25));
    builder.write("Format");
    builder.insertCell();
    builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(15));
    builder.write("Visible");
    builder.endRow();
    builder.getCellFormat().getShading().clearFormatting();
    for (int j = 1; j <= 5; j++)
    { 
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));
        builder.write("abcdefghijklmnopqrstuvwxyz: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20));
        builder.write("Column: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(10));
        builder.write("Column: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(10));
        builder.write("Column: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(25));
        builder.write("Column: " + j);
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(15));
        builder.write("Column: " + j);
        builder.endRow();
    } 
    builder.endTable();
    tbl.setAllowAutoFit(false);
}
doc.save("C:\temp\out.docx");

I hope, this helps.

Best regards,

Thank you for your help. In the end we managed to do this with fixed widths - not preferred width.

Regards,
Mariusz

Hi Mariusz,

Its great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,