Document.getStyles() does not work correctly with some styles

Hello,

Please find attached Word document containing custom styles with aliases.
In such document, there is a problem with method Document.getStyles().

The following code illustrate the problem :

Document doc = new Document(new FileInputStream(new File("StyleIterate.docx")));

int nbStyles_by_count = doc.getStyles().getCount();
int nbStyles_by_iterate = 0;
for (Style s : doc.getStyles())
{
    nbStyles_by_iterate++;
}

System.out.println("Nb styles by count : " + nbStyles_by_count);
System.out.println("Nb styles by iterate : " + nbStyles_by_iterate);

The result is :
Nb styles by count : 18 (Correct)
Nb styles by iterate : 44 (Wrong, it seems iterating over styles name + aliases)

Is it possible to fix it and to make Document.getStyles() iterating over the real number of styles in a document ?

Best regards,

Hi there,

Thanks for your inquiry. Your document contains alias styles E.g CustomStyle10 has alias C10 and cm10. This is the reason you are getting style count as 44. Please use Style.getAliases method to get all aliases of a style. If style has no aliases then empty array of string is returned.

If you
want to iterate through only unique styles in a document, please use following code example.

Document doc = new Document(MyDir  +"StyleIterate.docx");
for(int  i = 0; i < doc.getStyles().getCount(); i++)
{
    System.out.println(doc.getStyles().get(i).getName());
}

Hello,

Thanks for the workaround.

But it think this is very confusing for users of your Java API. I really think you have to do something about it :

- If this is not expected behavior : it should be fixed sooner or later.

- If this is expected behavior (i think it should not but i’m not an Aspose API designer) : you have to update java documentation of this call to warn users of your API that document.getStyles() will iterate over style names AND style aliases. And how to do to iterate only through unique styles

Best regards,

Hi there,

Thanks for your inquiry. This is the expected behavior of Aspose.Words. We will update the documentation about Document.Styles property. We apologize for your inconvenience.

Hello,

Ok.
Thanks for the time you spend on this ticket.

Best regards,