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.

Tue Jul 07, 2020 2:13 pm

Hi,

I'm trying to make linked style or character style in spire doc but I can't manage on how to do it.
Can you help me ?

Have a nice week

asterakin
 
Posts: 9
Joined: Mon Apr 15, 2019 7:50 am

Wed Jul 08, 2020 5:55 am

Hello,

Thanks for your inquiry.
To help us better understand your requirements, please provide us with more information, such as your input file as well as the output you expect. Thanks in advance for your assistance.

Sincerely,
Rachel
E-iceblue support team
User avatar

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

Wed Jul 08, 2020 6:39 am

I want to have different style in the same paragraph so I want to apply it at the textrange level (run in word) as described in the following website :
https://bettersolutions.com/word/styles ... %20heading.

asterakin
 
Posts: 9
Joined: Mon Apr 15, 2019 7:50 am

Wed Jul 08, 2020 8:40 am

Hello,

Thanks for your inquiry.
Our Spire.Doc supports setting multiple styles for text in a single paragraph. The following is the code for your reference. If there is any question, please provide your desired output for our reference.
Code: Select all
            Document doc = new Document();

            //Add a section
            Section section = doc.AddSection();

            //Add a paragraph
            Paragraph para = section.AddParagraph();

            CharacterFormat cf1 = new CharacterFormat(doc);
            cf1.FontName = "Calibri";
            cf1.FontSize = 16f;
            cf1.TextColor = Color.Blue;
            cf1.Bold = true;
            cf1.UnderlineStyle = UnderlineStyle.Single;
            //Add a text range 1 and set its style
            TextRange range = para.AppendText("Spire.Doc for .NET ");
            range.ApplyCharacterFormat(cf1);

            //Add a text range 2 and set its style
            CharacterFormat cf2= new CharacterFormat(doc);
            cf2.FontName = "Calibri";
            cf2.FontSize = 15f;
            range = para.AppendText("is a professional Word .NET library");
            range.ApplyCharacterFormat(cf2);

            //Save the Word document
            string output = "MultiStylesInAParagraph_out.docx";
            doc.SaveToFile(output, FileFormat.Docx2013);


Sincerely,
Rachel
E-iceblue support team
User avatar

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

Thu Jul 09, 2020 2:11 pm

It isn't an efficient solution for me. With character style i can modify all the textrange with the same style when I open it with word. I attach a file with 2 different character style ("Style1" and "Style2").

asterakin
 
Posts: 9
Joined: Mon Apr 15, 2019 7:50 am

Fri Jul 10, 2020 3:48 am

Hello,

Thanks for your feedback.
Please refer to the following modified code. If you have further questions, just feel free to contact us.
Code: Select all
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph para = section.AddParagraph();

            //Add style 1 and set its style type to CharacterStyle
            Style ps1 = Style.CreateBuiltinStyle(BuiltinStyle.Normal, StyleType.CharacterStyle, doc) as Style;
            ps1.Name = "Style1";
            ps1.CharacterFormat.FontName = "Calibri";
            ps1.CharacterFormat.FontSize = 22f;
            ps1.CharacterFormat.Bold = true;
            ps1.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
            doc.Styles.Add(ps1);
           
            //Add a text range 1 and set its style
            TextRange range = para.AppendText("Spire.Doc for .NET ");
            range.ApplyCharacterFormat(ps1.CharacterFormat);

            //Add style 2 and set its style type to CharacterStyle
            Style ps2 = Style.CreateBuiltinStyle(BuiltinStyle.Normal, StyleType.CharacterStyle, doc) as Style;
            ps2.Name = "Style2";
            ps2.CharacterFormat.FontName = "Calibri";
            ps2.CharacterFormat.FontSize = 15f;
            ps2.CharacterFormat.Italic = true;
            doc.Styles.Add(ps2);

            //Add a text range 2 and set its style
            range = para.AppendText("is a professional Word .NET library");
            range.ApplyCharacterFormat(ps2.CharacterFormat);

            //Save the Word document
            string output = "MultiStylesInAParagraph_out.docx";
            doc.SaveToFile(output, FileFormat.Docx2013);


Sincerely,
Rachel
E-iceblue support team
User avatar

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

Wed Jul 15, 2020 11:27 am

Hello,

Hope you are doing well.
Does the code meet your needs? Looking forward to your feedback.

Sincerely,
Rachel
E-iceblue support team
User avatar

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

Return to Spire.Doc

cron