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

Free Support Forum - aspose.com

Need to find image count

Dear team,

We need to find all images using aspose java. please find the below source code which we are using

source code :

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))
        {
            if ((para.getText().trim().startsWith(FIG) || para.getText().trim().startsWith(SCHEME)
            || para.getText().trim().startsWith(PLATE)) && !AIE.supplymentryCheck(para.toString(SaveFormat.TEXT).trim()))
            {
                if (!(para.toString(SaveFormat.TEXT).trim().startsWith("Figure Captions"))
                        && (!(para.toString(SaveFormat.TEXT).trim().startsWith("Figures and captions"))))
                {
                    try
                    {
                        Table parentTable = (Table)para.getAncestor(NodeType.TABLE);
                        if ((((Paragraph)para.getNextSibling()).getChildNodes(NodeType.SHAPE, true).getCount() > 0)
                                || (((Paragraph)para.getPreviousSibling()).getChildNodes(NodeType.SHAPE, true).getCount() > 0)
                                && 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();
    }

input : Figures.docx (216.3 KB)

Please do needful

@e503824 The problem is exactly the same as in your another post
https://forum.aspose.com/t/how-to-find-image-counts/244952
The solution also exactly the same, i.e. replace this condition:

Table parentTable = (Table)para.getAncestor(NodeType.TABLE);
if ((((Paragraph)para.getNextSibling()).getChildNodes(NodeType.SHAPE, true).getCount() > 0)
    || (((Paragraph)para.getPreviousSibling()).getChildNodes(NodeType.SHAPE, true).getCount() > 0)
    && parentTable != null && parentTable.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
 {

with this:

Table parentTable = (Table)para.getAncestor(NodeType.TABLE);
Paragraph nextPara = (Paragraph)para.getNextSibling();
Paragraph prevPara = (Paragraph)para.getPreviousSibling();
if ((nextPara != null && nextPara.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
    || (prevPara != null && prevPara.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
    || (parentTable != null && parentTable.getChildNodes(NodeType.SHAPE, true).getCount() > 0))
{