NullPointer when getting some styles

Hello,

While creating a document, I would like to customize the styles.
I don’t have any problem at all with much styles, but there are 6 styles that I can’t get.

Here are the styles :
NOTE HEADING (StyleIdentifier : 79)
HTML TOP OF FORM (StyleIdentifier : 92)
HTML BOTTOM OF FORM (StyleIdentifier : 93)
OUTLINE LIST 1 (StyleIdentifier : 108)
OUTLINE LIST 2 (StyleIdentifier : 109)
OUTLINE LIST 3 (StyleIdentifier : 110)

Here is my code :

Document doc = new Document();
Style style = doc.getStyles().getByStyleIdentifier(à);

Usually, even if the style isn’t contained yet in the document, the required style is created and add to the StyleCollection of the document.
With these 6 styles, the method only returns null.Why ?
How can I get these styles and then modify them ?

Thank you in advance
Jean-Pascal Lim

Hi Jean-Pascal,

Thanks for your inquiry. Please note that Document() constructor creates a blank Word document which have not the shared styles. Please execute the following code snippet to get the style of empty document.

Document doc = new Document();
for (Style style: (Iterable <Style> ) doc.getStyles())
{
    System.out.println(style.getName());
}

In your case, I suggest you please create a template document by using MS Word which contain the all styles (custom and built-in) and load it in Aspose.Words.Document. Once you loaded this document into Aspose.Words DOM, you can access all styles of your template document. Hope this answers your query. Please let us know if you have any more queries.

Hello

Thank you for your answer.

I am aware that Document() does not add all sthe built-in styles in the created document. Actually, it has very few style (4 to be exact : 0, 65, 105, 107).
However, for all styles except the 6 listed above, when I use the document.getStyles().getByStyleIdentifier(…) method, a style is created if it does not exist yet.

If I use the following code for example " document.getStyles().getByStyleIdentifier(33) ", I will get the IndexHeading style. Even if it didn’t exist at the start.

When I do the same exact code with the 6 styles listed above, I obtain null. Why such a difference ?
Using a template document is not the most convenient solution (althought it may work indeed) and I would prefer by far a method to get/create the requiered styles directly using Aspose.Word.

Thank you in advance

Jean-Pascal Lim

Hi Jean-Pascal,

Thanks for sharing the detail. I have managed to reproduce the same issue at my side. However, it seems that this is unexpected behavior of Aspose.Words (Document.getStyles().getByStyleIdentifier(…) method creates the style).

I have logged this issue as WORDSNET-8703 in our issue tracking system. We will update you via this forum thread once there is any update available on this issue.

We apologize for your inconvenience.

Thank you.

If this is an unexpected behaviour, how shoud I create a built-in style using Aspose.Words ?

If I use the doc.getStyles().add(StyleType.PARAGRAPH, “styleName”) method, I create a custom style, which is not what I want.

Thank you in advance

Jean-Pascal Lim

Hi Jean-Pascal,

Thanks for your inquiry.

*jplim:
Here is my code :

> Document doc = new Document();>
Style style = doc.getStyles().getByStyleIdentifier(à);*

Please note that Document() constructor creates a blank Word document and Document.getStyles return only following styles.

  • Default Paragraph Font
  • No List
  • Normal
  • Table Normal

Please create a document by using MS Word with some text having style e.g Heading 1 and then load this document into Aspose.Words DOM. The Document.getStyles will return Heading 1 style including above styles.

StyleIdentifier Enumerations are Locale independent style identifier. The names of built-in styles in MS Word are localized for different languages. Using a style identifier you can find the correct style regardless of the document language.

Yes, you can get the built-in styles by using getByStyleIdentifier method while working with empty Document. I am in communication with our development team about this unexpected behavior and will update you as soon as I have information on this.

Hi Jean-Pascal,

Further to my last post, I like to share with you that when you try to get built-in style by using getByStyleIdentifier method and the style does not exist in the document, Aspose.Word copies this style into StyleCollection. This is expected behavior of Aspose.Words.

Regarding your query (some styles do not add into StyleCollection when you get it by using getByStyleIdentifier method), I will update you via this forum thread once there is any update available on this issue.

Thanks for your patience.

Hi Jean-Pascal,

Thanks for your patience.

jplim:
Here are the styles :
NOTE HEADING (StyleIdentifier : 79)
HTML TOP OF FORM (StyleIdentifier : 92)
HTML BOTTOM OF FORM (StyleIdentifier : 93)
OUTLINE LIST 1 (StyleIdentifier : 108)
OUTLINE LIST 2 (StyleIdentifier : 109)
OUTLINE LIST 3 (StyleIdentifier : 110)

Please note that when you try to get built-in style by using getByStyleIdentifier method and the style does not exist in the document, Aspose.Word copies this style into StyleCollection. The styles you are using are not built-in. Hope this answers your query. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hello Tahir,

I perfectly understood your points. However, I still don’t know how I should create a NOTE HEADING style. Loading a document created with MSWord is not a convenient method, I would like to be able to create a built-in style directly using Aspose.Word, not MS.Word.

Besides, when I create a document using MS.Word. Only a few styles are existing by default (0, 65, 105, 107).It implies that I would need to create a document with the 200+ styles of MS.Word to load them after. It is not convenient at all. I really want to know how to create styles (and not custom styles) only using Aspose.Word.

At least, what is the definition of a built-in style ? To me, it is a style defined by MS.Word, and not a style customed by the user. All custom styles have a StyleIdentifier 4094, all others are built-in styles and have a specific StyleIdentifier. Being able to select the NOTE HEADING style in MS.Word and the NOTE HEADING style StyleIdentifier being 79, I think that the NOTE HEADING style is a built-in style.

Thank you in advance
Jean-Pascal Lim

Hi Jean-Pascal,

Thanks for your inquiry.

There are a lot of styles listed in StyleIdentifier enum; however, these all styles are not required to be embedded in the document. MS Word creates these styles when it is necessary. The main purpose of StyleIdentifier is language independence i.e. the names of built-in styles in MS Word are localized for different languages and using a style identifier you can find the correct style regardless of the document language.

The ‘NOTE HEADING’ is not built-in style. If you try to add a built-in style by using MS Word, you will get message ‘This style name already exists or is reserved or a built-in style’. Please see the attached image for detail.

Hello Tahir,

I understand perfectly your point and the purpose of StyleIdentifier.

However, when I generate a document with my generator, I don’t know in advance which styles the documents will contain. The user of my generator can choose any style he wants for each paragraph/etc.

So, indeed, the final document will not contain all the built-in styles. But if the only method to create a built-in style with Aspose.Word is to load a .docx file with the specified style embedded, this file must contain all the styles to be able to get them afterwards.

Maybe I just didn’t understand what you told me, but here’s what I understood. If I want to create a Heading-1 style (for example) in a document, I must create a .docx file using MS.Word, then in this document use the Heading-1. After that, with Aspose.Word, I load this file, get the Heading 1 style, copy this style from the loaded file to the document I am generating, and then use it. It is clearly not convenient. I can’t do this for every style I may need in a document.

Using the getStyleByIdentifier method, even if it is not a expected use of the method provides me a simple, easy way to do that : if it exists in the document, it gets it, and if it doesn’t, it creates and puts it inside. Having to create a document using MS.Word with all styles embedded is clearly not a convenient method.If the getStyleByIdentifier method is not intented to do that, there is not another method to do that ?

Finally, using your definition “note heading” is a built-in style.
As you can see on my printscreen, trying to create a new style with name “Titre de note” (which is french for “note heading”) returns the error message you send me. (Sorry, the picture is in french, my MS.Word version is in french)

Thank you in advance,
Jean-Pascal Lim

Hi Jean-Pascal,

Thanks for your inquiry. Please note that following styles are not built-in styles in MS Word. To create the style we have to know how it should look that is why we use only styles created by MS Word.

jplim:
Here are the styles :
NOTE HEADING (StyleIdentifier : 79)
HTML TOP OF FORM (StyleIdentifier : 92)
HTML BOTTOM OF FORM (StyleIdentifier : 93)
OUTLINE LIST 1 (StyleIdentifier : 108)
OUTLINE LIST 2 (StyleIdentifier : 109)
OUTLINE LIST 3 (StyleIdentifier : 110)

Regarding the message you are getting in MS Word, please check styles of your document, the style “Titre de note” already exist in the document.

Hope this answers your query. Please let us know if you have any more queries.

Hello Tahir,

After many tests, I found where is the problem.
In a .docx document, “Note heading” style is not a built-in style.
In a.doc document, “Note heading” style is a built - in style.
Anyway, I DO need a method to create/insert any built-in style in a document, directly using Aspose.Word and not MS.Word. There is not such a method ? If so, please ask your developper team to create such a method.

Thank you in advance
Jean-Pascal Lim

Hi Jean-Pascal,

Thanks for your feedback.

jplim:
I DO need a method to create/insert any built-in style in a document, directly using Aspose.Word and not MS.Word. There is not such a method ? If so, please ask your developper team to create such a method.

I have logged a new feature request as WORDSNET-8849 in our issue tracking system. This feature will allow user to add built-in styles by a method. You will be notified via this forum thread once this feature is available.

*jplim:
In a .doc document, "Note heading"style is a built-in style.
The ‘Note heading’ heading style is built-in in Doc file format. The other styles which you have mentioned in this thread are not built-in. The ‘Note heading’ style is same as normal font style in Doc file format. Please see the attached image for detail.

However, I have logged this issue as WORDSNET-8850 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.*

Hi Jean-Pascal,

Thanks for your patience.

jplim:
I DO need a method to create/insert any built-in style in a document, directly using Aspose.Word and not MS.Word. There is not such a method ? If so, please ask your developper team to create such a method.

I have received response about WORDSNET-8849 from our development team and like to share with you that we already have automatic creation of built-in styles on demand by using following code. So we are closing this issue. Please use the following code snippet to add built-in styles in style collection of document.

Document doc = new Document();
Style style = doc.getStyles().getByStyleIdentifier(...);

The issues you have found earlier (filed as WORDSNET-8850) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.