Replace a Table in Word DOCX Document with another Table using C# .NET

Thank you and its working fine. In my case i am checking for one more scenario

Consider StartTags as below
string[] StartTag = new string[2];
StartTag[0] = @"$abcstart{";
StartTag[1] = @"$xyzstart{";

I am taking output file of previous post as input file (that is after start tag and before the end tag already have table now I have to delete node type table after start tag and insert with new table.Please can you suggest with sample code

//Add Your code logic here to identify table after start tag and delete code.
and for Inserting will use previous post.
//Inserting table after startag
DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
Run run = (Run)runs[runs.Count - 1];

        if (run.NextSibling.NodeType == NodeType.Run)
        {
            builder.MoveTo(run.NextSibling);

            DataTable dataTable = GetEmpTable();
            Table table = ImportTableFromDataTable(builder, dataTable, true);
            table.SetBorders(LineStyle.Single, 1.5, Color.Black);
        }

Try option 2 in below Sample Code.

Sample code with input and current out put and expected output file attched.Source.zip (79.5 KB)

@saranyasrinivasan92,

Please try using the following C# code to replace a Table in Word DOCX Document with another Table:

Document doc = new Document(@"E:\Temp\Source (3)\Input File as RegaxOutputFile.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Table table1 = (Table)doc.GetChildNodes(NodeType.Table, true)[0];
if (table1 != null)
{
    Table newTable = GetTable(builder);
    table1.ParentNode.InsertBefore(newTable, table1);
    table1.Remove();
}

doc.Save("E:\\Temp\\Source (3)\\20.5.docx"); 

public static Table GetTable(DocumentBuilder builder)
{
    Table table = builder.StartTable();
    builder.InsertCell();

    // Set the cell formatting
    CellFormat cellFormat = builder.CellFormat;
    cellFormat.Width = 250;
    cellFormat.LeftPadding = 30;
    cellFormat.RightPadding = 30;
    cellFormat.TopPadding = 30;
    cellFormat.BottomPadding = 30;

    builder.Writeln("I'm a wonderful formatted cell.");

    builder.EndRow();
    builder.EndTable();

    return table;
}

Hope, this helps.

Sorry , I think this won’t work in my Case

My Query is different

  1. After Start Tag find Table to Delete it
  2. and Insert with new table After Start Tag Using Aspose.

@saranyasrinivasan92,

Please check the following code:

Document doc = new Document("E:\\Temp\\Source (3)\\Input File as RegaxOutputFile.docx");

string startTag = "$xyzstart{";
string endTag = "}xyzend$";

Paragraph startPara = null;
Paragraph endPara = null;

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.ToString(SaveFormat.Text).Contains(startTag))
        if (startPara == null)
            startPara = para;

    if (para.ToString(SaveFormat.Text).Contains(endTag))
        if (endPara == null)
            endPara = para;
}

if (startPara != null && endPara != null)
{
    Table table1 = null;
    // Find Table between Start and End Paras
    Node currentNode = startPara;
    while (currentNode != null && currentNode != endPara)
    {
        if (currentNode.NodeType == NodeType.Table)
        {
            table1 = (Table)currentNode;
            break;
        }

        currentNode = currentNode.NextPreOrder(currentNode.Document);
    }

    if (table1 != null)
    {
        DocumentBuilder builder = new DocumentBuilder(doc);
        Table newTable = GetTable(builder);
        table1.ParentNode.InsertBefore(newTable, table1);
        table1.Remove();
    }
}

doc.Save("E:\\Temp\\Source (3)\\20.5.docx"); 

public static Table GetTable(DocumentBuilder builder)
{
    Table table = builder.StartTable();
    builder.InsertCell();

    // Set the cell formatting
    CellFormat cellFormat = builder.CellFormat;
    cellFormat.Width = 250;
    cellFormat.LeftPadding = 30;
    cellFormat.RightPadding = 30;
    cellFormat.TopPadding = 30;
    cellFormat.BottomPadding = 30;

    builder.Writeln("I'm a wonderful formatted cell.");

    builder.EndRow();
    builder.EndTable();

    return table;
}

Hope, this helps.

Currently getting Ouput as table deleted after start tag but table is not getting insert.

Sample code with input and current out put and expected output file attched

Try option 1 in below Sample Code.

Source.zip (130.9 KB)

@saranyasrinivasan92,

Please see these input and output DOCX Word documents (Docs.zip (47.1 KB)) and try running the following code:

Document doc = new Document("E:\\Temp\\Source (4)\\RegaxInputFile.docx");

string startTag = "$abcstart{";
string endTag = "}abcend$";

Paragraph startPara = null;
Paragraph endPara = null;

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.ToString(SaveFormat.Text).Contains(startTag))
        if (startPara == null)
            startPara = para;

    if (para.ToString(SaveFormat.Text).Contains(endTag))
        if (endPara == null)
            endPara = para;
}

if (startPara != null && endPara != null)
{
    Table table1 = null;
    // Find Table between Start and End Paras
    Node currentNode = startPara;
    while (currentNode != null && currentNode != endPara)
    {
        if (currentNode.NodeType == NodeType.Table)
        {
            table1 = (Table)currentNode;
            break;
        }

        currentNode = currentNode.NextPreOrder(currentNode.Document);
    }

    if (table1 != null)
    {
        DocumentBuilder builder = new DocumentBuilder(doc);
        Table newTable = GetTable(builder);
        table1.ParentNode.InsertBefore(newTable, table1);
        table1.Remove();
    }
}

doc.Save("E:\\Temp\\Source (4)\\20.5.docx"); 

public static Table GetTable(DocumentBuilder builder)
{
    Table table = builder.StartTable();
    builder.InsertCell();

    // Set the cell formatting
    CellFormat cellFormat = builder.CellFormat;
    cellFormat.Width = 250;
    cellFormat.LeftPadding = 30;
    cellFormat.RightPadding = 30;
    cellFormat.TopPadding = 30;
    cellFormat.BottomPadding = 30;

    builder.Writeln("I'm a wonderful formatted cell.");

    builder.EndRow();
    builder.EndTable();

    return table;
}

You can see in output DOCX, old table is replaced with new table. Hope, this helps.