Struggling with borders in table

I’m currently evaluating Aspose.Word. I like it a lot so far but I’m struggling with borders in a table in a word document.
I have to insert a table with up to a few hundred rows. Normally there are no borders in the table.
Some cells may have border though.

Here is some sample code where I build a table with 6 rows and 5 cells each. I want the whole table to have no borders and in row 3 and column 3 a single bottom border.
No matter what I do I either have either borders on all cells in the last row or on all cells.
What am I doing wrong here?

Dim doc As New Document()
Dim builder As New DocumentBuilder(doc)

Dim table As Table = builder.StartTable()
table.SetBorders(LineStyle.None, 0, Color.White)

For introw As Integer = 0 To 5
    table.SetBorders(LineStyle.None, 0, Color.White)
    For intcol As Integer = 0 To 4
        builder.InsertCell()
        If introw = 2 And intcol = 2 Then
            builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single
            builder.CellFormat.Borders.Bottom.LineWidth = 2
        Else
            builder.CellFormat.Borders.ClearFormatting()
        End If
        builder.Write(String.Format("r={0} c={1}", introw, intcol))
    Next
    builder.EndRow()
Next

Hi Thomas,

Thanks for your inquiry. Please use following code example to achieve your requirements. Hope this helps you.

If you still face problem, please share your expected output document. We will then provide you more information about your query along with code.

Dim doc As New Document()
Dim builder As New DocumentBuilder(doc)
Dim table As Table = builder.StartTable()
table.SetBorders(LineStyle.None, 0, Color.White)
For introw As Integer = 0 To 5
    For intcol As Integer = 0 To 4
        builder.InsertCell()
        builder.Write(String.Format("r={0} c={1}", introw, intcol))
    Next
    builder.EndRow()
Next
builder.EndTable()
table.ClearBorders()
table.Rows(2).Cells(2).CellFormat.Borders.Bottom.LineStyle = LineStyle.Single
table.Rows(2).Cells(2).CellFormat.Borders.Bottom.LineWidth = 2
doc.Save("Out.docx")

Thanks for your help.
Is there no other way to adding the border to certain cells?
I have to build a table which may have up to 500-600 rows and depending on the input some cells may have borders.
If I have to clear the borders after building the table I have to somehow remember where to set the borders and the set the borders for those cells.

Relating question: is there a way to find out on which row the builder is currently adding cells?

Thomas

just another two questions:
- how do I set the left/right border for a whole column in a table? do I have to add a left/right border to every single cell?
- I tried to set the bottom border of a row. This does show in the word document. Why?

Thomas

Hi Thomas,

Thanks for your inquiry.

toschf71de:
Is there no other way to adding the border to certain cells?

You may use following code example to achieve your requirements.

toschf71de:
Relating question: is there a way to find out on which row the builder is currently adding cells?

Please check the highlighted code snippet in following example.

Dim doc As New Document()
Dim builder As New DocumentBuilder(doc)
Dim table As Table = builder.StartTable()
builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.None
builder.CellFormat.Borders.Bottom.LineWidth = 0
builder.CellFormat.Borders.Top.LineStyle = LineStyle.None
builder.CellFormat.Borders.Top.LineWidth = 0
builder.CellFormat.Borders.Left.LineStyle = LineStyle.None
builder.CellFormat.Borders.Left.LineWidth = 0
builder.CellFormat.Borders.Right.LineStyle = LineStyle.None
builder.CellFormat.Borders.Right.LineWidth = 0
For introw As Integer = 0 To 5
    For intcol As Integer = 0 To 4
        If introw = 2 And intcol = 2 Then
            builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.[Single]
            builder.CellFormat.Borders.Bottom.LineWidth = 2
        Else
            builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.None
            builder.CellFormat.Borders.Bottom.LineWidth = 0
            builder.CellFormat.Borders.Top.LineStyle = LineStyle.None
            builder.CellFormat.Borders.Top.LineWidth = 0
            builder.CellFormat.Borders.Left.LineStyle = LineStyle.None
            builder.CellFormat.Borders.Left.LineWidth = 0
            builder.CellFormat.Borders.Right.LineStyle = LineStyle.None
            builder.CellFormat.Borders.Right.LineWidth = 0
        End If
        builder.InsertCell()
        builder.Write(String.Format("r={0} c={1}", introw, intcol))
        Dim row As Row = DirectCast(builder.CurrentParagraph.GetAncestor(NodeType.Row), Row)
    Next
    builder.EndRow()
Next
builder.EndTable()

*toschf71de:

  • how do I set the left/right border for a whole column in a table? do I have to add a left/right border to every single cell?*

Please refer to the following article:
Applying Borders and Shading to Table

*toschf71de:

  • I tried to set the bottom border of a row. This does show in the word document. Why?*

Could you please share some more detail about this query along with input and output documents along with code? We will then provide you more information about this query.

toschf71de:
- I tried to set the bottom border of a row. This does show in the word document. Why?

This code does not set a bottom border on row 3.

Dim doc As New Document()
Dim builder As New DocumentBuilder(doc)

Dim table As Table = builder.StartTable()
table.SetBorders(LineStyle.None, 0, Color.White)
table.ClearBorders()

For introw As Integer = 0 To 5
    For intcol As Integer = 0 To 4
        builder.InsertCell()
        builder.Write(String.Format("r={0} c={1}", introw, intcol))
    Next
    builder.EndRow()
Next

table.ClearBorders()
table.Rows(2).Cells(2).CellFormat.Borders.Bottom.LineStyle = LineStyle.Single
table.Rows(2).Cells(2).CellFormat.Borders.Bottom.LineWidth = 2

table.Rows(3).RowFormat.Borders.Bottom.LineStyle = LineStyle.Double
table.Rows(3).RowFormat.Borders.Bottom.LineWidth = 1
table.Rows(3).RowFormat.Borders.Bottom.Color = Color.Red

Thomas

Hi Thomas,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-14604. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

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

Dim doc As New Document()
Dim builder As New DocumentBuilder(doc)
Dim table As Table = builder.StartTable()
table.SetBorders(LineStyle.None, 0, Color.White)
table.ClearBorders()
For introw As Integer = 0 To 5
    For intcol As Integer = 0 To 4
        builder.InsertCell()
        builder.Write(String.Format("r={0} c={1}", introw, intcol))
    Next
    builder.EndRow()
Next
builder.EndTable()
table.ClearBorders()
table.Rows(2).Cells(2).CellFormat.Borders.Bottom.LineStyle = LineStyle.[Single]
table.Rows(2).Cells(2).CellFormat.Borders.Bottom.LineWidth = 2
For Each cell As Cell In table.Rows(3).Cells
    cell.CellFormat.Borders(BorderType.Bottom).LineWidth = 1
    cell.CellFormat.Borders(BorderType.Bottom).Color = Color.Red
Next
doc.Save(MyDir + " Out v16.12.0.docx")