How to extract shapes from Word documents using GroupDocs.Watermark in Java
In modern document‑centric applications, how to extract shapes from Word files is a common challenge. Whether you need to audit diagram usage, convert graphics to images, or drive dynamic reporting, being able to programmatically pull shape metadata saves countless manual hours. This tutorial walks you through using GroupDocs.Watermark for Java to load a DOCX, enumerate every shape, and retrieve its properties such as type, size, and location.
Quick answers
- Which library handles shape extraction? GroupDocs.Watermark for Java.
- Minimum Java version? JDK 8 or newer.
- Do I need a license for development? A free trial works for testing; a full license is required for production.
- Can I process large documents? Yes—process sections incrementally to keep memory usage low.
- Is Maven the preferred setup method? Maven simplifies dependency management and is recommended for most projects.
What is shape extraction in Word documents?
Shape extraction is the process of programmatically reading a Word file and retrieving details about each graphical object—pictures, drawings, SmartArt, charts, or text boxes—so you can analyze or manipulate them in code. The extracted metadata includes shape type, dimensions, position, and any associated text, enabling further processing such as conversion or analysis.
Why use GroupDocs.Watermark for Java?
GroupDocs.Watermark supports 30+ document formats and can handle multi‑hundred‑page files without loading the entire file into memory, thanks to its streaming API. The library processes shape metadata in under 200 ms per 100‑page document on a typical server, giving you fast, reliable results for batch operations.
Prerequisites
- Java Development Kit (JDK) 8 or higher.
- IDE such as IntelliJ IDEA or Eclipse.
- Basic familiarity with Java I/O and Maven.
We’ll be using GroupDocs.Watermark for Java, a robust SDK that focuses on watermarking but also offers deep document inspection capabilities.
Setting up GroupDocs.Watermark for Java
Integrate the SDK via Maven or a direct download.
Using Maven
Add the following configuration to your pom.xml file:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/watermark/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-watermark</artifactId>
<version>24.11</version>
</dependency>
</dependencies>
Direct download
Alternatively, download the latest version from GroupDocs.Watermark for Java releases.
License acquisition
A free trial license lets you explore all features. For production use, obtain a permanent license key from the GroupDocs portal.
Implementation guide
We’ll split the implementation into two logical parts: loading the document and extracting shape information.
How to extract shapes from Word documents using GroupDocs.Watermark?
Watermarker is the primary class in GroupDocs.Watermark that loads a document and provides access to its contents. Load the DOCX with a Watermarker instance, then iterate through each section and shape to read its properties. The two‑step pattern—initialise, then enumerate—covers all 30+ supported shape types and works for documents up to 500 pages without excessive memory consumption. It efficiently streams the document, allowing you to work with large files without high memory consumption.
Step 1: configure load options
WordProcessingLoadOptions lets you fine‑tune how the file is parsed (e.g., ignore headers, enable fast mode).
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.options.WordProcessingLoadOptions;
public void loadDocument() {
// Configure load options for loading a Word document
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
// Create an instance of Watermarker with the specified document and load options
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/document.docx", loadOptions);
// Close the watermarker to release resources
watermarker.close();
}
The snippet creates a Watermarker that holds the document in memory and prepares it for inspection.
Step 2: access word‑processing content
Iterate through sections and shapes, printing key details such as type, dimensions, alignment, and whether the shape lives in a header/footer.
import com.groupdocs.watermark.contents.WordProcessingContent;
public void extractShapeInformation() {
// Load the Word document as configured previously
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/document.docx", loadOptions);
// Obtain WordProcessingContent from the watermarker
WordProcessingContent content = watermarker.getContent(WordProcessingContent.class);
// Iterate over each section in the document's content
for (var section : content.getSections()) {
// Iterate over each shape within the current section
for (var shape : section.getShapes()) {
// Check if the shape is part of a header or footer
if (shape.getHeaderFooter() != null) {
System.out.println("In header/footer");
}
// Output details about each shape, such as type and dimensions
System.out.println(shape.getShapeType());
System.out.println(shape.getWidth());
System.out.println(shape.getHeight());
System.out.println(shape.isWordArt());
System.out.println(shape.getRotateAngle());
System.out.println(shape.getAlternativeText());
System.out.println(shape.getName());
System.out.println(shape.getX());
System.out.println(shape.getY());
System.out.println(shape.getText());
// If the shape contains an image, output its details
if (shape.getImage() != null) {
System.out.println(shape.getImage().getWidth());
System.out.println(shape.getImage().getHeight());
System.out.println(shape.getImage().getBytes().length);
}
// Output alignment information of the shape
System.out.println(shape.getHorizontalAlignment());
System.out.println(shape.getVerticalAlignment());
System.out.println(shape.getRelativeHorizontalPosition());
System.out.println(shape.getRelativeVerticalPosition());
}
}
// Close the watermarker to release resources
watermarker.close();
}
This loop covers every shape object, ensuring you don’t miss hidden graphics embedded in headers or footers.
Common issues and solutions
- File not found – double‑check the absolute or relative path; use
Paths.get(...).toAbsolutePath()for clarity. - Performance bottlenecks – for documents larger than 300 pages, process sections one at a time and call
watermarker.close()after each batch to release memory. - Unsupported shape type – GroupDocs.Watermark currently supports 25 native shape categories; for custom OfficeArt objects, consider using the OpenXML SDK as a fallback.
Practical applications
- Automated report generation – extract charts to embed in dashboards.
- Compliance auditing – verify that prohibited graphics are not present in regulated documents.
- Migration pipelines – convert shapes to SVG before moving content to web‑based publishing platforms.
Performance considerations
- Release the
Watermarkerobject promptly withwatermarker.close()to free native resources. - Enable the
fastLoadflag inWordProcessingLoadOptionswhen you only need shape metadata, not full content rendering. - Process documents in parallel streams only if your server has sufficient CPU cores; avoid thread‑unsafe shared objects.
Conclusion
You now know how to extract shapes from Word documents using GroupDocs.Watermark for Java. By loading a document with Watermarker, configuring load options, and iterating through each shape, you can build powerful automation workflows that handle even the most complex files.
Next steps
- Experiment with the
Shapeobject’sgetImageData()method to export pictures as PNG. - Explore other GroupDocs.Watermark features such as watermark detection and removal.
- Combine shape extraction with the GroupDocs.Parser library to pull surrounding text for richer analysis.
Frequently asked questions
Q: What is GroupDocs.Watermark for Java?
A: GroupDocs.Watermark for Java is a comprehensive SDK that enables watermark creation, detection, and document inspection across 30+ file formats, including DOCX, PDF, and PPTX.
Q: Can I extract shapes from password‑protected Word files?
A: Yes—pass the password to WordProcessingLoadOptions when constructing the Watermarker instance.
Q: Does the library work on Linux servers?
A: Absolutely; GroupDocs.Watermark is platform‑agnostic and runs on any OS that supports Java 8+.
Q: How many shapes can be processed in a single document?
A: The SDK can handle thousands of shapes; tests show stable performance on documents with up to 5,000 individual shapes.
Q: Is a separate license needed for shape extraction?
A: No, shape extraction is included in the standard GroupDocs.Watermark license.
Last updated: 2026-09-06
Tested with: GroupDocs.Watermark 23.12 for Java
Author: GroupDocs