How to count total number of images

Dear team,

we are finding total number of images in docx but below document we are not able to find image count please help us to fix this issue

Source code :

private static void findAllfigures(Document initDoc, String nameAppend) throws NullPointerException {
String matches = "Fig.*(?:[ \\r\\n\\t].*)+|Scheme.*|Plate.*|Abbildung.*|Fig.*(?:[ \\r\\n\\t]*)+|FIG.*(?:[ \\r\\n\\t].*)+";
		
try {
	for (Paragraph para : (Iterable<Paragraph>) initDoc.getChildNodes(NodeType.PARAGRAPH, true)) {
	if ((para.getText().trim().startsWith(FIG)||para.getText().trim().startsWith(CFIG) || 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 : CONBUILDMAT-D-21-07385R1.docx (303.0 KB)

@e503824 The problem occurs because caption is in a group shape. You can modify your code like the following to take group shape in account:

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) || paraText.startsWith(FIGURE))
    {
        if (!paraText.startsWith("Figure Captions") && !(paraText.startsWith("Figures and captions")))
        {
            try
            {
                Table parentTable = (Table)para.getAncestor(NodeType.TABLE);
                Node next = para.getNextSibling();
                while (next != null && !next.isComposite())
                    next = next.getNextSibling();

                Node prev = para.getPreviousSibling();
                while (prev != null && !prev.isComposite())
                    prev = prev.getPreviousSibling();

                CompositeNode parentShape = para.getAncestor(NodeType.GROUP_SHAPE);
                while (parentShape != null && parentShape.getAncestor(NodeType.GROUP_SHAPE) != null)
                    parentShape = parentShape.getAncestor(NodeType.GROUP_SHAPE);

                CompositeNode nextNode = (CompositeNode)next;
                CompositeNode prevNode = (CompositeNode)prev;
                if ((para != null && para.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
                        || (nextNode != null && nextNode.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
                        || (prevNode != null && prevNode.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
                        || (parentShape != null && parentShape.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
                        || (parentTable != null && parentTable.getChildNodes(NodeType.SHAPE, true).getCount() > 0))
                {
                    System.out.println(paraText);
                }
            }
            catch (NullPointerException e)
            {
                System.out.println("Exception " + e.getMessage());
            }

        }
    }
}

Dear team,

I have checked but still we are facing same issue

im using below source code

String matches = "Fig.*(?:[ \\r\\n\\t].*)+|Scheme.*|Plate.*|Abbildung.*|Fig.*(?:[ \\r\\n\\t]*)+|FIG.*(?:[ \\r\\n\\t].*)+";
	
	try {
		for (Paragraph para : (Iterable<Paragraph>) initDoc.getChildNodes(NodeType.PARAGRAPH, true)) {
		if ((para.getText().trim().startsWith(FIG)||para.getText().trim().startsWith(CFIG) || 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")))) {
			
			Table parentTable = (Table)para.getAncestor(NodeType.TABLE);
            Node next = para.getNextSibling();
            while (next != null && !next.isComposite())
                next = next.getNextSibling();

            Node prev = para.getPreviousSibling();
            while (prev != null && !prev.isComposite())
                prev = prev.getPreviousSibling();

            CompositeNode parentShape = para.getAncestor(NodeType.GROUP_SHAPE);
            while (parentShape != null && parentShape.getAncestor(NodeType.GROUP_SHAPE) != null)
                parentShape = parentShape.getAncestor(NodeType.GROUP_SHAPE);

            CompositeNode nextNode = (CompositeNode)next;
            CompositeNode prevNode = (CompositeNode)prev;
 
  
					try {
						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)
							&&((para != null && para.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
			                || (nextNode != null && nextNode.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
			                || (prevNode != null && prevNode.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
			                || (parentShape != null && parentShape.getChildNodes(NodeType.SHAPE, true).getCount() > 0)))
				
								{

Please Note : we are using multiple sinario’s and I have tried same source code also Its not working

@e503824 As I can see you did not fully follow the suggestions I have provided. I suspect in your case the first condition does not pass:

if ((para.getText().trim().startsWith(FIG)||para.getText().trim().startsWith(CFIG) || para.getText().trim().startsWith(SCHEME)
		|| para.getText().trim().startsWith(PLATE)) && !AIE.supplymentryCheck(para.toString(SaveFormat.TEXT).trim()))

Could you please change it to:

String paraText = para.toString(SaveFormat.TEXT).trim();
if (paraText.startsWith("Fig") || paraText.startsWith("Scheme") ||
    paraText.startsWith("Plate") || paraText.startsWith("Figure"))

Also, there might be problem in the last condition:

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)
    && ((para != null && para.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
    || (nextNode != null && nextNode.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
    || (prevNode != null && prevNode.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
    || (parentShape != null && parentShape.getChildNodes(NodeType.SHAPE, true).getCount() > 0)))

First of all there are duplicated conditions, secondly the following (((Paragraph)para.getNextSibling()).getChildNodes(NodeType.SHAPE, true) and the following ((Paragraph)para.getPreviousSibling()).getChildNodes(NodeType.SHAPE, true).getCount() will cause NullReference exception because in your document paragraph with caption is the only paragraph in the textbox shape and does not have neither next not previous sibling. These two conditions can be removed, because they are covered by the following two:

    || (nextNode != null && nextNode.getChildNodes(NodeType.SHAPE, true).getCount() > 0)
    || (prevNode != null && prevNode.getChildNodes(NodeType.SHAPE, true).getCount() > 0)