Appending same/similar documents three times causes System.InvalidOperationException : "Cannot find a style with this istd

Dear Aspose support team,

we use .NET Aspose.Words v25.9.0 and ran into System.InvalidOperationException : "Cannot find a style with this istd" Error while using the Document.AppendDocument() function and thus seek your help.

The error is invoked when trying to append sample_file.docx three times.
Appending the document once or twice, works fine.

NOTE: previously we did not use the same document three times but very similar ones to invoke the error.

In the attachment you’ll find a XUnit project to reproduce the issue.

Error Log:

Message: 
System.InvalidOperationException : Cannot find a style with this istd.

  Stack Trace: 
StyleCollection.n(Int32 d, Int32 v)
Style.z()
Style.d(Style d, Ig v)
Style.Equals(Style style)
StyleCollection.m(Style d)
StyleCollection.c(eU7 d, Style v)
StyleCollection.d(eU7 d, Style v)
NodeImporter.d(PA d, PA v, Int32 c)
NodeImporter.d(Paragraph d, Paragraph v)
NodeImporter.d(Node d, Node v)
NodeImporter.v(Node d, Boolean v, j9 c)
NodeImporter.d(CompositeNode d, CompositeNode v, j9 c)
NodeImporter.v(Node d, Boolean v, j9 c)
NodeImporter.d(CompositeNode d, CompositeNode v, j9 c)
NodeImporter.v(Node d, Boolean v, j9 c)
NodeImporter.d(CompositeNode d, CompositeNode v, j9 c)
NodeImporter.v(Node d, Boolean v, j9 c)
NodeImporter.d(CompositeNode d, CompositeNode v, j9 c)
NodeImporter.v(Node d, Boolean v, j9 c)
NodeImporter.d(CompositeNode d, CompositeNode v, j9 c)
NodeImporter.v(Node d, Boolean v, j9 c)
NodeImporter.d(CompositeNode d, CompositeNode v, j9 c)
NodeImporter.v(Node d, Boolean v, j9 c)
NodeImporter.d(CompositeNode d, CompositeNode v, j9 c)
NodeImporter.v(Node d, Boolean v, j9 c)
NodeImporter.d(Node d, Boolean v, j9 c)
E9.v(Document d, ImportFormatMode v, ImportFormatOptions c)
Document.AppendDocument(Document srcDoc, ImportFormatMode importFormatMode, ImportFormatOptions importFormatOptions)
Document.AppendDocument(Document srcDoc, ImportFormatMode importFormatMode)
UnitTest1.AppendDocuments(IList`1 src_file_paths, ImportFormatMode import_format_mode) line 34
UnitTest1.AppendingDocuments_Fails(ImportFormatMode import_format_mode) line 22
InvokeStub_UnitTest1.AppendingDocuments_Fails(Object, Span`1)
MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

Best regards

sample_result.docx (14.3 KB)
AsposeIssuePOC.7z (18.3 KB)

@kaiwachter

The System.InvalidOperationException : "Cannot find a style with this istd" error you are encountering when appending the same or similar documents multiple times is likely due to style conflicts in the document. When you append a document, Aspose.Words attempts to merge styles, and if it encounters a style reference that it cannot resolve, it throws this exception.

To resolve this issue, consider the following approaches:

  1. Use ImportFormatMode: When appending the document, specify the ImportFormatMode to either KeepSourceFormatting or UseDestinationStyles. This can help manage how styles are handled during the append operation.

    Document srcDoc = new Document("sample_file.docx");
    Document mainDoc = new Document();
    mainDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
    mainDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
    mainDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
    
  2. Clone the Document: Instead of appending the same document reference, create a clone of the document each time you append it. This can help avoid style conflicts.

    Document srcDoc = new Document("sample_file.docx");
    Document mainDoc = new Document();
    for (int i = 0; i < 3; i++)
    {
        mainDoc.AppendDocument(srcDoc.Clone(), ImportFormatMode.KeepSourceFormatting);
    }
    
  3. Check Styles: Inspect the styles in both the source and destination documents to identify any discrepancies that may be causing conflicts. Ensure that the styles are consistent and do not conflict with each other.

If these solutions do not resolve the issue, consider reviewing the styles in your documents or providing a minimal reproducible example for further diagnosis.

@kaiwachter
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28631

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Dear alexey,

may I interpred the internal issue tracker as a cofirmation of the issue?
Also, how would I know whether the issue has been fixed?

@kaiwachter We decide whether a problem is a bug or expected behavior only after analyzing it. Not all reported problems can be confirmed as bugs. You can check the status of a problem in the “ Issues Status” section. If the problem is marked as “Closed” and no further information has been provided, the fix will be available in the next monthly release. When the release is published, you will receive a notification via the forum.