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:
importcom.groupdocs.markdown.*;importcom.groupdocs.markdown.imageexport.IImageSavingHandler;publicclassCustomStrategyRename{// Implement the IImageSavingHandler interface
staticclassRenameHandlerimplementsIImageSavingHandler{privateintindex;@Overridepublicvoidhandle(CustomImageSavingArgsargs){args.setOutputImageFileName("img_"+index+"_"+args.getImageFileName());index++;}}publicstaticvoidmain(String[]args){// Use the handler with CustomImagesStrategy
RenameHandlerhandler=newRenameHandler();DocumentConvertOptionsoptions=newDocumentConvertOptions();options.setImageExportStrategy(newCustomImagesStrategy("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.
Use setReplacementImage to substitute the original image with different content (e.g., a watermarked version or a placeholder):
importcom.groupdocs.markdown.*;importcom.groupdocs.markdown.imageexport.IImageSavingHandler;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;publicclassCustomStrategyReplace{staticclassWatermarkHandlerimplementsIImageSavingHandler{@Overridepublicvoidhandle(CustomImageSavingArgsargs){try{// Replace the original image with a custom placeholder
InputStreamplaceholder=newFileInputStream("placeholder.png");args.setReplacementImage(placeholder);args.setOutputImageFileName("placeholder.png");}catch(IOExceptione){thrownewIllegalStateException("placeholder.png not found",e);}}}publicstaticvoidmain(String[]args){WatermarkHandlerhandler=newWatermarkHandler();DocumentConvertOptionsoptions=newDocumentConvertOptions();options.setImageExportStrategy(newCustomImagesStrategy("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.
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.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.