We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Invalid Operation Exception - too many styles

I am trying to create a large document with Aspose.Words, and I keep getting an InvalidOperationException with the description “There are too many styles in the document.” My code is too long to post here, but basically the process is:

  1. I have a collection of HTML files to append to a master Document.
  2. For each HTML file, I create a temp Document. Each file may contain multiple sections, a mixture of text sections and table sections.
  3. I do some various cleanup, and set some background colors on table cells in the temp document.
  4. I append the temp document to the master Document. I have tried both Document.AppendDocument and NodeImporter to do this.
  5. Repeat steps 2-4 for each HTML document.

I have followed the suggestions in the following threads:
Large Scale Documents

I still get the too many styles Exception, regardless of using UseDestinationStyles, or the NodeImporter process. Any suggestions? Is there some way I can directly control the styles added to my document, so they are reused, rather than creating a new one for each style I want to apply to each table cell?


Thanks for your request. This can occur because you use AppendDocument method with ImportFormatMode.KeepSourceFormatting lot of times in your application. Please try using ImportFormatMode.UseDestinationStyles, this should decrease the number of styles.
Best regards,

As I clearly stated above:
“I still get the too many styles Exception, regardless of using

This is not a viable solution for large documents. I have run tests with this setting, and the same number of styles are created with KeepSourceFormatting and UseDestinationStyles.

I have discovered the root cause of the problem, although I don’t think there is an easy solution. The HTML files I am using were generated by publishing from Excel. When Excel creates the HTML files, it assigns a style to every single cell to set the width. So if you have a fairly large table (which we do), you will quickly exceed the Word limit of 4096 styles.

My solution will probably be to either generate the HTML manually, or try to fix the existing files to remove all the style tags and apply widths at the column level. Either solution is very time consuming.


Thank you for additional information. It is not Aspose.Words problem, it is MS Word restriction. Here is quotation from the DOC format specification:
“An istd is an index into rglpstd and is used to reference a particular style definition. Istd values are used internally within the stylesheet, such as the istdBase member of StdfBase, as well as externally outside the stylesheet, such as in Sprms such as sprmPIstd. An istd value MUST be greater than or equal to 0x0000 and less than 0x0FFE.”
Best regards,

Is this issue specific to any particular version of Word (e.g. Word 1997 - 2003) or to Aspose?

I should have noted, that we are experiencing this problem in generating one document from a total of over 650 pages. We are using Aspose to generate 2003 versioned Word documents.

Implementation-Title: Aspose.Words for Java
Specification-Version: 2.4.2

If your problem is the same as we experienced (too many styles), then it will happen with any version of MS Word. There is a limit of 4096 styles in Word 2003 and 2007, this may be different in 2010, but I doubt it.

As a follow-up, we basically had to rewrite the whole routine from scratch. Our initial approach of Excel -> HTML -> Word was the quick & dirty way, and it just didn’t work. It was very easy to exceed the maximum number of styles in this model. We now take the data from Excel and build up the Word Doc piece by piece in code.

Hi Jay and Paul,
Thank you for your requests. Paul is absolutely right, there is a limit of maximum number of styles that can be present in one document. So Jay, would you please share your code that you use to generate the document and source documents (if necessary)? We will check it and provide you more information.
Paul, you can use Excel2Word demo to convert Excel to Word using Aspose.Words and Aspose.Cells. This demo is available here:
Best regards,

As you only inquire of our code, can I presume that the issue is not corrected in later versions of Aspose.Words? We are using v2.4.2.

We essential follow the track of PoJo -> HTML -> Word. I will have to review the code with our team and get back to you with more detail.

Hi Jay
Thank you for additional information. Input HTML file would be also useful in order to reproduce the problem.
Best regards,

The HTML is generated on the fly, and there is no source per se. We do use a Word template with merge fields (although corporate retrictions bar me from sharing.) That aside, the issue I would expect is more in the manner in which we generate individual documents from the template, each with its own header, and then append a large number into one document. We retain the source styling, as we need to retain headers from each source document. But even when switching to the flag USE_DESTINATION_STYLES, we catch an exception just the same. (I should note that we are not seeing an InvalidOperationException, but the message is the same for our IllegalStateException.)
I have attached the handler class which includes all the relevent code. We enter in the method, generateBatchOutput, which generates a series of documents from a list of PoJos, and calls appendDocument. One process attempts to append 326 such documents. Somewhere around 290 in the batch, the appendDocument method catches the IlegalStateException.
Thank you for your assistance.

Hi Jay,

Thank you for additional information. We already have the similar issue fixed in .NET version of Aspose.Words. We will let you know once the fix is ported to Java version.
Best regards,

Thank you for your prompt response. And it is good to hear that you have found a solution, even if not yet ported to Java. When available, should we expect the port to be for your latest version of Aspose.Words, 4.0.x? Or would this be a patch that can be applied to older versions. We would have to upgrade from v2.4.2 if it is the former.

Hi Jay,

Thanks for your request. The fix will be included into the next version of Aspose.Words for Java. We will let you know once it comes out.
Best regards,

The issues you have found earlier (filed as 20505) have been fixed in this update.

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