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

Free Support Forum - aspose.com

Copy one entire cell from table

Hi,

I have 2 word files and both has some tables. I want to copy one entire cell with format from one file and paste it to the second file. Currently I can only copy the text and place it to the second file.

In the source file of cell has some text, images, hyperlink, table etc. and I want to copy all contents to the cell of destination file. How do I achieve this?

@echospider,

Please see these sample input/output Word documents (Copy-CellFormat-Info.zip (10.2 KB)) and try running the following code. Hope, this helps.

// Source document you want to copy CellFormat from
Document doc = new Document(MyDir + @"input.docx");
// Empty document to copy CellFormat info to
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
// Create a One Row One Cell Table from scratch
Table dstTable = builder.StartTable();
Cell targetCell = builder.InsertCell();
builder.Writeln("I'm a wonderful formatted cell.");
builder.EndRow();
builder.EndTable();
// Get Cell you want to copy CellFormat of
Cell srcCell = doc.FirstSection.Body.Tables[0].LastRow.LastCell;
// Import the Cell in destination document
Cell dstCell = (Cell)dstDoc.ImportNode(srcCell, false);
dstCell.EnsureMinimum();
builder.MoveTo(dstCell.FirstParagraph);
builder.Writeln("I'm a wonderful formatted cell.");
// Insert the cell at Row 1 Cell 2 position
targetCell.ParentNode.InsertAfter(dstCell, targetCell);
// Save document
dstDoc.Save(MyDir + @"18.5.docx");

This is not the thing I want to achieve. I want to copy the whole content of a cell which has some text, hyperlink ,image etc. and paste the content to another existing cell in the destination table.

@echospider,

Please see these documents (Docs.zip (32.5 KB)) and try running the following code:

Document src = new Document(MyDir + @"Docs\src.docx");
Document dst = new Document(MyDir + @"Docs\dst.docx");

// Get Cell you want to copy
Cell srcCell = src.FirstSection.Body.Tables[0].FirstRow.FirstCell;
// Import the Cell in destination document
Cell dstCell = (Cell)dst.ImportNode(srcCell, true);

Cell targetCell = dst.FirstSection.Body.Tables[0].FirstRow.FirstCell;
// Insert the cell at Row 1 Cell 2 position
targetCell.ParentNode.InsertAfter(dstCell, targetCell);
// Save document
dst.Save(MyDir + @"Docs\18.5.docx");

Hope, this helps.

How do I insert/copy based on index. Eg: I want to put the cell at Row 1 Cell 1

I want to paste the cell to a particular cell of the destination table. For example I have 2 rows and 5 columns in destination table, I want to paste the copied cell to row number 1 cell number 3. Please help.

@echospider,

Please ZIP and attach your input (source, destination) Word documents and expected Word document showing the final output here for our reference. Please create expected Word document by using MS Word. We will investigate your scenario further and provide you code to achieve this by using Aspose.Words.