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.

Sat Mar 21, 2020 4:55 pm

Hello:
For a long paragraph of text, how to make it Times New Roman in English and Bold in Chinese?
And,The first line is indented to 2 characters,Unit is not centimeter.How to set it?

Liyuanyuan
 
Posts: 13
Joined: Tue Mar 17, 2020 1:12 pm

Mon Mar 23, 2020 6:48 am

Hello,

Thanks for your inquiry.
Regarding your first issue, you could set the font of all paragraphs to "Times new Roman", and then find all Chinese characters through regular expression and set them to bold. Below is the corresponding code for reference.
Code: Select all
            Document document = new Document();
            document.LoadFromFile("style.docx");

            //Create a new style and set font
            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = "Mystyle";
            style.CharacterFormat.FontName = "Times New Roman";
            document.Styles.Add(style);

            //Apply style
            foreach (Section s in document.Sections)
            {
                foreach (Paragraph p in s.Paragraphs)
                {
                    p.ApplyStyle(style.Name);
                }
            }

            //Find all Chinese character
            Regex regex = new Regex(@"[\u4e00-\u9fa5]+");
            TextSelection[] textSelections = document.FindAllPattern(regex);
            foreach (TextSelection selection in textSelections)
            {
                TextRange range = selection.GetAsOneRange();
                //Set bold
                range.CharacterFormat.Bold = true;
            }

            string filePath = "result.docx";
            document.SaveToFile(filePath, FileFormat.Docx);

As for setting the first line indent to 2 characters, please refer to the following code.
Code: Select all
            Document document = new Document();
            Section sec = document.AddSection();
            Paragraph paragraph = sec.AddParagraph();
            TextRange tr = paragraph.AppendText("Sets the value that represents the first line indent for paragraph.");
            //Get the font size
            int size = (int)tr.CharacterFormat.FontSize;
            //Set the first line indent to 2 characters
            paragraph.Format.SetFirstLineIndent(2 * size);           
            string filePath = "result.docx";
            document.SaveToFile(filePath, FileFormat.Docx);

If there is any question, please provide your input file as well as your desired output, then we will do further investigation. You could upload here or send them to us(support@e-iceblue.com) via email.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Mar 27, 2020 10:04 am

Hello,

Greetings from E-iceblue.
Did my code help you? Could you please give us some feedback at your convenience?
Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Apr 03, 2020 11:17 am

Thank you for your help,it can solve my problems.

Liyuanyuan
 
Posts: 13
Joined: Tue Mar 17, 2020 1:12 pm

Mon Apr 06, 2020 2:13 am

Hi,

Thanks for your feedback.
We are glad to hear from you that it helps.
Feel free to contact us if you need further assistance.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri May 08, 2020 7:55 am

Only one section is set to distinguish Chinese and English formats, not doc

Liyuanyuan
 
Posts: 13
Joined: Tue Mar 17, 2020 1:12 pm

Fri May 08, 2020 9:35 am

Hello,

Thanks for your post.
I simulated a Word file with multiple sections and then tested it, but didn't encounter the issue you mentioned. To help us investigate your issue more accurately and quickly, please share your input file with us. You could upload it here or send it to us(support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Sat May 09, 2020 5:56 am

In the three sections of a doc, I want only set section two,The other sections are not set.

Liyuanyuan
 
Posts: 13
Joined: Tue Mar 17, 2020 1:12 pm

Sat May 09, 2020 8:12 am

//Find all Chinese character
Regex regex = new Regex(@"[\u4e00-\u9fa5]+");
TextSelection[] textSelections = document.FindAllPattern(regex);
foreach (TextSelection selection in textSelections)
{
TextRange range = selection.GetAsOneRange();
//Set bold
range.CharacterFormat.Bold = true;
}
// TextSelection[] textSelections = document.FindAllPattern(regex);
In the three sections of a doc, I want only set section two,The other sections are not set.
How to set "section.findAllPattern(regex)"

Liyuanyuan
 
Posts: 13
Joined: Tue Mar 17, 2020 1:12 pm

Sat May 09, 2020 10:03 am

Hello,

Thanks for your inquiry.
Sorry there isn't "section.findAllPattern(regex)" to find text. However, you can refer to the following sample code to achieve the demand.
Code: Select all
 //load document
 Document document = new Document();
 document.LoadFromFile("test.docx");
 //get the second section
 Section section = document.Sections[1];
 //find all Chinese character
 Regex regex = new Regex(@"[\u4e00-\u9fa5]+");
 TextSelection[] textSelections = document.FindAllPattern(regex);
 //Bold the specified section
 foreach (TextSelection selection in textSelections)
 {
     TextRange range = selection.GetAsOneRange();
     Paragraph para = range.OwnerParagraph;
     Section findSection = para.Owner.Owner as Section;
     //Determine if it is specified section
     if (findSection == section)
     {
         range.CharacterFormat.Bold = true;
     }
 }
 string filePath = "result.docx";
 document.SaveToFile(filePath, FileFormat.Docx);

If there is still any question, please provide your input file, then we will do further investigation. You could upload here or send it to us via email(support@e-iceblue.com).

Sincerely,
Sara
E-iceblue support team
User avatar

Sara.Yang
 
Posts: 33
Joined: Wed May 06, 2020 1:05 am

Wed May 13, 2020 9:23 am

Hello,

Greetings from E-iceblue.
How is your issue now? Could you please give us some feedback at your convenience?
Thanks in advance.

Sincerely,
Sara
E-iceblue support team
User avatar

Sara.Yang
 
Posts: 33
Joined: Wed May 06, 2020 1:05 am

Return to Spire.Doc