Free Support Forum - aspose.com

Getting borders from a table

attached is my project.
I am trying to detect if a table has borders.
this is returning true, even if there isn’t borders on the table.
Sections.zip (23.6 KB)
I am simply calling this code:
Aspose.Words.Tables.Table m_Table = (Aspose.Words.Tables.Table)AsposeDocument.GetChild(NodeType.Table, 0, true);
Border mBorder = m_Table.FirstRow.RowFormat.Borders[1];
Boolean isSet = mBorder.LineStyle != 0;

and isSet is returning true, even though the template is showing that the borders are set to none

this is happening on many tables on many templates.

how can I get this value

@conniem,

Thanks for your inquiry. Please use the following code example to check the border of table’s cell. Hope this helps you.

Document AsposeDocument = new Document(MyDir + "test.dot");
Aspose.Words.Tables.Table m_Table = (Aspose.Words.Tables.Table)AsposeDocument.GetChild(NodeType.Table, 0, true);
Boolean isSet = false;
foreach (Cell cell  in m_Table.GetChildNodes(NodeType.Cell, true))
{
    Console.WriteLine(cell.CellFormat.Borders[BorderType.Top].LineStyle);
    if (cell.CellFormat.Borders[BorderType.Top].LineStyle != LineStyle.None)
    {
        isSet = true;
        break;
    }
                        
}
Console.WriteLine(isSet);

it says none/false

@conniem,

Thanks for your inquiry. Yes, the border of table is set as “None” in your document. Please check the attached image for detail. table border.png (25.8 KB)

guess I don’t understand.
I am attempting to get the border on a table.
trace the code.
it says there is a border on the table, even though there is not.
the code you sent me looks for a table on a CELL, not on the table.

@conniem,

Thanks for your inquiry. Please use CellFormat.Borders property to get the border of table. To check the border of table, you need to check the top border of cells in first row and bottom border in the last row of table.

To check the left and right border, please check the border of first and last cell of row with BorderType Left and Right.

so, the response here:


is incorrect.

and the code that checks the border for the top row of the table, using the aspose call, is also incorrect (because it’s returning that it’s set, when it is not).

now,I have to iterate all the cells in the top row to see if a cell has a border, then all the cells in the bottom row to see if a border is set, then the first and last one in each row?

what happens if there is a table with a cell that has borders, vs the one that results in the border that surrounds the table?

Further,
using the borders from the first row often does work correctly (returns linestyle = none) on many of my tables.
however, like the one I sent you in the test, it isn’t always working.
when I used the code to iterate the cells, it is coming back as none, so why isn’t it working on the row level, which is the one I want?

so, I added this code:
Aspose.Words.BorderCollection Borders = m_Table.FirstRow.RowFormat.Borders;
Console.WriteLine(Borders[BorderType.Top].LineStyle);
Console.WriteLine(Borders[BorderType.Left].LineStyle);
Console.WriteLine(Borders[BorderType.Right].LineStyle);
Console.WriteLine(Borders[BorderType.Bottom].LineStyle);
and they are ALL returing ‘single’.
obviously, there are no borders in the template.
why would these then return ‘single’?

@conniem,

The response in the other thread is correct. Please note that MS Word sets the border to Cell and Table. However, Aspose.Words provides API to get the border of Row. In your case, this property returns incorrect value. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-16840. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

In your case, we suggest you please use Cell.CellFormat.Borders property to get the correct value.

@conniem,

Thanks for your patience. You are facing the expected behavior of Aspose.Words. The border can be defined on different level of table e.g. table style, table itself or cell. Your document has table with single border and this table has one cell with none border. Aspose.Words reports the default values for the first row (as properties for the table), but actually they are overridden for the one and only cell (that is in the first row).

In your case, we suggest you please use Cell.CellFormat.Borders property to get the correct value.

Document doc = new Document(MyDir + "test.dot");
Aspose.Words.Tables.Table m_Table = (Aspose.Words.Tables.Table)doc.GetChild(NodeType.Table, 0, true);
m_Table.Rows.Add(m_Table.Rows[0].Clone(true));

Console.WriteLine(m_Table.FirstRow.RowFormat.Borders.Top.LineStyle);  // single border
Console.WriteLine(m_Table.FirstRow.FirstCell.CellFormat.Borders.Top.LineStyle);  // none border