Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Aug 25, 2021 3:31 am

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!!

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Wed Aug 25, 2021 4:52 am

Adding another point on the table style.

When a table has applied with any inbuilt table style, then the cell background color and border color are not getting recognized by cellFormat() method. They come as 0 0 0 in RGB format.

Let me know if that colors can be extracted, so that we can create our own HTML table with those colors and styles after extracting the table content.

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Wed Aug 25, 2021 11:15 am

Hello,

Thanks for your inquiry.
Please refer to the following code to meet your needs.
Code: Select all
            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);

If this cannot meet your needs, please provide your input file. You can upload them here or send them to us (support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Thu Aug 26, 2021 3:37 am

Hi Brian,

Unfortunately, the given did not work for me. Sample file will be shared via email.

I get table style name as per the code, but when I read the cell color and border color, I get below:

Cell Back Color: java.awt.Color[r=0,g=0,b=0]
Cell Border Top Color: java.awt.Color[r=0,g=0,b=0]

Please refer the third table in the sample file which has table design as "Grid Table 5 Dark Accent 5".

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Thu Aug 26, 2021 10:37 am

Hello,

Thanks for providing more details via email.
After further investigation and analyzation, we found you need to apply the table style before getting the background color and border color.
Code: Select all
            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);


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Mon Oct 04, 2021 11:08 am

Hi Team

Please let me know that is there any possibilities to extract the border style of table and cell .

-Border color
-border height
-border width
-border-collapse
-border-spacing etc

Thank you

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Tue Oct 05, 2021 10:23 am

Hello,

Thanks for your reply.
Please refer to the following sample code to test. Regarding the "border height" you mentioned, do you mean the height of the cell? If I misunderstood something, just feel free to write back.

Code: Select all
    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
            }
        }
    }
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed Oct 06, 2021 4:38 am

Thank you Andy

This is my requirements mentioned below.

//table
<table style="border: 1px solid black;border-collapse: collapse;">
//Cell
<td style="border:1px solid black;width:176.95px;background-color:rgb(217,226,243);font-size:11.0;font-family:'Calibri';padding:0.0px 5.4px;" align="left" valign="top" >Name<br></td>

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Wed Oct 06, 2021 8:00 am

Hello,

Thanks for providing more details.
Please refer to the following modified code. If there are any other issues related to our products, please free to contact us.
Code: Select all
            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;
                                }
                            }
                        }
                    }
                }
            }


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Mon Oct 11, 2021 10:35 am

Hi

Is it possible to extract table style as shown below

<td style="border:1px solid black/>

Thanks

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Tue Oct 12, 2021 3:53 am

Hello,

Thanks for your response.
Kindly note that our Spire.Doc is based on Microsoft Word. And in Microsoft Word, the type, width and color of the table border are different attributes. I'm sorry that there is no way to get it like html.

Sincerely,
Brian
E-iceblue
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc