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.

Sun May 29, 2016 4:33 pm

I'm extending the Document class and adding some custom styles. One of these is the following:

Code: Select all
class MPDocument : Spire.Doc.Document
{
    public MPDocument()
    {
        ...
        ParagraphStyle style2 = new ParagraphStyle(this);
        style2.ApplyBaseStyle(BuiltinStyle.Heading2);
        style2.Name = CustomStyles.MPHeading2.ToString();
   
        style2.CharacterFormat.Bold = false;
        style2.CharacterFormat.Italic = false;
        style2.CharacterFormat.TextColor = Color.FromArgb(46, 116, 181);
   
        this.Styles.Add(style2); 
    }


I have to apply a base style of Heading2 so that the table of contents properly reflects paragraphs using the custom style.
The problem I'm having is that even though I'm setting `Bold` and `Italic` to false, the paragraph still comes out in both bold and italics.

I've also tried to explicitly remove bold after setting the style, yet the text still remains bold (the same occurs for italics):

Code: Select all
...
Paragraph subHeader = section.AddParagraph();
TextRange tr = subHeader.AppendText("Some header text");
subHeader.ApplyStyle(CustomStyles.MPHeading2);
tr.CharacterFormat.Bold = false;


It seems like the base style is overriding the bold and italic properties. If I don't apply a base style of Heading2 then it works fine, but my headers won't show in the table of contents so I can't solve it by avoiding that, unless there's another way to achieve the same effect which I'm unaware of.

kompiuter
 
Posts: 3
Joined: Sun May 29, 2016 4:28 pm

Mon May 30, 2016 4:21 am

Hello,

Thank you for kind feedback.
Please refer to the below code:
Code: Select all
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph subHeader = section.AddParagraph();

            ParagraphStyle style2 = new ParagraphStyle(document);
            style2.ApplyBaseStyle(BuiltinStyle.Heading2);

            style2.CharacterFormat.Bold = false;
            style2.CharacterFormat.Italic = false;
            style2.CharacterFormat.TextColor = Color.FromArgb(46, 116, 181);

            document.Styles.Add(style2);

            subHeader.AppendText("Some header text");
            subHeader.ApplyStyle(style2.Name);

            document.SaveToFile("Heading.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("Heading.docx");


Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Mon May 30, 2016 8:29 am

Hello Caroline, even with your code it is still coming out as bold & italics:

Image

kompiuter
 
Posts: 3
Joined: Sun May 29, 2016 4:28 pm

Mon May 30, 2016 9:13 am

Hi,

Thank you for your feedback further.
I use the latest version Spire.Doc Pack Version:5.7, and it works fine (see the below attachment). Please try the version.

Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Mon May 30, 2016 4:24 pm

It is working with 5.7 indeed Caroline, but that's the trial version.

I am using FreeSpire.Doc using NuGet, will that be updated?

kompiuter
 
Posts: 3
Joined: Sun May 29, 2016 4:28 pm

Tue May 31, 2016 1:12 am

Hello,

Sorry that the free version of Spire.Doc brought the inconvenience to you. At present, we don't plan to update the version.

Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Return to Spire.Doc