Remove Content Control from Footer & Convert Word DOCX to HTML Fixed File Format C# .NET | Avoid Stack Empty Error

I have a DOCX file that cannot be saved as HTML with save options. I have attached a sample document and code to reproduce this issue. PFA StackEmpty.zip

StackEmpty.zip (28.1 KB)

I get the following exception:

Unhandled exception. System.InvalidOperationException: Stack empty.
at System.Collections.Generic.Stack1.ThrowForEmptyStack() at System.Collections.Generic.Stack1.Pop()
at ​ . ()
at ​ . ​ ( )
at . ( ​ )
at . ( ​ )
at ​ . ( )
at ​ . ( , )
at .( , ​ )
at . ( )
at . ( )
at Aspose.Words.Document. ( )
at Aspose.Words.Document.​(Stream , String , SaveOptions )
at Aspose.Words.Document.Save(String fileName, SaveOptions saveOptions)
at StackEmpty.Program.Main(String[] args)

@GZaha,

It seems problem occurs because of the Content Control in the primary Footer of source Word DOCX document. The exception does not occur when you remove this StructuredDocumentTag node and then convert DOCX to HTML Fixed file format:

Aspose.Words.LoadOptions docxLoadOptions = new Aspose.Words.LoadOptions();
docxLoadOptions.LoadFormat = LoadFormat.Docx;
docxLoadOptions.MswVersion = MsWordVersion.Word2013;
Document doc = new Document(@"C:\Temp\StackEmpty\input.docx", docxLoadOptions);

StructuredDocumentTag sdt = doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary].FirstParagraph.LastChild as StructuredDocumentTag;
Node[] nodes = sdt.GetChildNodes(NodeType.Any, true).ToArray();
foreach (Node node in nodes)
    sdt.ParentNode.InsertAfter(node, sdt.PreviousSibling);
sdt.Remove();

HtmlFixedSaveOptions htmlFixedSaveOption = new HtmlFixedSaveOptions();
htmlFixedSaveOption.ShowPageBorder = true;
htmlFixedSaveOption.ExportEmbeddedImages = true;
htmlFixedSaveOption.ExportEmbeddedCss = true;
htmlFixedSaveOption.ExportEmbeddedFonts = true;

doc.Save(@"C:\Temp\StackEmpty\20.11.html", htmlFixedSaveOption);

For the sake of correction in Aspose.Words for .NET API, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-21462. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@awais.hafeez

Thank you for feedback.

The suggestion of removing the StructuredDocumentTag from footer is not valid in our case. We will wait for the final fix from dev team.

@GZaha,

Sure, we will inform you here as soon as this issue will get resolved in future.

The issues you have found earlier (filed as WORDSNET-21462) have been fixed in this Aspose.Words for .NET 21.1 update and this Aspose.Words for Java 21.1 update.