IllegalStateException while setting the base style

Hi there,
We are extracting the styles from one document and applying it to other document. Since Style.addCopy() would not copy base style we are doing it manually using Style.setBaseStyleName(SampleStyle.getBaseStyleName). Now if while setting the base style name if such as base style is not yet added to document then IllegalStateException would be thrown.

Attaching the document Sample.docx which we are using for copying the style from. We are applying styles from this to 2007 Office blank document and while doing so facing the IllegalStateException.
Note: While applying ‘H4’ and tried to set base style ‘H3’, since the base style is not yet added, the exception is seen.

The code we are using is -

static void applyStyles(Document wordDoc, Document sampleDoc)
{
    StyleCollection sampleDocStyles = sampleDoc.getStyles();
    for (int i = 0; i < sampleDocStyles.getCount(); i++)
    {
        Style sampleStyle = sampleDocStyles.get(i);
        // Need to apply non-linked character level styles, Not all character level styles ends with Char
        if (sampleStyle.getName().endsWith("Char"))
        {
            if (wordDoc.getStyles().get(sampleStyle.getName()) != null)
                continue;
        }
        Style wordDocStyle = wordDoc.getStyles().addCopy(sampleStyle);
        wordDocStyle.setName(sampleStyle.getName());
        // Need to set the base style separately, not applicable for LIST type styles.

        if (sampleStyle.getType() != StyleType.LIST && !StringUtils.equals(sampleStyle.getName(), NORMAL) &&
        !StringUtils.equals(sampleStyle.getName(), DPF) && !StringUtils.equals(sampleStyle.getBaseStyleName(), NORMAL) &&
        !StringUtils.equals(sampleStyle.getBaseStyleName(), DPF))
            wordDocStyle.setBaseStyleName(sampleStyle.getBaseStyleName());
    }
}

Could you please suggest us the by which we can add H4 (the style in Sample.docx) along with base style H3 without IllegalStateException being seen.

Hi Praneeth,

Thanks for your inquiry. I have tested the scenario using latest version of Aspose.Words for Java 14.4.0 and have not found any exception using following code example. Please use the latest version of Aspose.Words for Java 14.4.0.

Please let us know if you have any more queries.

Document doc = new Document(MyDir + "Sample.docx");
Document emptydoc = new Document();
applyStyles(emptydoc, doc);