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.

Sun Dec 15, 2019 8:43 am

Hi,

I'm wondering if we can remove underline from all anchor tags in Word document.

I'm appending text from database column to a paragraph using AppendHtml (We have multiple styles depending on column) and can't apply Underline style for entire paragraph as they might have underlined few topics intentionally.

I need underline removed only for the anchor tags. Could you please help me how I can achieve this?

In word, you usually use <BODY> tag: <STYLE>A {text-decoration: none;} </STYLE> this in Code tab.


Thanks,
Santhoshi.

santuvssantu
 
Posts: 98
Joined: Fri Feb 12, 2016 5:21 pm

Mon Dec 16, 2019 10:11 am

Hi,

Thanks for your inquiry.
To help us investigate your issue accurately, please offer us the following information.
1. Your input Word file(if any).
2. The complete code you were using which could reproduce your issue directly.
3. Your desired result file.

You could upload them here or send us(support@e-iceblue.com) via email.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Dec 16, 2019 1:15 pm

TestFiles.zip

Hi Amber,

Below is my sample code. jsonData here has some text which contains <a> tags.

var text = dataRow.Cells[1].AddParagraph();
text.ApplyStyle(DocumentStyles.CustomText);
text.AppendHTML(jsonData); //jsonData is my database column
text.Format.TextAlignment = TextAlignment.Top;

Attached is my full word/pdf. I want to have the underline removed for the hyperlinks/anchor tags programmatically.

Is this acheivable?

Thanks,
Santhoshi.

santuvssantu
 
Posts: 98
Joined: Fri Feb 12, 2016 5:21 pm

Tue Dec 17, 2019 10:47 am

Hi,

Thanks for your information.
Please use the property textRange.CharacterFormat.UnderlineStyle to change the underline setting. Below is the code for removing the underline for all hyperlinks in your file.

Code: Select all
            var text = dataRow.Cells[1].AddParagraph();
            text.ApplyStyle(DocumentStyles.CustomText);
            text.AppendHTML(jsonData); //jsonData is my database column
            text.Format.TextAlignment = TextAlignment.Top;

            //traverse your table and remove the underline from it
            foreach (Section s in doc.Sections)
            {
                foreach (var obj in s.Tables)
                {
                    Table table = obj as Table;
                    foreach (TableRow row in table.Rows)
                    {
                        foreach (TableCell cell in row.Cells)
                        {
                            foreach (DocumentObject cobj in cell.ChildObjects)
                            {
                                if (cobj is Paragraph)
                                {
                                    Paragraph par = cobj as Paragraph;
                                    foreach (DocumentObject ttr in par.ChildObjects)
                                    {
                                        if (ttr is TextRange)
                                        {
                                            TextRange tr = ttr as TextRange;
                                            tr.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
                                        }
                                    }
                                }

                                //Find embedded table and remove underline from it
                                if (cobj is Table)
                                {
                                    Table tableC = cobj as Table;
                                    foreach (TableRow rowc in tableC.Rows)
                                    {
                                        foreach (TableCell cellc in rowc.Cells)
                                        {
                                            foreach (DocumentObject cobjc in cellc.ChildObjects)
                                            {
                                                if (cobjc is Paragraph)
                                                {
                                                    Paragraph par = cobjc as Paragraph;
                                                    foreach (DocumentObject ttrc in par.ChildObjects)
                                                    {
                                                        if (ttrc is TextRange)
                                                        {
                                                            TextRange trc = ttrc as TextRange;
                                                            trc.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            doc.SaveToFile("result.docx", FileFormat.Docx);   


Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Tue Dec 17, 2019 12:24 pm

Hi Amber,
Thanks for your response. What if my textRange has some anchor tags and other texts which has underlines? This will remove underline altogether correct? I want the underline to be removed only for <a> tags.

Regards,
Santhoshi.

santuvssantu
 
Posts: 98
Joined: Fri Feb 12, 2016 5:21 pm

Wed Dec 18, 2019 9:48 am

Hi Santhoshi,

You could find the hyperlinks firstly, then only change the TextRange in the hyperlinks. Below is code sample for your kind reference:
Code: Select all
            text.AppendHTML(jsonData); //jsonData is my database column
            ......
            List<TextRange> textRanges = new List<TextRange>();

            foreach (Section s in doc.Sections)
            {
                foreach (var obj in s.Tables)
                {
                    Table table = obj as Table;
                    foreach (TableRow row in table.Rows)
                    {
                        foreach (TableCell cell in row.Cells)
                        {
                            foreach (DocumentObject cobj in cell.ChildObjects)
                            {
                                if (cobj is Paragraph)
                                {
                                    Paragraph par = cobj as Paragraph;
                                    //foreach (DocumentObject obb in par.ChildObjects)
                                    //{
                                    //    if (obb is TextRange)
                                    //    {
                                    //        TextRange ttrr = obb as TextRange;
                                    //        ttrr.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
                                    //    }
                                    //}

                                    Field cField = null;
                                    for (int a = 0; a < par.ChildObjects.Count; a++)
                                    {
                                        DocumentObject objj = par.ChildObjects[a];

                                        if (objj is Field)
                                        {
                                            Field field = objj as Field;
                                            if (field.Type == FieldType.FieldHyperlink)
                                            {
                                                cField = field;
                                            }
                                        }
                                        else if (cField != null && objj is FieldMark && (objj as FieldMark).Type == FieldMarkType.FieldEnd && cField.End == objj)
                                        {
                                            int fieldEnd = par.ChildObjects.IndexOf(objj);
                                            if (par.ChildObjects[fieldEnd - 1] is TextRange)
                                            {
                                                textRanges.Add(par.ChildObjects[fieldEnd - 1] as TextRange);
                                            }

                                        }
                                    }
                                }

                                //Find embedded table
                                if (cobj is Table)
                                {
                                    Table tableC = cobj as Table;
                                    foreach (TableRow rowc in tableC.Rows)
                                    {
                                        foreach (TableCell cellc in rowc.Cells)
                                        {
                                            foreach (DocumentObject cobjc in cellc.ChildObjects)
                                            {
                                                if (cobjc is Paragraph)
                                                {
                                                    Paragraph par = cobjc as Paragraph;

                                                    //Below code is for finding the hyperlink
                                                    Field cField = null;
                                                    for (int a = 0; a < par.ChildObjects.Count; a++)
                                                    {
                                                        DocumentObject objjj = par.ChildObjects[a];

                                                        if (objjj is Field)
                                                        {
                                                            Field field = objjj as Field;
                                                            if (field.Type == FieldType.FieldHyperlink)
                                                            {
                                                                cField = field;
                                                            }
                                                        }
                                                        else if (cField != null && objjj is FieldMark && (objjj as FieldMark).Type == FieldMarkType.FieldEnd && cField.End == objjj)
                                                        {
                                                            int fieldEnd = par.ChildObjects.IndexOf(objjj);
                                                            if (par.ChildObjects[fieldEnd - 1] is TextRange)
                                                            {
                                                                //find the textrange in hyperlink
                                                                textRanges.Add(par.ChildObjects[fieldEnd - 1] as TextRange);
                                                            }

                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            for (int b = 0; b < textRanges.Count; b++)
            {
                //change the underline setting
                textRanges[b].CharacterFormat.UnderlineStyle = UnderlineStyle.None;
            }

            doc.SaveToFile("19939.docx", FileFormat.Docx);


If you still have the issue, please provide your HTML.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Dec 19, 2019 7:54 am

Hi Betsy,

Thanks for your code, I tried this but with no luck..

I'm attaching the files here. When I debug the code, there are no textranges found to change the underlines/fonts.

I've one more issue with Arabic links here, if you see the "الموقع الشبكي" it displays correct in Word but not in pdf.

Links-Arabic and English.zip



Thanks,
Santhoshi.

santuvssantu
 
Posts: 98
Joined: Fri Feb 12, 2016 5:21 pm

Thu Dec 19, 2019 11:23 am

Hi,

Thanks for your reply.
For the first issue(remove the underline of <a> tags from your Word file), as far as I know there are <a> tag and <u> tag which will add the underline for text in Html. Spire.Doc analysis <a> tag as a hyperlink. I tested your new file with Spire.Office Platinum (DLL Only) Version:4.11.2, the list TextRange is not empty and the underline could be removed successfully in the result file. Below is my code.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"..\ Journal_2019-12-16_AR.docx");

            List<TextRange> textRanges = new List<TextRange>();

            foreach (Section s in doc.Sections)
            {
                foreach (var obj in s.Tables)
                {
                    Table table = obj as Table;
                    foreach (TableRow row in table.Rows)
                    {
                        foreach (TableCell cell in row.Cells)
                        {
                            foreach (DocumentObject cobj in cell.ChildObjects)
                            {
                                if (cobj is Paragraph)
                                {
                                    Paragraph par = cobj as Paragraph;

                                    Field cField = null;
                                    for (int a = 0; a < par.ChildObjects.Count; a++)
                                    {
                                        DocumentObject objj = par.ChildObjects[a];

                                        if (objj is Field)
                                        {
                                            Field field = objj as Field;
                                            if (field.Type == FieldType.FieldHyperlink)
                                            {
                                                cField = field;
                                            }
                                        }
                                        else if (cField != null && objj is FieldMark && (objj as FieldMark).Type == FieldMarkType.FieldEnd && cField.End == objj)
                                        {
                                            int fieldEnd = par.ChildObjects.IndexOf(objj);
                                            if (par.ChildObjects[fieldEnd - 1] is TextRange)
                                            {
                                                textRanges.Add(par.ChildObjects[fieldEnd - 1] as TextRange);
                                            }
                                        }
                                    }
                                }

                                //Find embedded table
                                if (cobj is Table)
                                {
                                    Table tableC = cobj as Table;
                                    foreach (TableRow rowc in tableC.Rows)
                                    {
                                        foreach (TableCell cellc in rowc.Cells)
                                        {
                                            foreach (DocumentObject cobjc in cellc.ChildObjects)
                                            {
                                                if (cobjc is Paragraph)
                                                {
                                                    Paragraph par = cobjc as Paragraph;

                                                    //Below code is for finding the hyperlink
                                                    Field cField = null;
                                                    for (int a = 0; a < par.ChildObjects.Count; a++)
                                                    {
                                                        DocumentObject objjj = par.ChildObjects[a];

                                                        if (objjj is Field)
                                                        {
                                                            Field field = objjj as Field;
                                                            if (field.Type == FieldType.FieldHyperlink)
                                                            {
                                                                cField = field;
                                                            }
                                                        }
                                                        else if (cField != null && objjj is FieldMark && (objjj as FieldMark).Type == FieldMarkType.FieldEnd && cField.End == objjj)
                                                        {
                                                            int fieldEnd = par.ChildObjects.IndexOf(objjj);
                                                            if (par.ChildObjects[fieldEnd - 1] is TextRange)
                                                            {
                                                                //find the textrange in hyperlink
                                                                textRanges.Add(par.ChildObjects[fieldEnd - 1] as TextRange);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            for (int b = 0; b < textRanges.Count; b++)
            {
                //change the underline setting
                textRanges[b].CharacterFormat.UnderlineStyle = UnderlineStyle.None;
            }

            doc.SaveToFile("result.docx", FileFormat.Docx);


If you still have the issue, could you please offer us your input Html and your desired result file for further investigation? You could upload them here or send us(support@e-iceblue.com) via email.

About the conversion issue, I have reproduced it and logged it into our bug tracking system. Once there is any progress, we will inform you. Apologize for the inconvenience caused.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Thu Dec 19, 2019 11:37 am

I'm using Spire.doc latest hotpack(nuget) as we are planning for Spire.doc Pro license.
Will that make any difference?

santuvssantu
 
Posts: 98
Joined: Fri Feb 12, 2016 5:21 pm

Fri Dec 20, 2019 1:55 am

Hi Santhoshi,

Thanks for your reply.
It's both ok for you to use Spire.Doc or Spire.Office in your project. I have attached my project here, please download and test it from the following link.
http://www.e-iceblue.com/downloads/demo/Test19939.zip

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.Doc