We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to get image counts

Hi there

Is there any way to get image count per page ?
(Including diagrams, shapes …)

and image count of the entire document

Both 2 situation need to implement, thanks

Hi Craig,

Thanks for your inquiry. An image is represented by Shape object in Aspose.Words and you can try something like below to get number of images in document:

int imgCount = 0;
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.HasImage)
        imgCount++;
}

Secondly, you can split document to pages and calculate number of images in each one-page document.

Hope, this helps.

Best regards,

Thanks for your solution!

But We met another problem.

We use Aspose to convert word files to html files,
and we need to know the count of images which will be produced in the result html files before the conversion.

Is there any solution to achieve this?

Hi Craig,

Thanks for your inquiry. You can use the following code to get count of how many images will be generated during Word to HTML conversion:

Document doc = new Document(MyDir + @"input.docx");
HandleImageSaving handler = new HandleImageSaving();
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);
options.ImageSavingCallback = handler;
doc.Save(MyDir + @"16.1.0.html", options);
Console.WriteLine(handler.imageCount);
public class HandleImageSaving : IImageSavingCallback
{
    public int imageCount = 0;
    void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
    {
        Shape shape = (Shape)args.CurrentShape;
        if (shape.HasImage)
            imageCount++;
    }
}

Hope, this helps.

Best regards,