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.

Thu Mar 31, 2022 9:15 am

Hi Team,

Please let me know is it possible to extract text along with style.

I have attached sample file for your reference.

Thanks in advance

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

Fri Apr 01, 2022 10:40 am

Hi,

Thanks for your inquiry.
Please refer to the code below to extract text with its style. If there is any question, please feel free to contact us.
Code: Select all
//Load document from disk
Document doc = new Document();
doc.LoadFromFile(path);
//Loop through section
foreach (Section section in doc.Sections)
{
    //Loop through paragraph in section
    foreach (Paragraph paragraph in section.Paragraphs)
    {
        //Loop through element in paragraph
        foreach (DocumentObject docObject in paragraph.ChildObjects)
        {
            if (docObject is TextRange)
            {
                TextRange tr = docObject as TextRange;
                //Get text
                string text = tr.Text;
                //Get CharacterFormat
                CharacterFormat format = tr.CharacterFormat;
                //Get text color
                Color textColor = format.TextColor;
                //Get Bold
                bool isBold = format.Bold;
                //Get highlight color
                Color highlightColor = format.HighlightColor;
            }
        }
    }
}

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Wed Apr 13, 2022 2:31 am

Hi Team ,

Thank you for your response.

I am able to fetch text's style ,Please let me know how to add value of text style in html tag .

//my code
TextRange textRange = (TextRange) childObj;
Color highligtedColor = textRange.getCharacterFormat().getHighlightColor();
Color textColor = textRange.getCharacterFormat().getTextColor();
boolean isBold = textRange.getCharacterFormat().getBold();
boolean isItalic = textRange.getCharacterFormat().getItalic();
Double fontSize = (double) textRange.getCharacterFormat().getFontSize();
text.append("<span style=\"color: " + ColorUtils.getColorNameFromColor(textColor) + ";");
if (isBold) text.append("font-weight:bold; ");
if (isItalic) text.append("font-style: italic; ");
text.append("font-size: "+Math.round(fontSize)+"pt;");
text.append(" \">");
text.append("<span style=\"background-color: " + ColorUtils.getColorNameFromColor(highligtedColor)+ ";\">" + "text"+ "</span>");
text.append(" </span>");
}

Thanks in advance

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

Wed Apr 13, 2022 9:11 am

Hi,

Thank you for your reply,
Our Spire.Doc is used to manipulate word documents and it does not provide a method to add value of text style in html tag like your code, but we provide the feature to convert Word to Html file directly, you can refer to this document: Java: Convert Word to HTML.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Fri Jul 01, 2022 2:50 am

Hi,

Hope you are doing well!
Has your problem been effectively solved now? Could you give us some feedback at your convenience?
Thanks in advance.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Return to Spire.Doc