Add a blank table row with top border

I am trying to add a blank row to a table and add a top single black border to the blank row. My code is as follows:

private static void AddBlankRow(Table table)
    {
        // Clone the last row in the table.
        var clonedRow = (Row)table.LastRow.Clone(true);

        // Add top border to row
        // Remove all content from the cloned row's cells. 
        // This makes the row ready for new content to be inserted into.
        foreach (Cell cell in clonedRow.Cells)
        {
            cell.CellFormat.Borders.Top.LineStyle = LineStyle.Single;
            cell.CellFormat.Borders.Top.Color = Color.Black;
            cell.RemoveAllChildren();
            cell.EnsureMinimum();
        }

        // Add the row to the end of the table.
        table.AppendChild(clonedRow);
    }

I run into an exception on the following line:

cell.CellFormat.Borders.Top.LineStyle = LineStyle.Single;

because cell.CellFormat.Borders.Top.LineStyle has a null reference (in fact so do all of the attribures of Top).

How should I handle this so that I can add the border to the blank row.

Thanks,

David

Here is a copy of some of the exception stack trace:

System.NullReferenceException: Object reference not set to an instance of an object.
at Aspose.Words.Tables.Cell. (Int32 )
at Aspose.Words.Tables.Cell.FetchInheritedCellAttr(Int32 key)
at Aspose.Words.Border. ()
at Aspose.Words.Border. ()
at Aspose.Words.Border. (LineStyle )
at Aspose.Words.Border.set_LineStyle(LineStyle value)
at BTT.DocAssembler.WebSPA.Helpers.Snapshot2Replacer.AddBlankRow(Table table) in

@daveywc,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your input Word document
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

@awais.hafeez I have attached a sample project as requested.

input.docx is my input document.
required_result.docx is what I am looking to generate.

I am trying to add 4 rows to the 2nd table in the document that has “Report Recommendation” in the first column of the last row of that table. The 4 rows are as follows:

  1. Blank row with top border
  2. Committee Recommendation row
  3. Blank row with top border
  4. Council Resolution row

I can get the 4 rows added OK, but I cannot get the top borders to appear on the 2 blank rows. If you run the project you will see where I am trying to add the border as it crashes with an exception.

Kind regards,

David

@daveywc,

I am afraid, we do not see any attachments in this thread. If you are facing some difficulty while attaching the files in this thread, please ZIP the files, upload the ZIP file to Dropbox and share the download link here for testing. Thanks for your cooperation.

Here is the link to the zip file in DropBox:

https://www.dropbox.com/s/a1oouq0n9vbf469/aspose-words-table-border.zip?dl=0

@daveywc,

Please replace the “AddBlankRow” method in your application with the following:

private static void AddBlankRow(Table table)
{
    // Clone the last row in the table.
    var clonedRow = (Row)table.LastRow.Clone(true);

    // Add the row to the end of the table.
    table.AppendChild(clonedRow);

    // Add top border to row
    // Remove all content from the cloned row's cells. 
    // This makes the row ready for new content to be inserted into.
    foreach (Cell cell in clonedRow.Cells)
    {
        cell.CellFormat.Borders.Top.LineStyle = LineStyle.Single;
        cell.CellFormat.Borders.Top.Color = Color.Black;
        cell.RemoveAllChildren();
        cell.EnsureMinimum();
    }            
}

Hope, this helps.

@awais.hafeez That worked great - thank you!