Hi,
I am generating a table which is a repeating section in my pdf. Currently, I am able to generate a table with rows and columns I just want to set text in the cells by using Run class(com.aspose.words.Run) but I am unable to find any methods which will set text for all the cells of the row. Can I get a help regarding this issue?
@Sri_Harsha You can achieve this using the following simple code:
Document doc = new Document("C:\\Temp\\in.docx");
// Get the table.
Table table = doc.getFirstSection().getBody().getTables().get(0);
// Remove old content of the cell.
Cell cell = table.getFirstRow().getFirstCell();
cell.removeAllChildren();
// Ensure minimum and put new content.
cell.ensureMinimum();
cell.getFirstParagraph().appendChild(new Run(doc, "This is new content of the cell"));
doc.save("C:\\Temp\\out.docx");
@alexey.noskov,
Thanks for the response, I think the above code works for only single cell of the row but I want to make it work for all the cells of all the rows and I am using
Table table = (Table) compositeParent;
where compositeParent is CompositeNode<?> compositeParent = repeaterTemplate.getParentNode();
and repeaterTemplate is a structured document tag in my code.Can you help me the code which suits the above code for setting the text for all the columns of all the rows.
@Sri_Harsha You can simply loop through all rows and cells in the table and change their content as shown in the above code.
Actually I tried to use cell collection instead of cell and I looped it for all the rows and cells but I was getting errors because of cell.clearallchildren
@Sri_Harsha Here is code that updates content of all cells in the table. it works fine on my side:
Document doc = new Document("C:\\Temp\\in.docx");
// Get the table.
Table table = doc.getFirstSection().getBody().getTables().get(0);
for (Row row : table.getRows())
{
for (Cell cell : row.getCells())
{
// Remove old content of the cell.
cell.removeAllChildren();
// Ensure minimum and put new content.
cell.ensureMinimum();
cell.getFirstParagraph().appendChild(new Run(doc, "This is new content of the cell"));
}
}
doc.save("C:\\Temp\\out.docx");