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

Free Support Forum - aspose.com

Replace table from one document to another using C#

Hi Team,
I have two word document file (File A,File B),
In File A there is one table with 3 row and File B there is one table with 3 row
So i have to Replace table content File b to file A

Please find the attachment for your Reference:Sample Document.zip (33.7 KB)

We are expected output file name:OUT PUT File A.docx

@thiru1711

In your case, we suggest you following solution.

  1. Please find/get the table from source document.
  2. Do the same for the destination document.
  3. Please use Document.ImportNode method to import a node from another document to the current document.
  4. Insert the Table after/before the table in the destination document.
  5. Remove the table that you want to replace.

Please use the following code example to get the desired output. Hope this helps you.

Document docA = new Document(MyDir + "File A.docx");
Document docB = new Document(MyDir + "File B.docx");

Table tableA = docA.FirstSection.Body.Tables[0];
Table tableB = docB.FirstSection.Body.Tables[0];

Table newTable = (Table)docA.ImportNode(tableB, true);
docA.FirstSection.Body.InsertAfter(newTable, tableA);

tableA.Remove();
docA.Save(MyDir + "20.1.docx");

thanks for replay ,
we are used above the sample coding, single orientation document only working , different orientation document not working .
(Above the coding you are mention only FirstSection( docA.FirstSection.Body.Tables[0]) )

We have using different orientation in that document , how to fix is issue.

sample Document for your Reference : DOC.zip (36.1 KB)

@thiru1711

The code example shared in my previous post is a sample code to insert table from one document into another document.

You need to use Document.ImportNode method to import table from one document into another document. The CompositeNode.InsertAfter and CompositeNode.InsertBefore methods can be used to insert the table after and before the specified reference node.

In your new documents, there are four sections and every section contains the Table node. You can use the same approach shared in my previous post to replace tables. You can get the sections of documents using Document.Sections property. We suggest you please read the following articles.
Aspose.Words Document Object Model
Obtaining a Section