Add paragraphs from a table to another

Hi,

I’m trying to insert Paragraph from an inputTableCellParagraph to outputTableCellParagraph

// outputTableCellParagraphs.getCount() => 1
// inputTableCellParagraphs.getCount() => 4

int size = inputTableCellParagraphs.getCount();  
while (size > i) {
  outputTableCellParagraphs.insert(i, inputTableCellParagraphs.get(i));
  i++;
}

In iteration = 2, inputTableCellParagraphs.get(i) is null.
At finish of this elaboration, outputTable contains the first and the last paragraph of inputTableCellParagraphs

Can anyone supports me?
Thanks in advice,
F.

@federico.altamura It’s hard to tell what happened without having the documents and code you use to insert paragraphs. Could you please provide the document and code to help us reproduce the problem on our side?

For example the following simple code copy paragraphs from second cell to first:

Document doc = new Document("Tables.docx");

// Find the source and destination table cells
Table sourceTable = (Table)doc.getChildNodes(NodeType.TABLE, true).get(0);
Cell sourceCell = sourceTable.getRows().get(0).getCells().get(0);
Cell destinationCell = sourceTable.getRows().get(0).getCells().get(1);

// Iterate through the paragraphs in the source cell
for (Paragraph sourceParagraph : destinationCell.getParagraphs()) {
    // Clone the source paragraph
    Paragraph clonedParagraph = (Paragraph)sourceParagraph.deepClone(true);

    // Append the cloned paragraph to the destination cell
    sourceCell.getParagraphs().add(clonedParagraph);
}

doc.save("output.docx");

Results:

Hi again,

Thanks for the answer.
The code, works! It seems that not maintain Runs (font, color, …) for every Paragraph

Thanks in advice
F.

@federico.altamura I’ve tested copying paragraphs and it works with fonts and colors preserved.