Convert Markdown to Image in Java

To convert Markdown to an image in Java, call Converter.convertMarkdown() to obtain an HTMLDocument, create ImageSaveOptions with the required ImageFormat, and pass both to Converter.convertHTML(). Supported output formats include PNG, JPG, BMP, GIF, and TIFF.

Markdown-to-image conversion creates a visual copy of Markdown content for documentation, presentations, previews, and sharing. Aspose.HTML for Java uses HTML as the intermediate document and ImageSaveOptions to select the image format and configure resolution, page setup, background color, rendering quality, and TIFF compression.

Convert Markdown to PNG

The parameterless ImageSaveOptions() constructor selects PNG by default. The example below specifies ImageFormat.Png explicitly:

  1. Prepare the Markdown source.
  2. Call Converter.convertMarkdown() to obtain an HTMLDocument.
  3. Create ImageSaveOptions with ImageFormat.Png.
  4. Call Converter.convertHTML() with the document, options, and output path.

The sample writes Markdown content to document.md and converts it to output_md.png.

 1// Convert Markdown to PNG using Java
 2
 3// Prepare a simple Markdown example
 4String code = "### Hello, World\n\n" +
 5        "[visit applications](https://products.aspose.app/html/family)";
 6
 7// Create a Markdown file
 8try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.md")) {
 9    fileWriter.write(code);
10}
11
12// Convert Markdown to HTML document
13HTMLDocument document = Converter.convertMarkdown("document.md");
14
15// Convert HTML document to PNG image file format
16Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Png), "output_md.png");

Customize Image Output with ImageSaveOptions

Use ImageSaveOptions to select the output format and control image rendering.

MethodUse it to
setFormat(value)Select JPG, PNG, BMP, GIF, or TIFF. The default format is PNG.
setBackgroundColor(value)Set the color that fills each output page. The default is transparent.
getPageSetup()Access the page setup used to configure output page size and margins.
setHorizontalResolution(value)Set horizontal resolution in pixels per inch. The default is 300 dpi.
setVerticalResolution(value)Set vertical resolution in pixels per inch. The default is 300 dpi.
setSmoothingMode(value)Configure image rendering quality.
setCompression(value)Select TIFF compression. The default TIFF compression is LZW.
getCss()Access CSS processing options.
getText()Access text rendering options.

For additional rendering controls, see Fine-Tuning Converters.

Convert Markdown to JPG with ImageSaveOptions

JPG uses lossy compression and is useful when file size is more important than transparency or lossless detail. To customize JPG output:

  1. Convert the source Markdown file to an HTMLDocument with Converter.convertMarkdown().
  2. Create ImageSaveOptions with ImageFormat.Jpeg.
  3. Configure the required resolution, background color, rendering quality, page size, and margins.
  4. Pass the document, options, and output path to Converter.convertHTML().

The following example converts nature.md to nature-options.jpg. It uses 200 dpi horizontal and vertical resolution, an Alice Blue background, high-quality smoothing, and a 600 × 950 pixel page with custom margins.

 1// Convert Markdown to JPG in Java with custom settings
 2
 3// Convert Markdown to HTML
 4HTMLDocument document = Converter.convertMarkdown("nature.md");
 5
 6// Initialize ImageSaveOptions
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
 8options.setSmoothingMode(SmoothingMode.HighQuality);
 9options.setHorizontalResolution(Resolution.to_Resolution(200));
10options.setVerticalResolution(Resolution.to_Resolution(200));
11options.setBackgroundColor(Color.getAliceBlue());
12options.getPageSetup().setAnyPage(new Page(new Size(600, 950), new Margin(30, 20, 10, 10)));
13
14// Convert HTML to JPG
15Converter.convertHTML(document, options, "nature-options.jpg");

Download complete Java examples and data files from GitHub.

Related Markdown Conversion Guides

Other Platforms

FAQ

Can I convert Markdown to an image for free?

Yes. Use the free online Markdown to Image Converter for a quick manual conversion. Aspose.HTML for Java is a commercial API for programmatic conversion and can be evaluated before licensing; see Licensing.

Which image format should I choose for Markdown output?

Choose PNG when text sharpness, lossless output, or transparency is important. Choose JPG when a smaller file is more important and transparency is not required. Use BMP when a simple bitmap format is required, TIFF for archival or configurable compression workflows, and GIF when that format is required by the target system. These outputs are static raster images.

Try Online Markdown to Image Conversion

Use the free online Markdown to Image Converter for quick manual conversion without writing Java code.

Free Online Markdown to Image Converter