We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Table : Replace cell by another

Hello,

I want in a document table, replace one cell by another but when i use methods “insertAfter” and “removeChild” my table format has changed, is shifted to the left side…

In the zip attached there the source template (before.dot) and the final document after my processing (after.doc)

I used the code below with Aspose.Words 2.4.2 for Java :
//Replace cell at position[0;0] by cell at position [0,1]
//“refEmptyCell” is a deep clone of the cell at postion [0;1]
currentTable.getRows().get(0).insertAfter(refEmptyCell,currentTable.getRows().get(0).getFirstCell());
currentTable.getRows().get(0).removeChild(currentTable.getRows().get(0).getFirstCell());

Thanks in advance for your helps.

Dominique

Hi,

Thank you for your request. Starnge but the inserted cell has incorrect left padding which makes the table shifting. Could you please post the entire code (includeing deep cloning of the cell [0; 1])?

Hi,

Please find below a sample of the main code :

if ((currentPageNumber > 1) && (currentPageNumber < pageCount)) {
// -----Fill table from position position [0;0] to
// end of the table
for (int x = 0; x < currentTable.getRows().getCount(); x++) {
for (int y = 0; y < currentTable.getRows().get(x).getCells().getCount(); y++) {
internalLabelCopyCountIndex++;
internalTotalLabelCopyCountIndex++;
// Replace current cell by the reference
// cell with bookmark
Cell currentCell = currentTable.getRows().get(x).getCells().get(y);
currentTable.getRows().get(x).insertAfter(refCell.deepClone(true), currentCell);
currentTable.getRows().get(x).removeChild(currentCell);
// Replace bookmarks by values
this.replaceBookmarksByValues(workingDocument, addressee.get(currentAddressIndex)
.getAddressBookmarks());
// Check if we have inserted the number of
// copy of the current address
if (internalLabelCopyCountIndex >= addressee.get(currentAddressIndex)
.getLabelCopyCount()) {
internalLabelCopyCountIndex = 0;
currentAddressIndex++;
}
}
}
// Next page
continue;
}

I have noticed that the problem only occurs if the start of the table is not filled…

I have added the entire method in attachment.

Thanks you very much in advance for your help.

Dominique

Hi,

The source of problem is the replacement of a cell by another through association of invocation of “insertAfter” and “removeChild” methods on a DocumentBuilder object…but i don’t know why…

Document workingDocument = new Document(templateFile.getAbsolutePath());
DocumentBuilder workingDocumentBuilder = new DocumentBuilder(workingDocument);
// —Check presence of a main and single table and also
// that template contains only one page

NodeCollection nodeCollection = workingDocument.getChildNodes(NodeType.TABLE, true);
Table mainTable = (Table) nodeCollection.get(0);
// —Get of copy of the cell at position[0;0] of the main
// table
// because it’s the reference cell that contains the
// bookmarks

Cell refCell = (Cell) mainTable.getRows().get(0).getCells().get(0).deepClone(true);
Cell refEmptyCell = (Cell) mainTable.getRows().get(0).getCells().get(1).deepClone(true);
// —Create the target page count and duplicate main table
// on each page (one table by page)

for (int i = 1; i < pageCount; i++) {
// Insert the new table on new page
workingDocumentBuilder.moveToDocumentEnd();
workingDocumentBuilder.insertBreak(BreakType.PAGE_BREAK);
workingDocumentBuilder.getCurrentParagraph().getParentNode().insertAfter(
mainTable.deepClone(true), workingDocumentBuilder.getCurrentParagraph());
// In the new table, replace cell at position [0;0] by
// cell at position [0,1] of the main table in order to
// have only bookmarks on cell[0;0] in the first table

Table currentNewTable = (Table) workingDocument.getChildNodes(NodeType.TABLE, true).get(i);
currentNewTable.getRows().get(0).insertAfter(refEmptyCell.deepClone(true),
currentNewTable.getRows().get(0).getFirstCell());
currentNewTable.getRows().get(0).removeChild(currentNewTable.getRows().get(0).getFirstCell());
}

The green code run well but the after the red code, the table format has changed…

Do you have a official and clean way to replace one cell by another in a table ?

Thanks in advance for your helps.

I have resolved my issue using the RowAlignment property and set it to CENTER for each row of my table.
for (int x = 0; x < currentTable.getRows().getCount(); x++) {
currentTable.getRows().get(x).getRowFormat().setAlignment(RowAlignment.CENTER);
}

I’m glad you have resolved it yourself. Thank you for your valuable input.