Free Support Forum - aspose.com

Table Formatting with Word for Java

Hi,
I am trying to populate a table from a template present in word file (Pls look at attached template file). The 2nd row in table contains format which should be used in newly created row. I am able to get cell width, colors etc, but unable to get Font specific formatting like Bold, italics, underline, color etc.

Following is the code I am using to create table:
------------------------------------------------------------------------------------------------------
Document doc = new Document(“C:/data/table.doc”);
DocumentBuilder builder = new DocumentBuilder(doc);

Range range = doc.getRange();
FormFields flds = doc.getRange().getFormFields();
FormField fld = flds.get(0);
FormField st = flds.get(“start”);

Row tableRow = (Row)fld.getParentNode().getParentNode().getParentNode();
// Get reference to enclosing table
Table t = tableRow.getParentTable();
Row formatRow = t.getLastRow();
Rows rs = t.getRows();
boolean removeFormatRow = rs.getCount() == 2;

Cells cells = t.getLastRow().getCells();

int colSize = cells.getCount();
int noOfRows = 10;
builder.moveTo(t.getNextSibling());
for(int rowIndex=0; rowIndex<noOfRows; rowIndex++)
{
for(int colIndex=0; colIndex<colSize; colIndex++)
{
String n = rowIndex+""+colIndex;
ParagraphFormat pf = cells.get(colIndex).getParagraphs().get(0).getParagraphFormat();
cells.get(colIndex).getParagraphs().get(0).getText();
Style sty = pf.getStyle();
Font font = sty.getFont();
System.out.println("-------------------");
System.out.println(font.getBold());
System.out.println(font.getItalic());
System.out.println(font.getUnderline());


builder.insertCell();
builder.getCellFormat().setWidth(cells.get(colIndex).getCellFormat().getWidth());
builder.getParagraphFormat().setAlignment(cells.get(colIndex).getParagraphs().get(0).getParagraphFormat().getAlignment());
builder.getCellFormat().getShading().setBackgroundPatternColor(cells.get(colIndex).getCellFormat().getShading().getBackgroundPatternColor());
builder.getParagraphFormat().setStyle(sty);
builder.insertTextInput(n,0,"",rowIndex+":"+colIndex,20);
FormField f = range.getFormFields().get(n);
f.setHelpText(“somehscls”);
}
builder.endRow();
}
builder.endTable();
if(removeFormatRow)
{
rs.remove(formatRow);
}
doc.save(“C:/data/out.doc”);

-----------------------------------------------------------------------------------------------
Am I missing something to in this code to achive desired result?

Attachment : Word template used ( table_template.doc)
output of above code (ooutput.doc)


Adding zip file to used in sample code

You can just clone the row using the Clone method and append the cloned row to the table:

row.getParentTable().getRows().add(row.deepClone(true));

Best regards,