IMarkdownImageSavingCallback
Contents
[
Hide
]
IMarkdownImageSavingCallback interface
Handles custom processing of images while saving to Markdown. Invoked once per image; mutate MarkdownImageSavingArgs to control the URI embedded in the Markdown output and/or redirect where the image bytes are written.
public interface IMarkdownImageSavingCallback
Methods
| Name | Description |
|---|---|
| ImageSaving(MarkdownImageSavingArgs) | Called for each image being written to the Markdown document. |
Examples
Scenario 1 — capture image bytes in memory and embed placeholder ids (useful when the caller wants to store images elsewhere or post-process them):
class CaptureImagesCallback : IMarkdownImageSavingCallback
{
private int _index;
private readonly Dictionary<string, MemoryStream> _images;
public CaptureImagesCallback(Dictionary<string, MemoryStream> images) => _images = images;
public void ImageSaving(MarkdownImageSavingArgs args)
{
var id = $"image{_index++}";
var buffer = new MemoryStream();
_images[id] = buffer;
args.ImageStream = buffer; // redirect image bytes into our buffer
args.ImageFileName = id; // placeholder URI written into the .md
args.KeepImageStreamOpen = true; // keep buffer readable after Convert() returns
}
}
var captured = new Dictionary<string, MemoryStream>();
try
{
var options = new WordProcessingConvertOptions { Format = WordProcessingFileType.Md };
options.MarkdownOptions.ImageSavingCallback = new CaptureImagesCallback(captured);
using var converter = new Converter("source.pdf");
converter.Convert("output.md", options);
// captured["image0"], captured["image1"], ... now hold the image bytes
}
finally
{
foreach (var s in captured.Values) s.Dispose(); // caller owns the streams
}
Scenario 2 — persist images to disk alongside the .md and reference them by file name:
class FileImagesCallback : IMarkdownImageSavingCallback
{
private readonly string _outputFolder;
private int _index;
public FileImagesCallback(string outputFolder) => _outputFolder = outputFolder;
public void ImageSaving(MarkdownImageSavingArgs args)
{
var fileName = $"image{_index++}.png";
args.ImageStream = new FileStream(Path.Combine(_outputFolder, fileName), FileMode.Create);
args.ImageFileName = fileName; // written into the .md as 
// KeepImageStreamOpen left at default (false) → the converter flushes and closes the file.
}
}
var options = new WordProcessingConvertOptions { Format = WordProcessingFileType.Md };
options.MarkdownOptions.ImageSavingCallback = new FileImagesCallback("./out");
using var converter = new Converter("source.pdf");
converter.Convert("./out/output.md", options);
// ./out/image0.png, ./out/image1.png, ... are written and closed by the converter.
See Also
- namespace GroupDocs.Conversion.Options.Convert
- assembly GroupDocs.Conversion