Access to characterstyle

Hi,

I'm trying to identify whether a WTextRange is in bold characters.
I can not identify it when those characters are bold because of a word character style.
When i use quickwatch to watch my WTextRange, I see that Bold is set to false but its CharacterStyle.CharacterStyleName refers to a CharacterStyle which is in Bold.

My problem is that CharacterStyle class and CharacterStyleName properties are 'Friend', therefore I can not access them through code.

Can you help me to find how can I get CharacterStyle informations ?

In advance, thank you


8 Replies

BP Bhuvaneswari P Syncfusion Team February 18, 2009 10:21 AM UTC

Hi Pierre,

Thank you for your interest in Syncfusion products.

It is possible to access the WTextRange characterStyle information by using the CharacterFormat object. Please refers to the below code snippet to access the WTextRange characterFormat.

//Assign to characterFormat class and get the class information
TextRange range = item as WTextRange;
WCharacterFormat characterformat = range.CharacterFormat;
MessageBox.Show(characterformat.Bold.ToString());

//Direct access
MessageBox.Show(range.CharacterFormat.Bold.ToString ());

In DocIO everything is Section, Paragraph based. So you can access and check the text character by enumerating each section, paragraph and its item. Please use the below code snippet to do so:


WordDocument document = new WordDocument(@"..\..\charstyle.doc");
foreach (Entity ent in document.ChildEntities)
{
if (ent is WSection)
{
WSection section = ent as WSection;
foreach (WParagraph paragraph in section.Body.Paragraphs)
{
foreach (ParagraphItem item in paragraph.Items)
{
if (item.EntityType == EntityType.TextRange)
{
WTextRange range = item as WTextRange;
WCharacterFormat characterformat = range.CharacterFormat;
MessageBox.Show(characterformat.Bold.ToString());

MessageBox.Show(range.CharacterFormat.Bold.ToString ());

}
}
}
}
}


Please try this and let us know if this helps you.

Best Regards,
Bhuvana




PC Pierre CHAUSSEE February 18, 2009 11:11 AM UTC

Hi Bhuvana,
And thank you for your answer.

The thing is that Bold property does not show if my textrange is really in bold characters.
For example if in your Word document you ahve specified a Paragraph Style in Bold. The Bold property of the text range is set to false.

That means you have to check both paragraph Style.CharacterFormat.Bold and WTextRange.CharacterFormat.Bold property.

The problem is that in my case I do not have a paragraph style defined in Word but a CharacterStyle. And I can't see how to access that character style.

You'll see in the attachment a word document with a character style defined. You'll notice that when I read it with DocIO that both
Paragraph Style.Bold and WTextRange.Bold are set to false.

How can I access the CharacterStyle since that class is friend in DocIO ?



>Hi Pierre,

Thank you for your interest in Syncfusion products.

It is possible to access the WTextRange characterStyle information by using the CharacterFormat object. Please refers to the below code snippet to access the WTextRange characterFormat.

//Assign to characterFormat class and get the class information
TextRange range = item as WTextRange;
WCharacterFormat characterformat = range.CharacterFormat;
MessageBox.Show(characterformat.Bold.ToString());

//Direct access
MessageBox.Show(range.CharacterFormat.Bold.ToString ());

In DocIO everything is Section, Paragraph based. So you can access and check the text character by enumerating each section, paragraph and its item. Please use the below code snippet to do so:


WordDocument document = new WordDocument(@"..\..\charstyle.doc");
foreach (Entity ent in document.ChildEntities)
{
if (ent is WSection)
{
WSection section = ent as WSection;
foreach (WParagraph paragraph in section.Body.Paragraphs)
{
foreach (ParagraphItem item in paragraph.Items)
{
if (item.EntityType == EntityType.TextRange)
{
WTextRange range = item as WTextRange;
WCharacterFormat characterformat = range.CharacterFormat;
MessageBox.Show(characterformat.Bold.ToString());

MessageBox.Show(range.CharacterFormat.Bold.ToString ());

}
}
}
}
}


Please try this and let us know if this helps you.

Best Regards,
Bhuvana






BP Bhuvaneswari P Syncfusion Team February 19, 2009 02:18 PM UTC

Hi Pierre,

Thanks for the update.

I am working on this issue and update the details by tomorrow.

Best Regards
Bhuvana



BP Bhuvaneswari P Syncfusion Team February 20, 2009 10:40 AM UTC

Hi Pierre,

Thanks for the details and I couldn't find any attachment in your last update.

Accessing the CharacterStyle can be possible by getting the document styles list by using the document.Styles property and we can check corresponding type. I am afraid that I am unable to reproduce the issue while accessing through document.Styles and using WtTextRnage CharacterFormat, it returning the correct value for Bold property. Please refers to the below code to access the format through styles.


IStyleCollection col = document.Styles;
foreach (Style st in col)
{
if (st.StyleType == StyleType.CharacterStyle)
{
WCharacterFormat ch = st.CharacterFormat;
//To change the style
//ch.TextBackgroundColor = Color.Red;
Console.WriteLine("Style Type = " + st.StyleType.ToString() + " Bold= " + ch.Bold.ToString() );
}
}

Here is the sample for both the methods:

http://www.syncfusion.com/uploads/redirect.aspx?file=Character_Style1_a40b4f6e.zip&team=support


Please have a look into the sample and could you please send us the modified one to reproduce the issue and to investigate further on this issue?

Best Regards,
Bhuvana



PC Pierre CHAUSSEE February 20, 2009 03:45 PM UTC

Hi Bhuvana,

Thanks for the answer, I'm surprised you get correct values with the word document I did provide you, since I don't but anyway thanks for the method that allows to browse document.Styles
The only thing is :
How do I know which Character Style is used on my WTextRange ?

>Hi Pierre,

Thanks for the details and I couldn't find any attachment in your last update.

Accessing the CharacterStyle can be possible by getting the document styles list by using the document.Styles property and we can check corresponding type. I am afraid that I am unable to reproduce the issue while accessing through document.Styles and using WtTextRnage CharacterFormat, it returning the correct value for Bold property. Please refers to the below code to access the format through styles.


IStyleCollection col = document.Styles;
foreach (Style st in col)
{
if (st.StyleType == StyleType.CharacterStyle)
{
WCharacterFormat ch = st.CharacterFormat;
//To change the style
//ch.TextBackgroundColor = Color.Red;
Console.WriteLine("Style Type = " + st.StyleType.ToString() + " Bold= " + ch.Bold.ToString() );
}
}

Here is the sample for both the methods:

http://www.syncfusion.com/uploads/redirect.aspx?file=Character_Style1_a40b4f6e.zip&team=support


Please have a look into the sample and could you please send us the modified one to reproduce the issue and to investigate further on this issue?

Best Regards,
Bhuvana





BP Bhuvaneswari P Syncfusion Team February 24, 2009 10:33 AM UTC

Hi Pierre,

Thanks for the update.

Currently we don't have any property to check the WTextRange character style; we can only check the characterformat used for that TextRange using the below code snippet:

WTextRange range = item as WTextRange;
WCharacterFormat characterformat = range.CharacterFormat;
Console.WriteLine("Text =" + range.Text);
Console.WriteLine("Bold= " + characterformat.Bold.ToString());


Please try this if it’s not return the correct character format then could please provide with your word document with code snippet to reproduce the issue. It would be helpful to analysis more on this issue.

Best Regards,
Bhuvana



PC Pierre CHAUSSEE February 24, 2009 10:55 AM UTC

Hi Bhuvana,

It doesn't work. Here's the document, I'm sorry you didn't receive in my older post, I just realised you didn't.

Thank you,
Pierre



Syncfusion_92816a17.zip


BP Bhuvaneswari P Syncfusion Team February 25, 2009 08:55 AM UTC

Hi Pierre,

Thanks for the document.

I am able to reproduce the issue when applying the customer character style to the text; it’s not returning the correct value. Could you please open a new direct – trac incident with subject line as Forum 79610 - Access to characterstyle? So that it would be possible for us to provide more details regarding this issue.

Best Regards,
Bhuvana



Loader.
Up arrow icon