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 Sep 04, 2024 5:58 am

Hi..

I want to loop through all sections, all characters in a document, and set the font to them.

When it's English, I want to process it using font A,
when it's numbers, I want to process it using font B,
and when it's another language, I want to process it using font C.

The problem is that when accessing TextRange, I can only specify the font for the entire TextRange,
and I don't know how to specify the font for each Char within the TextRange. How should I handle this?


Code: Select all
        private void SetFontsForNumberAndEnglish(Document doc) {
            foreach (Section section in doc.Sections) {
                foreach (Paragraph para in section.Paragraphs) {
                    SetFontsForParagraph(para);
                }
                foreach (Table table in section.Tables) {
                    foreach (TableRow row in table.Rows) {
                        foreach (TableCell cell in row.Cells) {
                            foreach (Paragraph para in cell.Paragraphs) {
                                SetFontsForParagraph(para);
                            }
                        }
                    }
                }
            }
        }
 
        private void SetFontsForParagraph(Paragraph para) {
            foreach(DocumentObject item in para.ChildObjects) {
                if(item.DocumentObjectType == DocumentObjectType.TextRange) {
                    TextRange range = item as TextRange;
                    if (range.Text != null) {
                        for (int i = 0; i < range.Text.Length; i++) {
                            char c = range.Text[i];
                           //How to access char font
                            if (IsNumber(c)) {
                                range.CharacterFormat.FontName = "Helvetica";
                            } else if (IsEnglish2(c)) {
                                range.CharacterFormat.FontName = "Arial";
                            }else ...
                        }
                    }
                }
            }
        }

Sungkyu_min
 
Posts: 35
Joined: Fri Jan 07, 2022 5:53 am

Thu Sep 05, 2024 3:32 am

Hello,

Thanks for your inquiry.For your situation, We recommend using regular expressions to match characters in documents. I have attached my test code for your reference.If you have any other questions, please feel free to write to me.

Code: Select all
 Document doc = new Document();
            doc.LoadFromFile(@"in.docx");
            //Match English
            Regex regex = new Regex("[A-Za-z]+");
            TextSelection[] text = doc.FindAllPattern(regex);
            //Create a characterFormat object
            CharacterFormat format = new CharacterFormat(doc);
            //Set font
            format.Font = new Font("Arial", 16);
            foreach (TextSelection seletion in text)
            {
                seletion.GetAsOneRange().ApplyCharacterFormat(format);
                seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Green;
            }
            //Matching Number
            regex = new Regex(@"-?\d+");
            text = doc.FindAllPattern(regex);
            //Create a characterFormat object
            format = new CharacterFormat(doc);
            //Set font
            format.Font = new Font("Times New Roman", 20);
            foreach (TextSelection seletion in text)
            {
                seletion.GetAsOneRange().ApplyCharacterFormat(format);
                seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Red;
            }
            doc.SaveToFile(@"out.docx", FileFormat.Docx);

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 277
Joined: Mon Jul 15, 2024 5:40 am

Thu Sep 05, 2024 4:13 am

There was a simpler way. The problem has been solved.
Thank you always. Have a nice day.

Sungkyu_min
 
Posts: 35
Joined: Fri Jan 07, 2022 5:53 am

Thu Sep 05, 2024 5:53 am

Hello

Thanks for your reply.
I'm glad to hear this news. If you have any other questions, please feel free to write to me.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 277
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.Doc