Background image of Word document can not be detected

Hi Team,

I’m trying to detect and extract the background image of a word document (docx), created with Ms Office 2016 but I’m failing to do so.
Background image.docx (1.6 MB)

Here is the code:

using System;
using Aspose.Words;
using Aspose.Words.Drawing;

namespace WordImagery
{
    class Program
    {
        static void Main(string[] args)
        {
            Aspose.Words.License wordslic = new Aspose.Words.License();
            wordslic.SetLicense("<licence file>");

            string fileName = "Background image.docx";

            Document wordsDocument = new Document(fileName);

            int shapeNr = 0;
            NodeCollection shapes = wordsDocument.GetChildNodes(NodeType.Shape, true);
            foreach (var node in shapes)
            {
                var shape = (Shape)node;
                if (shape.HasImage)
                {
                    ++shapeNr;
                }
            }
            Console.WriteLine("{0} has {1} image shapes\n", fileName, shapeNr);

            if (wordsDocument.BackgroundShape != null)
            {
                Console.WriteLine("{0} has background shape", fileName);
                if (wordsDocument.BackgroundShape.HasImage)
                {
                    Console.WriteLine("wordsDocument.BackgroundShape.HasImage is TRUE");
                }
                else if (wordsDocument.BackgroundShape.ImageData.HasImage)
                {
                    Console.WriteLine("wordsDocument.BackgroundShape.ImageData.HasImage is TRUE");
                }
                else
                {
                    Console.WriteLine("{0} has no background image", fileName);
                }
            }
        }
    }
}

This is giving me the following output on windows:

Background image.docx has 0 image shapes

Background image.docx has background shape
Background image.docx has no background image

Please advise if this is a bug or if I am doing something wrong here.

@zpopswat You can access the background shape using Document.BackgroundShape property. For example the following code resets the background shape:

Document doc = new Document(@"C:\Temp\in.docx");
doc.BackgroundShape = null;
doc.Save(@"C:\Temp\out.docx");

Since background shape is rectangle shape, it does nto dirrectly has an image, it has fill of picture type. So you can use the following condition to check whether background shape is picture:

if(doc.BackgroundShape.Fill.FillType == FillType.Picture)

@alexey.noskov Thank you for the quick answer.
I understand now that this is the way of discovering if the background has an image

if(doc.BackgroundShape.Fill.FillType == FillType.Picture)

but how can I actually access the image data (bytes) and save them into a stream or image.

I’m looking for a solution similar to what is described on the Shape.ImageData property page. Ie:

Document doc = new Document(MyDir + "Images.docx");

// Get the collection of shapes from the document,
// and save the image data of every shape with an image as a file to the local file system.
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

Assert.AreEqual(9, shapes.Count(s => ((Shape)s).HasImage));

int imageIndex = 0;
foreach (Shape shape in shapes.OfType<Shape>())
{
    if (shape.HasImage)
    {
        // The image data of shapes may contain images of many possible image formats. 
        // We can determine a file extension for each image automatically, based on its format.
        string imageFileName =
            $"File.ExtractImages.{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}";
        shape.ImageData.Save(ArtifactsDir + imageFileName);
        imageIndex++;
    }
}

Thank you!

@zpopswat You can use doc.BackgroundShape.Fill.ImageBytes:

Document doc = new Document(@"C:\Temp\in.docx");
File.WriteAllBytes("C:\\Temp\\image.jpeg",doc.BackgroundShape.Fill.ImageBytes);
1 Like