Styles are being removed

[Edited: I don’t know why the HTML formatting isn’t showing up, so I’m going to have to better describe things]



I’ve been struggling with this for a very long time. Here’s what I’m trying to accomplish:



Some text in Custom Style 1 Some text in Custom Style 2 (imagine that the first phrase is bolded, and the second is underlined)


Here’s the code that I would think should work:



db.ParagraphFormat.StyleName = “Custom Style 1”;

db.Write(“Some text in Custom Style 1”);

db.ParagraphFormat.StyleName = “Custom Style 2”;

db.Write(" Some text in Custom Style 2");



What I end up with is:



Some text in Custom Style 1 Some text in Custom Style 2 (imagine that the both phrases are underlined)



Both strings are set to the second style.



I’ve found that many times if I insert a continuous section break
between style changes, it seems to help, but this doesn’t always hold
true.


I’m seeing this problem any time I want to switch styles. Another
example where it’s happen is in tables. I’ll set the first row to one
style and subsequent rows to “Normal”. What I’ll get are all of the
cells, except the very last cell in the first row will be the right
style, but the last cell’s content will be “Normal”.



What am I doing wrong? Please help!

ParagraphFormat is applied to a paragraph you are currently in. When you call DocumentBuilder.Write you remain in the same paragraph. So the style is simply reapplied to the whole paragraph.

You should change paragraph style only when you are done with the current paragraph and have moved to another one. For example, after Writeln, EndRow, EndTable calls.

miklovan wrote:

ParagraphFormat is applied to a paragraph you are currently in. When you call DocumentBuilder.Write you remain in the same paragraph. So the style is simply reapplied to the whole paragraph.

You should change paragraph style only when you are done with the current paragraph and have moved to another one. For example, after Writeln, EndRow, EndTable calls.



So, you're telling me that I cannot have 2 styles in the same line of text? Word supports this. I simply highlight a few words and select the style. Instantly I have mixed styles in the same line.

Sorry for providing misleading answer.

MS Word 2003 has 4 types of styles - character, paragraph, list and table.

Character styles are for runs of text. Paragraph styles are for paragraphs.

What I have said is true for applying paragraph style to a paragraph. If you want to apply character style to a particular run of text you should use DocumentBuilder.Font.StyleName property, like in the following example:

builder.Font.StyleName = "Style1";

builder.Write("Style1");

builder.Font.StyleName = "Style2";

builder.Write("Style2");

Please mind, however, that MS Word permits applying not only character but also paragraph styles to the runs, taking font formatting from paragraph style and applying it to the run.

Aspose.Words does not offer this kind of functionality yet. This feature request is logged in our defect base as Issue #942. We will try to add this feature in one of our future releases.

So, for the style to be applied correctly to the run of text created by DocumentBuilder.Write you need to set the character style name in DocumentBuilder.Font.StyleName property.

Absolutely perfect. I knew I was doing something insanely stupid. I’ll give it a try.

miklovan wrote:

Please mind, however, that MS Word permits applying not only character but also paragraph styles to the runs, taking font formatting from paragraph style and applying it to the run.



I've given Font.StyleName a shot. I think I understand what you're saying in that sentence. What's hapening in the resulting Word doc is that each paragraph looks the same (Normal), even though I've set the Font.StyleName to be the style that I want. When I reveal the formatting, there is no paragraph style defined (so I assume it's using Normal), and the Font/Character Style is set to what I told it to be. But, Word renders it all using the paragraph style. So, this essentially makes Font.StyleName useless (for me, anyways), since Word is overriding any Font.StyleName with Paragraph.StyleName.

If I set the paragraph style, the entire text is rendered in that style, without regards to the Font.StyleName.

From what I can tell, there's no way to output text as: Style1Style2, since you can only have 1 paragraph style, and it always overrides the font style.

Attached is the document made using the code I have given in the previous message. Please note that I have created the character styles Style1 and Style2 for the template document. Paragraph styles indeed don't work.

Thanks for your patience with me. I was unaware of the different types
of styles available in Word. The person who gave me the source template
defined all styles as paragraph styles, which explains the behavior I
was seeing. I’ve since changed the necessary styles to character
styles, and things are much better now.

Yes, its a bit hairy what Microsoft Word allows to do. Unless one is familiar with the details it might be hard to rationally explain what's happening and hard to use Aspose.Words as a consequence.

So my point is that one often needs to understand things about Microsoft Word before using Aspose.Words properly. In Aspose.Words documentation we cannot give a proper and full description of all document features just because Microsoft Word is so feature rich.

I find Microsoft has now heaps of useful articles in the Office Online Assistance that really explain things well. Here is a couple related to styles (not sure if they exactly about the issue discussed in this thread, but anyway).

http://office.microsoft.com/training/training.aspx?AssetID=RC011039261033
http://office.microsoft.com/en-gb/assistance/HA011876081033.aspx

Regarding the character formatting here is a "simple" formula:

Final Character Formatting in the Document =

1. Character Formatting specified in the Paragraph Style of the Paragraph that contains the Run of text.

plus

2. Character Formatting specified in the Character Style applied to the Run of text.

plus

3. Direct Character Formatting applied to the Run of text.

In addition to the above I must say that styles can be based on each other so the formatting specified by the style is actually an accumulation of all formatting specified in this style and all the styles it is based on.