Error while saving word document - Unexpected subdocument type

Hello team,

I’m using Aspose.Words for Watermarking Word documents and I have run into an exception.
I used the following code,

Document doc = new Document(file);
doc.Save(savedir);

The exception occurs at the line - doc.Save(savedir).

The exception details as follows :

*System.InvalidOperationException was caught*
*HResult=-2146233079*
*Message=Unexpected subdocument type.*
*Source=Aspose.Words*
*StackTrace:*
*at . ( ​ )*
*at . (ShapeBase )*
*at . (ShapeBase )*
*at . (Shape )*
*at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)*
*at Aspose.Words.CompositeNode.AcceptChildren(DocumentVisitor visitor)*
*at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)*
*at Aspose.Words.CompositeNode.AcceptChildren(DocumentVisitor visitor)*
*at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)*
*at Aspose.Words.CompositeNode.AcceptChildren(DocumentVisitor visitor)*
*at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)*
*at Aspose.Words.CompositeNode.AcceptChildren(DocumentVisitor visitor)*
*at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)*
*at . (Section )*
*at . ()*
*at . ​ ( )*
*at Aspose.Words.Document. ( , SaveOptions )*
*at Aspose.Words.Document. (Stream , String , SaveOptions )*

I’m using Aspose.Words version 14.10.0.0, and I also tried with the latest version 15.5.0.0 but faced the same issue. The exception looks similar to the one mentioned in this post .

When we convert the document from .doc to .docx it works fine. But I want watermarking to be successful for both doc and docx files. Can you guide me on a solution for this problem ?

Regards,
Kiran
.

Hi Michelle,

Thanks for your inquiry. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information. Please also share to which file format you want to convert your output document.

Hi Tahir,

Please find the attached sample file here.
The output is saved as WORD document and the error is coming while saving the document.
Please investigate the issue and let us know if you find any solution for this.

Regards,
Kiran

Hi Kiran,

Thanks for sharing the document. I have tested the scenario using latest version of Aspose.Words for .NET 15.6.0 and have not found the shared issue. Please use Aspose.Words for .NET 15.6.0 and read following documentation link about adding watermark in the document.
https://docs.aspose.com/words/net/working-with-watermark/

Please let us know if you have any more queries.

Thank you Tahir,

I tried with the latest version of Aspose.Words - 15.6.0 but still getting the same exception while saving the document.
Regarding the approach for watermarking word documents, we are using a different approach than what you have suggested in the above mentioned link. And the current approach is evolved based on the discussions which we had with your team to accomplish our requirements.
Below are the threads containing discussions which we had with your team

Regards,
Kiran

Hi Kiran,

Thanks for sharing the detail. Please use following method and let us know how it goes on your side. Hope this helps you. If you still face problem, please share a standalone/runnable simple application (for example a Console Application Project) that demonstrates the code (Aspose.Words code) you used to generate your output document.

public static void InsertWatermarkImageAtEachPage(Document doc, string watermarkImagePath)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    LayoutCollector collector = new LayoutCollector(doc);
    Paragraph anchorPara = null;
    int pageIndex = 1;
    foreach (Section section in doc.Sections)
    {
        foreach (Paragraph para in section.Body.Paragraphs)
        {
            if (collector.GetStartPageIndex(para) == pageIndex)
            {
                anchorPara = para;
                Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.Image);
                watermark.ImageData.SetImage(watermarkImagePath);
                watermark.Width = 300;
                watermark.Height = 300;
                watermark.Left = 0;
                watermark.Top = 0;
                watermark.BehindText = true;
                anchorPara.AppendChild(watermark);
                pageIndex++;
            }
        }
    }
}

Thank you Tahir, after making this change issue got resolved, I am able to save document without error.

Thank you,
Kiran

Hi Kiran,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.