DocX
DocX copied to clipboard
Cannot access document styles (nor effective MagicText formatting)
I cannot find how to see the list of actual styles in the document (heading 1, heading 2 & user defined styles).
I am trying to state if a text is bold or not, and magictext formatting.bold reports 'null', as the paragraph has no other formatting indication than the style. At that point, I would need to search the style and check if that style has bold = true in its definition.
Most Office libraries have a ParagraphyStyles collection, such as https://docs.devexpress.com/OfficeFileAPI/DevExpress.XtraRichEdit.API.Native.Document.ParagraphStyles
Hi,
The magicText will have a non-null formatting if it contains a bold text. For example, if you have a paragraph that looks like: "This paragraph contains a nice bold text." You will be able to manipulates the 3 magicText of this paragraph: -"This paragraph contains a" : formatting is null -"nice" : formatting is non-null -"bold text." : formatting is null
Here's an example: var doc = DocX.Load( "Test.docx" );
var p = doc.Paragraphs[ 0 ];
foreach( var magicText in p.MagicText )
{
if(magicText.formatting != null && magicText.formatting.Bold.HasValue && magicText.formatting.Bold.Value )
{
// do something
}
}
Sorry, but this is not true when Word Styles are applied. Steps to reproduce:
- Open Microsoft Word. Create New Empty Document.
- Write "hello". Select and apply a style that has Bold in its definition (e.g. apply 'Strong' style)
- Save document.
Run: var xdoc = Xceed.Words.NET.DocX.Load(@"C:\Users\pablodg\Documents\hello.docx"); var first = xdoc.Paragraphs.First().MagicText.First(); Console.WriteLine(first.text); Console.Write(first.formatting.Bold.HasValue ? "Has Value" : "Do not have value"); Console.ReadLine(); And you will see:
hello Do not have value
You cannot tell if a text is bold or not if you don't examine the styles it has applied, as word does.
Hi, I can see the issue. Thank you for the explanation details.
You can retrieve the styleId:
var styleId = magicText.formatting.StyleId;
but as you mentioned, you do not know what this style is all about.
In v2.1, this should all be fixed. We will manage the styleId internally to fill the formatting object with the correct values from the styleId. We will need to test all this.
Thank you.
Great!
Don't forget that Word Styles have a hierarchy (they inherit attributes). And it would be great to have a way to examine (enumerate) existing doc styles... such as in doc.ParagraphStyles.
Thanks a lot!
Hi, Yes, we are aware of the inherit hierarchy. We will try to also add something like a ParagraphStyles property for a document, but it may not be easy in the current design (we don't want to return a list of XElements for each styles). Thank you.