DocX icon indicating copy to clipboard operation
DocX copied to clipboard

Cannot access document styles (nor effective MagicText formatting)

Open pablodegrande opened this issue 3 years ago • 5 comments

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

pablodegrande avatar May 24 '21 22:05 pablodegrande

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
    }
  }

XceedBoucherS avatar May 25 '21 12:05 XceedBoucherS

Sorry, but this is not true when Word Styles are applied. Steps to reproduce:

  1. Open Microsoft Word. Create New Empty Document.
  2. Write "hello". Select and apply a style that has Bold in its definition (e.g. apply 'Strong' style)
  3. 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.

pablodegrande avatar May 25 '21 14:05 pablodegrande

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.

XceedBoucherS avatar May 25 '21 18:05 XceedBoucherS

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!

pablodegrande avatar May 25 '21 19:05 pablodegrande

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.

XceedBoucherS avatar May 25 '21 20:05 XceedBoucherS