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

Free Support Forum - aspose.com

Need to find total images in docx file

Dear team,

we need to find total images in given docx file, we are using below java code to find images

private static void findAllfigures(Document initDoc, String nameAppend) throws NullPointerException
{
    String matches = "Fig.*(?:[ \\r\\n\\t].*)+|Scheme.*|Plate.*|Abbildung.*";
	try {

            for (Paragraph para : (Iterable<Paragraph>)initDoc.getChildNodes(NodeType.PARAGRAPH, true))
            {
                System.out.println("All para  :" + para.getText().toString());
                if (para.getText().trim().startsWith(FIG) || para.getText().trim().startsWith(SCHEME)
                        || para.getText().trim().startsWith(PLATE))
                {
                    if (!(para.toString(SaveFormat.TEXT).trim().startsWith("Figure Captions")))
                    {
                        try
                        {
                            Table parentTable = (Table)para.getAncestor(NodeType.TABLE);
                            if (parentTable != null && parentTable.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
                            {
                                String allFignames = null;
                                {
                                    allFignames = formatImgcaption(para.toString(SaveFormat.TEXT).trim(), nameAppend);
                                }
                                allimages.add(allFignames);
                            }
                        }
                        catch (NullPointerException e)
                        {
                            logger.info("Exception ", e.getMessage());
                            e.printStackTrace();
                        }

                    }
                }
            }
            initDoc.save(interim);
        } catch (Exception e) {
		logger.info("Exception ", e.getMessage());
		e.printStackTrace();
	}
}

but this code not found the images for few type of documents

input file : 1. Manuscript_Imoto_20220330_revise2_clean.docx (5.8 MB)

please do need full

@e503824 In your code you are supposing that image caption and the image is in the table. but in this case they are not. There are simply two sibling paragraphs - one with image and another with caption. You should modify your code like the following:

for (Paragraph para : (Iterable<Paragraph>)initDoc.getChildNodes(NodeType.PARAGRAPH, true))
{
    String paraText = para.toString(SaveFormat.TEXT).trim();
    if (paraText.startsWith(FIG) || paraText.startsWith(SCHEME) || paraText.startsWith(PLATE))
    {
        if (!paraText.startsWith("Figure Captions"))
        {
            try
            {
                Paragraph previousPara = (Paragraph)para.getPreviousSibling();
                if (previousPara != null && previousPara.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
                {
                    System.out.println(paraText);
                }
            }
            catch (NullPointerException e)
            {
                System.out.println("Exception " + e.getMessage());
                e.printStackTrace();
            }

        }
    }
}