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

Free Support Forum - aspose.com

Change Border Line Style of Some Table Cells in Word Document using C# .NET | Set Borders of Table Rows Cells

Hi,

I just need to change the border styles from a cell.

The cell borders ingerits border style which was set in the table level.

As depict in the attached “before” image, the cell that I want is sournded with a border.

But I just need to get rid of bottom border as shown in the “after” image.

Before.PNG (2.3 KB)
after.PNG (1.3 KB)

Appreciate your prompt reply in this regard.

Reagrds,

@Efic_Helpdesk,

Please see these sample input/output Word documents (Table with Cell level borders.zip (20.6 KB)) and try running the following code that processes/removes specific cell level borders from table:

Document doc = new Document("E:\\Temp\\Table with Cell level borders.docx");

Table table = doc.FirstSection.Body.Tables[0];
for (int i = 1; i < table.Rows.Count - 1; i++)
{
    foreach (Cell cell in table.Rows[i])
    {
        cell.CellFormat.Borders.Top.LineStyle = LineStyle.None;
        cell.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
    }
}

doc.Save("E:\\Temp\\20.6.docx");

We first need to determine whether the Table borders are applied via a Table Style or at Table, Row or Cell levels? Can you please also ZIP and attach the following resources here for testing?

  • Your simplified input Word document
  • Your expected DOCX document showing the desired output. You can create this document by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you code to achieve the same expected output by using Aspose.Words API.

@awais.hafeez,

Thanks for your prompt reply.

I have applid borders in table levels. Because I just want to maintain the 3 columns and any numbers rows as I wanted.
There for I applied the table borders as below.
e.g:
table.SetBorders(LineStyle.Single, 0.1, Color.FromArgb(191, 191, 191));

I just want to remove the border as highlighted in the attached image.
BordersToBeREmoved.PNG (1.4 KB)

Also note that, in MS word, I can use the cell context menu to show/hide borders of the table.
Please see the attached.
hide-borders.png (10.4 KB)

Below is the code that I used to generate the table and cells. Also I’m trying to set the borders of the cells, but seems it doesn’t get applied to the cell borders.

var templatePath = _documentFacilityScheduleFactory.GetTemplatePath();
        var document = new Document(templatePath);
        var builder = new DocumentBuilder(document);
       
        var table = builder.StartTable();

        var emptyName = builder.InsertCell();
        emptyName.CellFormat.Width = 42.5;
        
        
        //empty.CellFormat.Borders.ClearFormatting();
        var name = builder.InsertCell();
        builder.Writeln("Name");
        name.CellFormat.Width = 77.10;
        var nameValue = builder.InsertCell();
        builder.Writeln("Name of the element");
        nameValue.CellFormat.Width = 425.197;
        nameValue.CellFormat.Borders.Left.LineStyle = LineStyle.None;
        nameValue.CellFormat.Borders.Right.LineStyle = LineStyle.None;
        builder.EndRow();
        builder.RowFormat.ClearFormatting();
        builder.CellFormat.ClearFormatting();

        var emptyABN = builder.InsertCell();
        emptyABN.CellFormat.Width = 42.5;
        //empty.CellFormat.Borders.ClearFormatting();
        var abn = builder.InsertCell();
        builder.Writeln("ABN"); 
        abn.CellFormat.Width = 77.10;
        var abnValue = builder.InsertCell();
        builder.Writeln("12332222");
        abnValue.CellFormat.Width = 425.197;
        abnValue.CellFormat.Borders.Top.LineStyle = LineStyle.DoubleWave;
        builder.EndRow();

        table.TopPadding = 2.8;
        table.BottomPadding = 2.8;
        table.LeftPadding = 5.3;
        table.RightPadding = 5.3;
        table.FirstRow.RowFormat.HeadingFormat = true;
        table.ClearBorders();
        table.SetBorders(LineStyle.Single, 0.1, Color.FromArgb(191, 191, 191));
        builder.EndTable();
        
        builder.Document.Save(_saveSaveFilePath);

Appreciate your prompt reply in this regard.

@Efic_Helpdesk,

I have generated a DOCX file (input-table.docx) by using the following code: (see Remove Some Table Borders.zip (13.3 KB))

Document doc = new Document();
var builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();

var table = builder.StartTable();

var emptyName = builder.InsertCell();
emptyName.CellFormat.Width = 42.5;

//empty.CellFormat.Borders.ClearFormatting();
var name = builder.InsertCell();
builder.Writeln("Name");
name.CellFormat.Width = 77.10;
var nameValue = builder.InsertCell();
builder.Writeln("Name of the element");
nameValue.CellFormat.Width = 425.197;
nameValue.CellFormat.Borders.Left.LineStyle = LineStyle.None;
nameValue.CellFormat.Borders.Right.LineStyle = LineStyle.None;
builder.EndRow();
builder.RowFormat.ClearFormatting();
builder.CellFormat.ClearFormatting();

var emptyABN = builder.InsertCell();
emptyABN.CellFormat.Width = 42.5;
//empty.CellFormat.Borders.ClearFormatting();
var abn = builder.InsertCell();
builder.Writeln("ABN");
abn.CellFormat.Width = 77.10;
var abnValue = builder.InsertCell();
builder.Writeln("12332222");
abnValue.CellFormat.Width = 425.197;
abnValue.CellFormat.Borders.Top.LineStyle = LineStyle.DoubleWave;
builder.EndRow();

table.TopPadding = 2.8;
table.BottomPadding = 2.8;
table.LeftPadding = 5.3;
table.RightPadding = 5.3;
table.FirstRow.RowFormat.HeadingFormat = true;
table.ClearBorders();
table.SetBorders(LineStyle.Single, 0.1, Color.FromArgb(191, 191, 191));
builder.EndTable();

doc.Save("E:\\Temp\\input-table.docx");

After that you can use the following code to remove the desired table borders:

Document doc = new Document("E:\\Temp\\input-table.docx");

Table table = doc.FirstSection.Body.Tables[0];
for (int i = 0; i < table.Rows.Count; i++)
{
    foreach (Cell cell in table.Rows[i])
    {
        if (i == 0)
        {
            cell.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
        }
        else if (i == table.Rows.Count - 1)
        {
            cell.CellFormat.Borders.Top.LineStyle = LineStyle.None;
        }
        else
        {
            cell.CellFormat.Borders.Top.LineStyle = LineStyle.None;
            cell.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
        }
    }
}

doc.Save("E:\\Temp\\20.6.docx");

@awais.hafeez,

Thanks for this. But I don’t want to read the generated document again and apply the format.
I just want to do it as I build the document and content.

Do you have solution for that?

Thanks

@Efic_Helpdesk,

Please note that Table.SetBorders method is just a shorthand method for setting all Borders for all rows to the same value. You are calling it at the end; that will just reset all Table borders to the specified new values. In that case, you have to remove borders from desired cells like this:

Document doc = new Document();
var builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();

var table = builder.StartTable();

var emptyName = builder.InsertCell();
emptyName.CellFormat.Width = 42.5;

//empty.CellFormat.Borders.ClearFormatting();
var name = builder.InsertCell();
builder.Writeln("Name");
name.CellFormat.Width = 77.10;

var nameValue = builder.InsertCell();
builder.Writeln("Name of the element");
nameValue.CellFormat.Width = 425.197;
nameValue.CellFormat.Borders.Left.LineStyle = LineStyle.None;
nameValue.CellFormat.Borders.Right.LineStyle = LineStyle.None;
builder.EndRow();
builder.RowFormat.ClearFormatting();
builder.CellFormat.ClearFormatting();

var emptyABN = builder.InsertCell();
emptyABN.CellFormat.Width = 42.5;
//empty.CellFormat.Borders.ClearFormatting();

var abn = builder.InsertCell();
builder.Writeln("ABN");
abn.CellFormat.Width = 77.10;

var abnValue = builder.InsertCell();
builder.Writeln("12332222");
abnValue.CellFormat.Width = 425.197;
abnValue.CellFormat.Borders.Top.LineStyle = LineStyle.DoubleWave;
builder.EndRow();

table.TopPadding = 2.8;
table.BottomPadding = 2.8;
table.LeftPadding = 5.3;
table.RightPadding = 5.3;
table.FirstRow.RowFormat.HeadingFormat = true;
table.ClearBorders();
table.SetBorders(LineStyle.Single, 0.1, Color.FromArgb(191, 191, 191));

builder.EndTable();

for (int i = 0; i < table.Rows.Count; i++)
{
    foreach (Cell cell in table.Rows[i])
    {
        if (i == 0)
        {
            cell.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
        }
        else if (i == table.Rows.Count - 1)
        {
            cell.CellFormat.Borders.Top.LineStyle = LineStyle.None;
        }
        else
        {
            cell.CellFormat.Borders.Top.LineStyle = LineStyle.None;
            cell.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
        }
    }
}

doc.Save("E:\\Temp\\20.6.docx");