Basic Image Insertion Corrupts Word Doc

I’m trying to do a very basic image insertion, but when I try to open the result in Word 2013 I see “We’re sorry. We can’t open because we found a problem with its contents.”

If I choose to Recover the document, the file seems to be correct, but even if I re-save this recovered file, it still gives the same error message next time. This message should not be appearing at all! Why is it not being saved in a valid format?

I’ve upgraded to the latest version (17.9) but this problem remains.

I’m attaching the source code and the input document & image, and also including the source code below.

What can I do to resolve this? Thanks.

Code:
public static void AsposeSupportTestCase_WordDocIsCorrupted(byte[] wordDocFile, byte[] imageFile, string destinationFilename)
{
// SetLicense(); // License is being set here! This is happening on the full version, not a trial.
Aspose.Words.Document doc = new Aspose.Words.Document(new MemoryStream(wordDocFile));
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.InsertImage(imageFile);
byte[] resultBytes;
using (MemoryStream saveStream = new MemoryStream())
{
builder.Document.Save(saveStream, Aspose.Words.SaveFormat.Docx);
resultBytes = saveStream.ToArray();
}
System.IO.File.WriteAllBytes(destinationFilename, resultBytes);
}
asposeWordDocCorrupted.zip (12.1 KB)

@DerrickAccountNumber

Thanks for your inquiry. Please note Aspose.Words mimics MS Word behavior. You cannot insert image in a Checkbox StructuredDocumentTag using MS Word as well. If you want to add image in start of the document, then please add following code lines in your code for the purpose. However, if there is any difference in your requirement and our understanding then please share some more details about your requirement along with a sample expected Word document. It will help us to address your issue exactly.

......
......
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
if (doc.FirstSection.Body.FirstChild.NodeType == NodeType.StructuredDocumentTag &&
    ((StructuredDocumentTag)doc.FirstSection.Body.FirstChild).SdtType == SdtType.Checkbox)
{
    doc.FirstSection.Body.InsertBefore(new Paragraph(doc), doc.FirstSection.Body.FirstChild);
}

builder.MoveToDocumentStart();
......
......

Thank you for the quick and helpful reply. This suggestion did fix the problem I was having.

I have one additional question:

Is this Checkbox + StructuredDocumentTag the only node into which I cannot insert an image? It seems likely that there might be more of these. If there are any other nodes into which I can’t insert images using builder.InsertImage, I would like to know where to find the entire list of them, because our documents won’t always contain these checkboxes.

Thanks again.

@DerrickAccountNumber

Thanks for your feedback. I am afraid there is no list available for such Nodes. However, as Aspose.Words mimics MS Word as closest as possible, so we can insert/add image to all the Nodes supported by MS Word. Furthermore, in your scenario, to ensure that image always inserted in the start of document you can use following code.

Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

//Add a paragraph node in the start of document
doc.FirstSection.Body.InsertBefore(new Paragraph(doc), doc.FirstSection.Body.FirstChild);

builder.MoveToDocumentStart();
builder.InsertImage(@"D:\Downloads\asposeWordDocCorrupted\testRed.jpg");