Hi,
Unable to wrap text or fit all text in a Table Cell using insertHtml(String html) of DocumentBuilder in Aspose.Words for 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.
The HTML file has the expected result but in my analysis, I think Aspose is not applying word-break: break-all
CSS style on table cells.
Attaching HTML file and Java code snippet below.
String recipientsTableHtml = "";
recipientsTableHtml+="<h4 style=\"text-align: left;\"> Recipients Record</h4>";
recipientsTableHtml+="<table style=\" height: 127px; width:800px; border: 1px ; border-spacing: 0px; font-size: 12px; \">";
recipientsTableHtml+="<tbody style=\"border: 1px ;\">";
recipientsTableHtml += "<tr style=\"border: 1px solid; height: 10px;text-align:center; \">";
recipientsTableHtml +="<th style=\"border: 1px solid; height: 10px;width : 25px;text-align:center; \">S.No</th>";
recipientsTableHtml +="<th style=\"border: 1px solid; height: 10px;width : 100px;text-align:center; \">Name</th>";
recipientsTableHtml +="<th style=\"border: 1px solid; height: 10px; width : 100px;text-align:center;\">Designation</th>";
recipientsTableHtml +="<th style=\"border: 1px solid; height: 10px; width : 100px;text-align:center;\">Mailing Address</th>";
recipientsTableHtml +="<th style=\"border: 1px solid; height: 10px;width : 100px; text-align:center;\">Category</th>";
recipientsTableHtml +="<th style=\"border: 1px solid; height: 10px;width : 100px; text-align:center;\">Contact</th>";
recipientsTableHtml +="</tr>";
for(int i = 0 ; i < 2; i++)
{
recipientsTableHtml+="<tr style=\"border: 1px solid; height: 10px;text-align:center;word-break: break-all;\">";
int index = i+1;
recipientsTableHtml+="<td style=\"border: 1px solid; height: 10px;text-align:center;word-break: break-all;\">"+ index +"</td>";
recipientsTableHtml+="<td style=\"border: 1px solid; height: 10px;text-align:center;word-break: break-all;\">ddddddddddddddddddddddddddddddddddddddddddddddddddddssssssssddddddddddddddddddddddddddddddddd </td>";
recipientsTableHtml+="<td style=\"border: 1px solid; height: 10px;text-align:center;word-break: break-all;\">sdasdadasdd</td>";
recipientsTableHtml+="<td style=\"border: 1px solid; height: 10px;text-align:center;word-break: break-all;\">ddddddddddddddddddddddddddddddddddddddddddddddddddddssssssssddddddddddddddddddddddddddddddddd </td>";
recipientsTableHtml+="<td style=\"border: 1px solid; height: 10px;text-align:center;word-break: break-all;\">Category</td>";
recipientsTableHtml+="<td style=\"border: 1px solid; height: 10px;text-align:center;word-break: break-all;\">sdgbgfjng</td>";
recipientsTableHtml+="</tr>";
}
recipientsTableHtml+=" </tbody>";
recipientsTableHtml+="</table>";
recipientsTableHtml+="</html>";
try {
String filename = UUID.randomUUID().toString()+"_htmlToPdf";
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));
builder.insertHtml(html);
// 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();
// Save doc
doc.save(filename+".pdf", com.aspose.words.SaveFormat.PDF);
System.out.println("PDF Created");
return "";
} catch (Exception e) {
System.out.println("Exception occurred" + e.getMessage());
return null;
}
html.zip (1.4 KB)
Please guide, thanks in advance!