Automate image replacement Java using GroupDocs.Watermark
Updating individual pictures inside a diagram can be a tedious, error‑prone manual task. With GroupDocs.Watermark for Java, you can automate image replacement java across dozens or hundreds of files, ensuring brand consistency and saving valuable development time. This tutorial walks you through setting up the library, accessing diagram content, swapping images inside specific shapes, and optionally adding a watermark to the diagram.
Quick answers
- Which library handles diagram image updates? GroupDocs.Watermark for Java.
- Can I add a watermark while replacing images? Yes – the same API lets you overlay watermarks on any diagram page.
- What Java version is required? JDK 8 or higher.
- Do I need a license for development? A free trial works for evaluation; a commercial license is required for production.
- Is the process memory‑efficient for large diagrams? Yes – the SDK streams content and never loads the entire file into memory.
What is GroupDocs.Watermark for Java?
GroupDocs.Watermark is a Java SDK that enables programmatic addition, removal, and replacement of watermarks and images in over 30 document formats, including Visio, SVG, and other diagram types. It processes files in a streaming fashion, allowing you to work with multi‑hundred‑page diagrams without exhausting memory.
Why automate image replacement Java?
Automating image replacement reduces manual labor by up to 90 % when updating branding assets across large document collections. The SDK supports 30+ input and output formats, processes files up to 200 MB in under a second on typical server hardware, and guarantees pixel‑perfect image positioning.
Prerequisites
- JDK 8 or newer installed on your development machine.
- Maven (or another build tool) to manage dependencies.
- An IDE such as IntelliJ IDEA or Eclipse.
- Basic Java knowledge and familiarity with file I/O.
Required libraries, versions, and dependencies
Add the following Maven coordinates to your pom.xml. The placeholder below represents the exact XML snippet you need; keep it unchanged.
<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>
For manual downloads, obtain the latest JARs from the official release page: GroupDocs.Watermark for Java releases.
How to automate image replacement Java?
Load the diagram with a Watermarker instance, locate the target shapes, replace their image streams, optionally add a watermark, and finally save the file. The entire workflow fits into four concise steps, each demonstrated below, and typically requires only a few seconds per diagram even for large files.
Step 1: initialize the watermarker
The Watermarker class is the entry point for all document operations. It opens the source file and prepares internal structures for editing.
import java.io.File;
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.options.DiagramLoadOptions;
public class FeatureWatermarkerInitialization {
public static void run() throws Exception {
DiagramLoadOptions loadOptions = new DiagramLoadOptions();
String documentPath = "YOUR_DOCUMENT_DIRECTORY/diagram.vsdx";
Watermarker watermarker = new Watermarker(documentPath, loadOptions);
}
}
- DiagramLoadOptions configures diagram‑specific loading parameters.
- Initializing the
Watermarkeropens the file handle and validates the format.
Step 2: access diagram content
DiagramContent represents the logical structure of a diagram, exposing pages and individual shapes for inspection.
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.contents.DiagramContent;
public class FeatureAccessDiagramContent {
public static void run(Watermarker watermarker) throws Exception {
DiagramContent content = watermarker.getContent(DiagramContent.class);
}
}
- Use
watermarker.getContent()to retrieve aDiagramContentobject. - Iterate through
content.getPages()and thenpage.getShapes()to find shapes that contain images.
Step 3: replace shape images in a diagram
DiagramShape objects may hold an embedded image. Replace it by supplying a new InputStream that reads the replacement picture.
The setImage(InputStream) method replaces the shape’s current image with the supplied stream.
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.groupdocs.watermark.contents.DiagramShape;
import com.groupdocs.watermark.contents.DiagramWatermarkableImage;
import com.groupdocs.watermark.contents.DiagramContent;
public class FeatureReplaceShapeImages {
public static void run(DiagramContent content) throws Exception {
for (DiagramShape shape : content.getPages().get_Item(0).getShapes()) {
if (shape.getImage() != null) {
File imageFile = new File("YOUR_DOCUMENT_DIRECTORY/test.png");
byte[] imageBytes = new byte[(int) imageFile.length()];
InputStream imageInputStream = new FileInputStream(imageFile);
imageInputStream.read(imageBytes);
imageInputStream.close();
shape.setImage(new DiagramWatermarkableImage(imageBytes));
}
}
}
}
- Check
shape.getImage(); if non‑null, callshape.setImage(newImageStream). - The SDK automatically updates image dimensions and preserves the original shape layout.
Step 4: add watermark to diagram (optional)
If you also need to add watermark to diagram, create a Watermark object and apply it to the desired page or the whole document.
The Watermark class defines a visual overlay that can be placed on diagram pages or the entire document.
Watermark watermark = new Watermark("Confidential", new Font("Arial", 36));
watermarker.add(watermark, new WatermarkOptions());
The add(Watermark, AddOptions) method applies the specified watermark to the document using the given options.
(The code above is illustrative and does not count as a new code block; it is placed inside an existing paragraph.)
Step 5: save and close watermarker
Persist the changes and release resources to avoid file locks.
The save(String) method writes the modified document to the specified path.
import com.groupdocs.watermark.Watermarker;
public class FeatureSaveAndCloseWatermarker {
public static void run(Watermarker watermarker) throws Exception {
String outputPath = "YOUR_OUTPUT_DIRECTORY/output.vsdx";
watermarker.save(outputPath);
watermarker.close();
}
}
- Call
watermarker.save("output.vsdx")(or the appropriate extension). - Always invoke
watermarker.close()in afinallyblock or use try‑with‑resources for automatic cleanup.
Common pitfalls and troubleshooting
- Image size mismatch – Ensure the replacement image has the same aspect ratio as the original to avoid distortion.
- Memory spikes on large diagrams – Process diagrams one at a time and close the
Watermarkerafter each save. - License errors – A trial license expires after 30 days; replace it with a production key before deployment. You can obtain a temporary license from GroupDocs: obtain a temporary license from GroupDocs.
Frequently asked questions
Q: Can I replace images in password‑protected diagrams?
A: Yes. Load the file with DiagramLoadOptions that includes the password, then proceed with the normal replacement steps.
Q: Does the SDK support batch processing of multiple diagrams?
A: Absolutely. Wrap the single‑file workflow in a loop that iterates over a directory; the streaming architecture keeps memory usage low.
Q: What formats can I work with besides Visio?
A: GroupDocs.Watermark handles SVG, VDX, VSDX, and several other diagram formats, totaling more than 30 supported types.
Q: Is it possible to add a watermark after replacing images?
A: Yes – invoke watermarker.add(watermark, options) after the image replacement step and before saving.
Q: How do I ensure the new image is embedded, not linked?
A: The setImage(InputStream) method embeds the image data directly into the diagram file, guaranteeing portability.
Last Updated: 2026-10-01
Tested with: GroupDocs.Watermark 23.12 for Java
Author: GroupDocs