Image Extraction along with Drawing Tools objects inside the image - Java

Hi Team

I am working with extracting the entire image along with the drawing tools inside the image in java.Since DrawingML is not in java how to proceed. Kindly support.Doc2.zip (39.9 KB)
Thanks in Advance.

@MikeLak,

Thanks for your inquiry. Please refer to the following article about extracting the images.
How to Extract Images from a Document

Your document contains the group shape. Following code example shows how to extract it and convert it to image. Hope this helps you.

Document doc = new Document(MyDir + "Doc2.docx");

NodeCollection<GroupShape> shapes = (NodeCollection<GroupShape>) doc.getChildNodes(NodeType.GROUP_SHAPE, true);
int imageIndex = 0;
for (GroupShape groupShape : shapes) {
    groupShape.getShapeRenderer().save(MyDir + "output"+imageIndex+".png", new ImageSaveOptions(SaveFormat.PNG));
    imageIndex++;
}

Hi Tahir

For this fig the drawing tool inside the image is not rendered.Here in this case the arrow inside the fig is not captured for Fig 1.For Fig 2 the image is seperated. Help please.
Input is
doc3.zip (425.6 KB)

public static void main(String[] args) throws Exception {
	/** ASPOSE LICENSE START **/
	try {
		com.aspose.words.License license = new com.aspose.words.License();
		
	} catch (Exception e) {
		System.out
				.println("License File Not Accessed\nError in Word Document");

		System.exit(0);
	}
	/** ASPOSE LICENSE END **/
	// ExStart:ExtractImagesToFiles
	// The path to the documents directory.
	System.out.println("**********");
	String dataDir = args[0];
	System.out.println("**********");
	Document doc = new Document(dataDir);
	//Document doc = new Document("tested.doc");
	DocumentBuilder builder = new DocumentBuilder(doc);
	int i = 1;
	// Get collection of Group shapes
	NodeCollection<GroupShape> Gshapes = doc.getChildNodes(	NodeType.GROUP_SHAPE, true);
	// Loop through all Group Shapes
	for (GroupShape shape : Gshapes) {
		// Save Group shape as image
		ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
		ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.JPEG);
		shape.getShapeRenderer().save(imageStream, imageOptions);
		builder.moveTo(shape);
		builder.insertImage(imageStream.toByteArray());
		Document dstDoc = new Document();
		NodeImporter importer = new NodeImporter(doc, dstDoc,ImportFormatMode.KEEP_SOURCE_FORMATTING);
		Node newNode = importer.importNode(shape, true);
		dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(newNode);
		dstDoc.save("D:\\temp\\Output_" + i + ".png");
		i++;
		shape.remove();
	}}}

@MikeLak,

Thanks for your inquiry. Please check the attached DOM image. DOM.png (19.9 KB)
The GroupShape Node and drawing tools (Shape nodes) are in different paragraph nodes. In this case, we suggest you please bookmark the content and extract them using the approach shared here:
Extract Content from a Bookmark