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

Free Support Forum - aspose.com

Insert table below the content to last row of the table

I want to find the table below the content should be sage table refence.copy that content and insert it into the last row of the table.
Before inserting create an extra row in the table
Marge that table
Remove left border
Remove right border
Remove bottom border
then insert that sage table reference to the last row of the table.

Please find the below input and expected output.
Expected_output66.docx (15.4 KB)
Input_word_document66.docx (13.8 KB)

@Manasahr You can achieve what you need using code like the following:

Document doc = new Document(@"C:\Temp\in.docx");

NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table t in tables)
{
    // Create an empty row.
    Row emptyRow = (Row)t.LastRow.Clone(true);
    emptyRow.FirstCell.RemoveAllChildren();
    emptyRow.FirstCell.CellFormat.HorizontalMerge = CellMerge.First;
    Cell nextCell = emptyRow.FirstCell.NextSibling as Cell;
    while (nextCell != null)
    {
        nextCell.RemoveAllChildren();
        nextCell.CellFormat.HorizontalMerge = CellMerge.Previous;
        nextCell = nextCell.NextSibling as Cell;
    }

    // Put an empty row at the end of the table.
    t.AppendChild(emptyRow);

    // Copy the paragraph after the table into the last row.
    emptyRow.FirstCell.AppendChild(t.NextSibling);

    // Remove borders.
    emptyRow.RowFormat.Borders.ClearFormatting();
}

doc.Save(@"C:\Temp\out.docx");

@alexey.noskov Kindly understand my question. You’re given the above code is just inserting a row for all the tables and appending the table below the content to the newly inserted row. But that’s not my question.

Kindly find the below mentioned input and expected output words file.
Expected_output66.docx (18.9 KB)
Input_word_document66.docx (17.6 KB)

  1. In the document I need to find out if the table is there or not.
  2. if the document contents table check below the table sage table reference style data is there or not
  3. if the table below the content is sage table reference style data.
  4. then insert one row and marge it
  5. remove the left, right, and bottom borders.
  6. copy that sage table reference style data and past into the newly inserted row

@Manasahr You can easily modify the provided code to meat your requirements:

Document doc = new Document(@"C:\Temp\in.docx");

// Get paragraphs with "Sage Table Reference" style applied.
List<Paragraph> paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>()
    .Where(p => p.ParagraphFormat.StyleName == "Sage Table Reference").ToList();

foreach (Paragraph p in paragraphs)
{
    // The the table before the "Sage Table Reference" paragraph
    Table table = p.PreviousSibling as Table;
    // Check whether the table exsists.
    if (table != null)
    {
        // Create an empty row.
        Row emptyRow = (Row)table.LastRow.Clone(true);
        emptyRow.FirstCell.RemoveAllChildren();
        emptyRow.FirstCell.CellFormat.HorizontalMerge = CellMerge.First;
        Cell nextCell = emptyRow.FirstCell.NextSibling as Cell;
        while (nextCell != null)
        {
            nextCell.RemoveAllChildren();
            nextCell.CellFormat.HorizontalMerge = CellMerge.Previous;
            nextCell = nextCell.NextSibling as Cell;
        }

        // Put an empty row at the end of the table.
        table.AppendChild(emptyRow);

        // Copy the paragraph after the table into the last row.
        emptyRow.FirstCell.AppendChild(p);

        // Remove borders.
        emptyRow.RowFormat.Borders.ClearFormatting();
    }
}

doc.Save(@"C:\Temp\out.docx");

The code provided earlier provides a basic idea of the implementation. It is not supposed to work out of the box for all your cases.