Get image x,y and width and height with out figure caption

Chapter09.docx (468.6 KB)

MS Drawing tools doc file image size get.

@mohamed.sathakathullah

To extract the dimensions (x, y, width, and height) of images in a Word document without including figure captions, you can use Aspose.Words for Java. Here’s a step-by-step guide to achieve this:

Cause
Images in Word documents can be embedded in various ways, and extracting their properties requires iterating through the document’s shapes.

Solution
You can loop through the document’s shapes and check if they are images. For each image, you can retrieve its position and size.

Code
Here’s a sample code snippet to get you started:

import com.aspose.words.*;

public class ExtractImageDimensions {
    public static void main(String[] args) throws Exception {
        // Load the document
        Document doc = new Document("Chapter09.docx");

        // Iterate through all shapes in the document
        for (Shape shape : (Iterable<Shape>) doc.getChildNodes(NodeType.SHAPE, true)) {
            if (shape.getShapeType() == ShapeType.IMAGE) {
                // Get image dimensions
                double x = shape.getAbsoluteHorizontalPosition();
                double y = shape.getAbsoluteVerticalPosition();
                double width = shape.getWidth();
                double height = shape.getHeight();

                System.out.println("Image Position: (" + x + ", " + y + ")");
                System.out.println("Image Size: " + width + " x " + height);
            }
        }
    }
}

Next Steps

  1. Ensure you have the Aspose.Words library included in your project.
  2. Replace "Chapter09.docx" with the path to your document.
  3. Run the code to see the dimensions of the images printed in the console.

If you have any further questions or need additional assistance, please feel free to ask!

@mohamed.sathakathullah You can use LayoutCollector and LayoutEnumerator to calculate coordinates of shapes.

Document doc = new Document("C:\\Temp\\in.docx");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
for (Shape s : (Iterable<Shape>)doc.getChildNodes(NodeType.SHAPE, true))
{
    // LayoutCollector and LayoutEnumerator work only with nodes in the main document body
    // Only top level shapes van be processed
    if (!s.isTopLevel() || s.getAncestor(NodeType.HEADER_FOOTER) != null)
        continue;

    enumerator.setCurrent(collector.getEntity(s));
    System.out.println("Page " + enumerator.getPageIndex() + "; Shape Rect : " + enumerator.getRectangle());
}