Cell width and text wrap ignored for long text in a cell

Hello,

When creating a table using DocumentBuilder, I explicitly set the width of each cell and enable text wrap. However, when the text placed in the cell exceeds that width and has no word breaks, the cell expands to fit the text. Below is the code that can be used to reproduce the issue:

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception
{
    // Create a new document to write the table to.
    com.aspose.words.Document doc = new com.aspose.words.Document();

    // Create the document builder we will use.
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Start the table.
    builder.startTable();

    // Insert the first cell.
    builder.insertCell();
    builder.getCellFormat().setWrapText(true);
    builder.getCellFormat().setWidth(ConvertUtil.inchToPoint(2.0));
    builder.write("THIS_SHOULD_WRAP_AT_SOME_POINT_AND_NOT_EXPAND_THE_CELL");

    builder.insertCell();
    builder.getCellFormat().setWrapText(false);
    builder.getCellFormat().setWidth(ConvertUtil.inchToPoint(5.5));
    builder.write("This text should not need to wrap.");

    builder.endRow();
    builder.endTable();

    File file = new File("C:\Reproduced.doc");
    doc.save(file.getAbsolutePath());
}

When I open the resulting document, The first cell is wide enough to fit all of the text (which does not wrap). The table goes off the edge of the page. I am able to shrink the size of the first cell, and the text wraps, which makes me think there should be some way to do it in Java as well.

I’ve tried setting the cell format after and before all of the text insertion, but the cell width changes are not reflected in the final document.

Any help would be appreciated.

Thanks,

awp2513

CellFormat.setFitText can be used to force the text to wrap instead of expanding the cell. However, it appears the cell’s size must be explicitly set in order for it to work.

Hi
Thanks for your request. You should just reset AllowAutoFit option. Please see the following modified code:

// Create a new document to write the table to.
com.aspose.words.Document doc = new com.aspose.words.Document();
// Create the document builder we will use.
DocumentBuilder builder = new DocumentBuilder(doc);
// Start the table.
builder.startTable();
builder.getRowFormat().setAllowAutoFit(false);
// Insert the first cell.
builder.insertCell();
builder.getCellFormat().setWrapText(true);
builder.getCellFormat().setWidth(ConvertUtil.inchToPoint(2.0));
builder.write("THIS_SHOULD_WRAP_AT_SOME_POINT_AND_NOT_EXPAND_THE_CELL");
builder.insertCell();
builder.getCellFormat().setWrapText(false);
builder.getCellFormat().setWidth(ConvertUtil.inchToPoint(5.5));
builder.write("This text should not need to wrap.");
builder.endRow();
builder.endTable();
doc.save("C:\\Temp\\out.doc");

Best regards,

Thanks! This is exactly what I was looking for!