Save image by custom strategy

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Markdown MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-markdown -- dnx GroupDocs.Markdown.Mcp --yes
Folders, licensing and more →

Use CustomImagesStrategy with the IImageSavingHandler interface for full control over how each image is saved during conversion. You can rename files, redirect output to a custom stream, or replace image content entirely.

Rename images with IImageSavingHandler

IImageSavingHandler is a functional interface, so a stateless handler can be written as a lambda. This one keeps a running counter, so it is written as a named class:

import com.groupdocs.markdown.*;
import com.groupdocs.markdown.imageexport.IImageSavingHandler;

public class CustomStrategyRename {

    // Implement the IImageSavingHandler interface
    static class RenameHandler implements IImageSavingHandler {

        private int index;

        @Override
        public void handle(CustomImageSavingArgs args) {
            args.setOutputImageFileName("img_" + index + "_" + args.getImageFileName());
            index++;
        }
    }

    public static void main(String[] args) {
        // Use the handler with CustomImagesStrategy
        RenameHandler handler = new RenameHandler();

        DocumentConvertOptions options = new DocumentConvertOptions();
        options.setImageExportStrategy(new CustomImagesStrategy("output/images", handler));

        MarkdownConverter.toFile("business-plan.docx", "output/document.md", options);
    }
}

business-plan.docx is a sample file used in this example. Click here to download it.

output/document.md (5 KB)
output/images/img_0_img-001.png (4 KB)
output/images/img_1_img-002.jpg (41 KB)
output/images/img_2_img-003.png (14 KB)

Download full output

Replace image content with setReplacementImage

Use setReplacementImage to substitute the original image with different content (e.g., a watermarked version or a placeholder):

import com.groupdocs.markdown.*;
import com.groupdocs.markdown.imageexport.IImageSavingHandler;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class CustomStrategyReplace {

    static class WatermarkHandler implements IImageSavingHandler {

        @Override
        public void handle(CustomImageSavingArgs args) {
            try {
                // Replace the original image with a custom placeholder
                InputStream placeholder = new FileInputStream("placeholder.png");
                args.setReplacementImage(placeholder);
                args.setOutputImageFileName("placeholder.png");
            } catch (IOException e) {
                throw new IllegalStateException("placeholder.png not found", e);
            }
        }
    }

    public static void main(String[] args) {
        WatermarkHandler handler = new WatermarkHandler();

        DocumentConvertOptions options = new DocumentConvertOptions();
        options.setImageExportStrategy(new CustomImagesStrategy("output/images", handler));

        MarkdownConverter.toFile("business-plan.docx", "output/document.md", options);
    }
}

business-plan.docx is a sample file used in this example. Click here to download it.

output/document.md (5 KB)
output/images/placeholder.png (14 KB)

Download full output

Note
Note the stream direction. setReplacementImage takes an InputStream – the library reads your replacement bytes from it. setOutputStream, by contrast, takes an OutputStream, because the library writes the image into it. C# blurs this distinction behind a single Stream type; Java makes it explicit.