Convert Markdown to Image in Java – PNG, JPG, BMP, GIF, TIFF
Markdown to Image conversion can be useful where you want to create a visual representation of your content, share it on social media, embed it in websites or blog posts, or use it as part of a presentation. Aspose.HTML for Java library provides a wide range of Markdown conversions to images, such as JPG, PNG, BMP, TIFF, and GIF. Converting Markdown to any supported image format follows the mandatory steps:
- Open a Markdown file.
- Convert Markdown file to HTML document.
- Create an ImageSaveOptions object.
- Convert HTML document to the chosen image format using the Converter class.
The only difference is in specifying the output image format using the
ImageSaveOptions class. The ImageSaveOptions(format)
constructor initializes the options object with the format specifying. You can set the image format to JPG, PNG, BMP, GIF, or TIFF. The default ImageFormat
is PNG.
In this article, you will find information on how to convert a Markdown to Image using Aspose.HTML for Java and how to apply
ImageSaveOptions You can easily use Java examples to convert Markdown to images, which are detailed here for converting Markdown to PNG, JPG, BMP, GIF, and TIFF. Just set the ImageFormat
to ImageSaveOptions!
Markdown to PNG
The static methods of the Converter class are primarily used as the easiest way to convert a Markdown file into various formats. You can convert Markdown to Image in your Java application literally with a few lines of code!
- Use the
convertMarkdown(
sourcePath
) method to convert Markdown to HTML document. - Create a new
ImageSaveOptions object.
ImageFormat.Png
will be used as default image format. - Use the
сonvertHTML(document, options, savePath)
method of the Converter class to save an intermediate HTML document as a PNG image. The method takes as parametersdocument
,options
, andsavePath
and performs the conversion.
If your scenario requires rendering Markdown document, for instance, to PNG file format, the following example demonstrates how that is simple:
1// Convert Markdown to HTML
2// Prepare a simple Markdown example
3String code = "### Hello, World! \n" +
4 "[visit applications](https://products.aspose.app/html/family)";
5
6// Create a Markdown file
7FileHelper.writeAllText($o("document.md"), code);
8
9// Convert Markdown to HTML
10HTMLDocument document = Converter.convertMarkdown($o("document.md"));
11
12// Convert HTML document to JPG image file format
13Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), $o("document-output.jpg"));
Save Options – ImageSaveOptions Class
Aspose.HTML for Java provides the ability to programmatically convert Markdown to Image with full control over various conversion options using ImageSaveOptions. This feature allows users to customize the rendering process by setting the image format, page size, margins, compression level, media type, and other parameters.
Method | Description |
---|---|
setCompression(value) | Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW . |
getCss | Gets a CssOptions object which is used for configuration of CSS properties processing. |
setFormat(value) | Sets ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default this property is ImageFormat.Png . |
setBackgroundColor(value) | Sets Color which will fill background of every page. Default value is Color.Transparent(Color.getTransparent()) . |
setPageSetup(value) | Gets a page setup object is used for configuration output page-set. |
setHorizontalResolution(value) | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
setVerticalResolution(value) | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
setSmoothingMode(value) | Sets the rendering quality for this image. |
getText() | Gets a TextOptions object which is used for configuration of text rendering. |
For further information on how to customize the conversion process with ImageSaveOptions you can refer to the Fine-Tuning Converters article.
Convert Markdown to JPG using ImageSaveOptions
Aspose.HTML for Java allows converting Markdown to Image using default or custom save options. Setting different options of the ImageSaveOptions class gives you control over the page size, margins, background color, and resolution settings for the output image.
These are the steps to convert a Markdown file to JPG format with ImageSaveOptions specifying:
- Use the
convertMarkdown(
sourcePath
) method to convert Markdown to HTML document. - Create a new
ImageSaveOptions object with JPG
ImageFormat
and specify the required save options:- Use the
setSmoothingMode()
method to set the quality of the image smoothing to high. - Use the
setHorizontalResolution()
andsetVerticalResolution()
methods to set the horizontal and vertical resolution of the image to 200. - Use the
setAnyPage()
method to set the size of the image and margins.
- Use the
- Use the
convertHTML(document, options, savePath)
method to save Markdown file as a JPG image. The method takes thedocument
,options
, output file pathsavePath
and performs the conversion operation.
The following Java example shows how to use ImageSaveOptions
and create a JPG file with custom page-size, margins, resolution, and rendering quality:
1// Convert Markdown to HTML
2HTMLDocument document = Converter.convertMarkdown($i("nature.md"));
3
4// Initialize ImageSaveOptions
5ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
6options.setSmoothingMode(SmoothingMode.HighQuality);
7options.setHorizontalResolution(Resolution.to_Resolution(200));
8options.setVerticalResolution(Resolution.to_Resolution(200));
9options.setBackgroundColor(Color.getAliceBlue());
10options.getPageSetup().setAnyPage(new Page(new Size(600, 950), new Margin(30, 20, 10, 10)));
11
12// Convert HTML to JPG
13Converter.convertHTML(document, options, $o("nature-options.jpg"));
Conclusion
Converting Markdown to image formats such as PNG, JPG, BMP, GIF, or TIFF is a practical approach for visually representing content in presentations, social media posts, or web pages. Aspose.HTML for Java provides a robust API to achieve this goal easily and flexibly. Using the ImageSaveOptions
class, developers can customize image properties such as format, allowing for seamless selection between supported image formats. In addition, the API supports fine-tuning output properties such as resolution, margins, background color, rendering quality, etc., to meet specific user requirements.
Converting Markdown to image formats involves an important intermediate step: converting Markdown to HTML. Aspose.HTML for Java facilitates this process with its Converter
class, ensuring accurate HTML generation as a basis for subsequent conversion to image.
You can download the complete examples and data files from GitHub.
Aspose.HTML offers a free online Markdown to Image Converter that converts Markdown to JPG with high quality, easy and fast. If you need to include Markdown files in a PowerPoint presentation or send them by email, please transform them to the appropriate image format and use them as you want! Just upload, convert your files and get the result in a few seconds!