Hi Team,
Please let me know if there is any possibility to read and extract the inbuilt table designs (Grid Table 1 Light, Grid Table 1 Light - Accent 1 etc.) while reading the table content from word file.
Thanks in advance!!
Document doc = new Document();
doc.LoadFromFile(@"test.docx");
Section section = doc.Sections[0];
Table table = section.Tables[0] as Table;
string tableStyleName = table.TableStyleName;
foreach(TableRow tableRow in table.Rows)
{
foreach (TableCell tableCell in tableRow.Cells)
{
Color backColor = tableCell.CellFormat.BackColor;
Color borcerColor = tableCell.CellFormat.Borders.Top.Color;
}
}
doc.SaveToFile("result.docx", FileFormat.Docx);
Document doc = new Document();
doc.LoadFromFile("table_issue.docx");
Section section = doc.Sections[0];
Table table = section.Tables[2] as Table;
string tableStyleName = table.TableStyleName;
table.ApplyTableStyle();
foreach (TableRow tableRow in table.Rows)
{
foreach (TableCell tableCell in tableRow.Cells)
{
Paragraph childObjects = (Paragraph)tableCell.ChildObjects[0];
Color backColor = tableCell.CellFormat.BackColor;
Color borcerColor = tableCell.CellFormat.Borders.Top.Color;
}
}
doc.SaveToFile("result.docx", FileFormat.Docx);
Document doc = new Document();
doc.LoadFromFile("table_issue.docx");
Section section = doc.Sections[0];
Table table = section.Tables[2] as Table;
//Apply the table style
table.ApplyTableStyle();
//Get table's border colors
Color colorTopBorder = table.TableFormat.Borders.Top.Color;
Color colorBottomBorder = table.TableFormat.Borders.Bottom.Color;
Color colorLeftBorder = table.TableFormat.Borders.Left.Color;
Color colorRightBorder = table.TableFormat.Borders.Right.Color;
foreach (TableRow tableRow in table.Rows)
{
//Get row's height
float rowHeight = tableRow.Height;
int row = table.Rows.IndexOf(tableRow);
foreach (TableCell tableCell in tableRow.Cells)
{
int column = tableRow.Cells.IndexOf(tableCell);
//Get cell's width
Console.WriteLine("Cell's width: " + tableCell.Width);
//Get cell's background color
Color backColor = tableCell.CellFormat.BackColor;
//Get cell's border color
Color topBorderColor = tableCell.CellFormat.Borders.Top.Color;
Color bottomBorderColor = tableCell.CellFormat.Borders.Bottom.Color;
Color leftBorderColor = tableCell.CellFormat.Borders.Left.Color;
Color rightBorderColor = tableCell.CellFormat.Borders.Right.Color;
//Get borders' width
float topBorderWidth = tableCell.CellFormat.Borders.Top.LineWidth;
float bottomBorderWidth = tableCell.CellFormat.Borders.Bottom.LineWidth;
float leftBorderWidth = tableCell.CellFormat.Borders.Left.LineWidth;
float rightBorderWidth = tableCell.CellFormat.Borders.Right.LineWidth;
//Get borders' space
float topBorderSpace = tableCell.CellFormat.Borders.Top.Space;
float bottomBorderSpace = tableCell.CellFormat.Borders.Bottom.Space;
float leftBorderSpace = tableCell.CellFormat.Borders.Left.Space;
float rightBorderSpace = tableCell.CellFormat.Borders.Right.Space;
//Get the vertical merge type of the cell.
CellMerge type =tableCell.CellFormat.VerticalMerge;
switch (type)
{
case CellMerge.None:
//This cell is not vertical merged
break;
case CellMerge.Start:
//This cell is the start cell of a vertical merging
break;
case CellMerge.Continue:
//This cell is not the start cell of a vertical merging
break;
}
//Get the grid span to determine the horizontal merging of cells.
int spanCount = tableCell.GridSpan;
if (spanCount == 1)
{
//This cell is not horizontal merged
}
else
{
//This cell is horizontally merged by "spanCount" cells
}
}
}
Document doc = new Document();
doc.LoadFromFile("table_issue.docx");
Section section = doc.Sections[0];
Table table = section.Tables[2] as Table;
//Apply the table style
table.ApplyTableStyle();
//Get table's border width
float lineWidth = table.TableFormat.Borders.Left.LineWidth;
//Get table's border type
BorderStyle borderType = table.TableFormat.Borders.Left.BorderType;
//Get table's cell space
object cellSpace = table.TableFormat.CellSpacing;
//Get table's border colors
Color colorTopBorder = table.TableFormat.Borders.Top.Color;
Color colorBottomBorder = table.TableFormat.Borders.Bottom.Color;
Color colorLeftBorder = table.TableFormat.Borders.Left.Color;
Color colorRightBorder = table.TableFormat.Borders.Right.Color;
foreach (TableRow tableRow in table.Rows)
{
//Get row's height
float rowHeight = tableRow.Height;
int row = table.Rows.IndexOf(tableRow);
foreach (TableCell tableCell in tableRow.Cells)
{
int column = tableRow.Cells.IndexOf(tableCell);
//Get cell's width
Console.WriteLine("Cell's width: " + tableCell.Width);
//Get cell's background color
Color backColor = tableCell.CellFormat.BackColor;
//Get cell's border color
Color topBorderColor = tableCell.CellFormat.Borders.Top.Color;
Color bottomBorderColor = tableCell.CellFormat.Borders.Bottom.Color;
Color leftBorderColor = tableCell.CellFormat.Borders.Left.Color;
Color rightBorderColor = tableCell.CellFormat.Borders.Right.Color;
//Get borders' width
float topBorderWidth = tableCell.CellFormat.Borders.Top.LineWidth;
float bottomBorderWidth = tableCell.CellFormat.Borders.Bottom.LineWidth;
float leftBorderWidth = tableCell.CellFormat.Borders.Left.LineWidth;
float rightBorderWidth = tableCell.CellFormat.Borders.Right.LineWidth;
//Get borders' space
float topBorderSpace = tableCell.CellFormat.Borders.Top.Space;
float bottomBorderSpace = tableCell.CellFormat.Borders.Bottom.Space;
float leftBorderSpace = tableCell.CellFormat.Borders.Left.Space;
float rightBorderSpace = tableCell.CellFormat.Borders.Right.Space;
//Get the vertical merge type of the cell.
CellMerge type = tableCell.CellFormat.VerticalMerge;
switch (type)
{
case CellMerge.None:
//This cell is not vertical merged
break;
case CellMerge.Start:
//This cell is the start cell of a vertical merging
break;
case CellMerge.Continue:
//This cell is not the start cell of a vertical merging
break;
}
//Get the grid span to determine the horizontal merging of cells.
int spanCount = tableCell.GridSpan;
if (spanCount == 1)
{
//This cell is not horizontal merged
}
else
{
//This cell is horizontally merged by "spanCount" cells
}
//Gets the text direction in the cell.
TextDirection textDirection = tableCell.CellFormat.TextDirection;
//Gets the padding in the cell.
float left = tableCell.CellFormat.Paddings.Left;
//Gets the vertical alignment of text in a cell.
VerticalAlignment verticalAlignment = tableCell.CellFormat.VerticalAlignment;
foreach ( DocumentObject documentObject in tableCell.ChildObjects)
{
if(documentObject.DocumentObjectType == DocumentObjectType.Paragraph)
{
Paragraph paragraph = documentObject as Paragraph;
foreach (DocumentObject documentObject2 in paragraph.ChildObjects)
{
if (documentObject.DocumentObjectType == DocumentObjectType.TextRange)
{
TextRange textRange = documentObject as TextRange;
//Get font size
float fontSize = textRange.CharacterFormat.FontSize;
//Get font name
string fontName = textRange.CharacterFormat.FontName;
}
}
}
}
}
}