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.

Mon Oct 03, 2011 5:52 pm

Hello.

1) Is there a way to create my own style from code and apply it to selected text ?
2) When I open an existing doc file, is there a way to check what style is used in selected text ?

Best regards,
Greg

g.musial
 
Posts: 3
Joined: Tue Sep 27, 2011 6:07 am

Sat Oct 08, 2011 2:30 am

Hello,Greg

Sorry for the late reply and thank you for your patience with our reply.
If you want to apply your own styles from code to the selected text, you can try the following codes:
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"..\..\Blank.docx", FileFormat.Docx);

            Section section = doc.Sections[0];
            Paragraph paragraph = section.AddParagraph();

            TextRange txtRange = paragraph.AppendText("hello");

            //Font name
            txtRange.CharacterFormat.FontName = "Tahoma";

            //Font size
            txtRange.CharacterFormat.FontSize = 20;

            //Underline
            txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot;

            //Change text color
            txtRange.CharacterFormat.TextColor = Color.Blue;

            doc.SaveToFile(@"Sample.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start(@"Sample.docx");

You can also check the style is used in selected text like this:
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"..\..\test.doc", FileFormat.Doc);
            for (int j = 0; j < doc.Sections.Count;j++)
            {
                for (int i = 0; i < doc.Sections[0].Paragraphs.Count; i++)
                {
                    Console.WriteLine(doc.Sections[j].Paragraphs[i].StyleName);
                }
            }
            Console.ReadKey();

If you still have any questions, please contact us.
Tina
Technical Support/Developer,
e-iceblue Support Team
User avatar

Tina.Lin
 
Posts: 152
Joined: Tue Sep 13, 2011 5:37 am

Mon Oct 10, 2011 6:50 am

Hello.

Thanks for your reply.
Your second example is very useful.

I've noticed that in code I can use only builtin styles for text formatting, like this:

Code: Select all
foreach (BuiltinStyle builtinStyle in Enum.GetValues(typeof(BuiltinStyle)))
            {
                paragraph = section.AddParagraph();
                //Append Text
                paragraph.AppendText(builtinStyle.ToString());
                //Apply Style
                paragraph.ApplyStyle(builtinStyle);
            }


But is there a way to create my own style from code and apply it to selected text ?
When I open created (in code) document I want to have my own style in styles list and use it for formatting text while editing.

Best regards,
Greg

g.musial
 
Posts: 3
Joined: Tue Sep 27, 2011 6:07 am

Tue Oct 11, 2011 4:11 am

Hello,Greg
Sorry for late reply and thank you for your patience with our reply.
You can create your own style and apply it as following code:
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"..\..\Blank.docx", FileFormat.Docx);
            Section section = doc.Sections[0];
            ParagraphStyle myStyle = new ParagraphStyle(doc);
            myStyle.Name = "myStyle";
            myStyle.ParagraphFormat.BackColor = Color.Yellow;
            myStyle.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
            doc.Styles.Add(myStyle);
            //use the style created
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("Free Style");
            paragraph.ApplyStyle(myStyle.Name);
            doc.SaveToFile(@"Sample.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start(@"Sample.docx");

You can have a try. If you still have any questions, Please don’t hesitate to contact us at any time for anything.
Have a nice day.
Tina
Technical Support/Developer,
e-iceblue Support Team
User avatar

Tina.Lin
 
Posts: 152
Joined: Tue Sep 13, 2011 5:37 am

Return to Spire.Doc

cron