ParagraphFormat not updated correctly?

Hello,

when are the ParagraphFormat structures for Styles and paragraphs themselves filled with data ? When I open a document with Aspose I get these structures with default (I guess so) values, not the ones from document itself. Let’s take an example, I’ve created a very simple Word document with only two paragraphs with some text in it, leaving all the settings to defaults. I just typed the text with default style (Normal). By default this style has a 10pt spacing after the paragraph set and I left it like this.
I then opened this document by code and checked the value of doc.Styles(0).ParagraphFormat.SpaceAfter. In most cases it’s 0. Sometimes I managed to read correct value (10) in debug session, but I don’t know why did it happen and trying to repeat the same usually gave no results. In 95% tries I always got 0. It’s the same for doc.Styles(x).ParagraphFormat or the ParagraphFormat of the paragraph itself. Looks like they are not initialized (some other properties were also incorrect) for some reason.
After a short investigation I figured out that when I call doc.UpdatePageLayout() after the document is opened, they are initialized properly and I can access correct values. Is this behaviour intentional?
Does this UpdatePageLayout() make any “harm” to the document ? Does it make any changes there ? Can I use it like this safely or is there a better way ?

I tried with 8.1.0.0, 8.2.2.0 and 9.0.0.0, doesn’t make any difference.

Hi

Thanks for your request. ParagraphFormat and other format structures are filled with data upon loading the document.
Actually, the problem is in your code. doc.Styles(0) will not actually return Normal style, it just return the first style in the collection. You should use code like the following to get Normal style:

Style normal = doc.Styles[StyleIdentifier.Normal];

In this case, you will always get an expected value.
Hope this helps. Please let me know if you need more information. I will be glad to help you.
Best regards.

Hello,

I don’t think the problem is in the code I’ve shown… doc.Styles(0) was just an example and in my particular case first style was the Normal style actually. As I said before it’s not only a problem with Styles collection. The same happens when I iterate over the document tree when I check the ParagraphFormat for the Paragraph nodes, it’s 0 there as well, but it shouldn’t be.
Then after calling UpdatePageLayout() everything magically starts to work. This is why I think it’s the problem with initialization… I can prepare a simple example that you could run for yourself if you wish.

Update: example project added as attachment here, please have a look on this and tell me if you get the same results (0 for first method and 10 for second one).

Hi

Thank you for additional information. There is no problem with initialization. The problem occurs because in DOCX documents formatting can be applied directly, via Styles and via Themes. In your case paragraph spacing is specified via Theme. That is why you do not see it neither in Paragraph.ParagraphFormat nor in Style.ParaphraphFormat.
Aspose.Words supports Themes, but does not expose formatting applied via themes. Themes are preserved upon Docx to Docx roundtrip. During converting document to another formats, which do not support Themes, Themes formatting is converted to direct formatting. So as a workaround, you can try converting your document to DOC for example, and then read formatting from DOC file.
Best regards.

Ok, i see… converting to a different format is not a good solution in
our case. I prefer the workaround with UpdatePageLayout(), is it safe ?
Does it make any changes to the document that will be saved with the
document ?

Hi

Thanks for your request. Yes it is safe, but I think it is more expensive than just converting document into another format. Document.UpdatePageLayout method layouts whole document into pages, this process requires more memory and CPU resources than just saving. Maybe you will not see difference with small documents, but with larger documents the difference in processing time can be noticeable.
Best regards.

Hello,

ok, I will take this into account. Thank you for help.

Hello,

I figured out an even better solution. Actually I don’t need to get the value, I just need to change it for some specific paragraphs. I’ve just made a test and set ParagraphFormat.SpaceAfter to 0 in these paragraphs (even though this property already had a value of 0) and it seems the theme setting has got overridden by what I’ve set, which is exactly what I wanted.

Hi

It is perfect that you found the solution of your issue. Please feel free to ask in case of any issues, I will be glad to help you.
Best regards.