Free Support Forum - aspose.com

Unable to Wrap Text in a Table Cell

Unable to wrap text or fit all text in a Table Cell using DocumentBuilder of Aspose.Words in Java.
If the text of a cell does not have any spaces or line breaks, the text stretches all the way to the right and makes the table overflow to right and outside of the page limit.
I have tried setting wrapping property of table

com.aspose.words.Table table = builder.startTable();
table.setTextWrapping(com.aspose.words.TextWrapping.AROUND);

And also tried setting wrap text and fit text properties of table cells

builder.getCellFormat().setWrapText(true);
builder.getCellFormat().setFitText(true);

But none of the solutions worked. Screenshots attached.

Please guide, thanks!

OverflowText.png (26.4 KB)

NormalTextWithSpacesLineBreaks.png (44.5 KB)

@mussab,

We are unable to observe this problem with latest 20.9 version of Aspose.Words for Java. Please see these sample input/output Word documents (Cell Text Wrapping Test Documents.zip (18.6 KB)) and try running the following code:

Document doc = new Document("C:\\temp\\wrapping test.docx");

Table table = doc.getFirstSection().getBody().getTables().get(0);
Row cloned = (Row) table.getFirstRow().deepClone(false);
cloned.ensureMinimum();

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(cloned.getFirstCell().getFirstParagraph());
builder.write("veryLongTextWithoutSpaces");

table.getRows().add(cloned);

doc.save("C:\\temp\\20.10.docx");  

Can you please also ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document manually by using MS Word.
  • Please also create a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your particular scenario/issue and provide you more information.

Hi @awais.hafeez ,

I’m using the latest version 20.9 of Aspose.Words for Java.

There is no sample word file. I am using Aspose.Words document builder to create a file that has a table in it and save it in PDF.

Sharing the complete Java code below and also attaching the PDF being obtained from it:

    String filename = "Table_"+UUID.randomUUID().toString();
    com.aspose.words.Document doc = null;
    try {
    	doc = new com.aspose.words.Document();
    } catch (Exception e1) {
    	// TODO Auto-generated catch block
    	e1.printStackTrace();
    }
    com.aspose.words.DocumentBuilder builder = new com.aspose.words.DocumentBuilder(doc);

    //Page setup
    builder.getPageSetup().setPaperSize(com.aspose.words.PaperSize.A4);
    builder.getPageSetup().setTopMargin(com.aspose.words.ConvertUtil.inchToPoint(0.5));
    builder.getPageSetup().setBottomMargin(com.aspose.words.ConvertUtil.inchToPoint(0.5));
    builder.getPageSetup().setLeftMargin(com.aspose.words.ConvertUtil.inchToPoint(0.5));
    builder.getPageSetup().setRightMargin(com.aspose.words.ConvertUtil.inchToPoint(0.5));
    // Create Table
    // Start building a table
    com.aspose.words.Table table = builder.startTable();

    int i = 0;

    for(int j=0; j<10; j++) {
    			   
    	builder.insertCell();
    	builder.getFont().setBold(true);
    	builder.getCellFormat().getShading().setBackgroundPatternColor(java.awt.Color.LIGHT_GRAY);
    	builder.getCellFormat().setWrapText(true);
    	builder.getCellFormat().setFitText(true);
    	builder.write("Field Name veryveryveryverylongnamewithoutspaces");
    	builder.getFont().clearFormatting();
    	
    	builder.insertCell();
    	builder.getCellFormat().clearFormatting();
    	builder.getCellFormat().setWrapText(true);
    	builder.getCellFormat().setFitText(true);

builder.write("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    	
    	i = i+1;
    	
    	if(i%2 == 0)
    		builder.endRow();
    }

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

    // Set Font Family
    for (com.aspose.words.Run run : (java.lang.Iterable<com.aspose.words.Run>) doc.getChildNodes(com.aspose.words.NodeType.RUN, true)) {
    	run.getFont().setName("Arial");
    }

    // Save doc
    try {
    	doc.save(filename+".pdf", com.aspose.words.SaveFormat.PDF);
    } catch (Exception e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }

Thanks.
Table_24a0ad00-561a-490c-9067-d8dc22a0e418.pdf (27.8 KB)

@mussab,

In your case, please just call Document.updateTableLayout method before saving to PDF format to fix this issue:

Document doc = new Document();
com.aspose.words.DocumentBuilder builder = new com.aspose.words.DocumentBuilder(doc);

//Page setup
builder.getPageSetup().setPaperSize(com.aspose.words.PaperSize.A4);
builder.getPageSetup().setTopMargin(com.aspose.words.ConvertUtil.inchToPoint(0.5));
builder.getPageSetup().setBottomMargin(com.aspose.words.ConvertUtil.inchToPoint(0.5));
builder.getPageSetup().setLeftMargin(com.aspose.words.ConvertUtil.inchToPoint(0.5));
builder.getPageSetup().setRightMargin(com.aspose.words.ConvertUtil.inchToPoint(0.5));
// Create Table
// Start building a table
com.aspose.words.Table table = builder.startTable();

int i = 0;
for (int j = 0; j < 5; j++) {
    builder.insertCell();
    builder.getFont().setBold(true);
    builder.getCellFormat().getShading().setBackgroundPatternColor(java.awt.Color.LIGHT_GRAY);
    builder.getCellFormat().setWrapText(true);
    builder.getCellFormat().setFitText(true);
    builder.write("Field Name veryveryveryverylongnamewithoutspaces");
    builder.getFont().clearFormatting();

    builder.insertCell();
    builder.getCellFormat().clearFormatting();
    builder.getCellFormat().setWrapText(true);
    builder.getCellFormat().setFitText(true);
    builder.write("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

    i = i + 1;
    builder.endRow();
}

builder.endTable();

// Set Font Family
for (com.aspose.words.Run run : (java.lang.Iterable<com.aspose.words.Run>) doc.getChildNodes(com.aspose.words.NodeType.RUN, true)) {
    run.getFont().setName("Arial");
}

doc.updateTableLayout();
doc.save("C:\\temp\\20.10-updateTableLayout.pdf");

Thanks @awais.hafeez, Document.updateTableLayout helped correcting the output.