Convert SVG to PNG in Java – Aspose.HTML for Java

Each image format has unique strengths and weaknesses. SVG is great for creating graphics that can be scaled up or down without losing quality. But sometimes, you need to convert SVG files to other image formats like PNG, JPG, BMP, TIFF, or GIF. Overall, SVG conversions can be helpful for developers and designers who must work with different image formats for various purposes. In addition, knowing how to convert between formats can help ensure your graphics and images are accessible and usable for a wider audience.

Aspose.HTML for Java library provides a wide range of SVG conversions to images, such as JPG, PNG, BMP, TIFF, and GIF. Converting SVG to any supported image format follows the mandatory steps:

To specify the output image format, use 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 learn how to convert an SVG to PNG using Aspose.HTML for Java and how to apply ImageSaveOptions. You can easily use Java examples to convert SVG to PNG, which are detailed here for converting SVG to JPG, BMP, GIF, and TIFF images. Just set the required ImageFormat to ImageSaveOptions!

Convert SVG to PNG by a single line of code

By converting SVG to PNG, you get a raster image that can be easily shared, viewed, sent by email, and compressed without losing quality. The static methods of the Converter class are primarily used as the easiest way to convert an SVG code into various formats. You can convert SVG to PNG in your Java application literally with a single line of code!

1// Invoke the ConvertSVG() method for SVG to JPG conversion
2Converter.convertSVG($i("shapes.svg"), new ImageSaveOptions(ImageFormat.Png), "convert-with-single-line.png");

Convert SVG to PNG

In the following Java example, we will walk through the step-by-step instructions for converting SVG to PNG with default save options:

  1. Load an SVG file. You can load SVG from a file, SVG code, or URL. In the following example, we prepare SVG code to create SVG from scratch and pass it directly to the convertSVG() method.
  2. Use the ImageSaveOptions() constructor to create a new ImageSaveOptions object. By default, the Format property is PNG.
  3. Call one of the convertSVG() methods of the Converter class to save SVG as a PNG image. In the example, we use the convertSVG(document, options, outputPath) method that takes three parameters: a source SVG document, an instance of the ImageSaveOptions class, and the output file path where the converted image will be saved.

The following Java code snippet shows how to convert SVG to PNG using Aspose.HTML for Java.

 1// Prepare an SVG code and save it to the file.
 2String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
 3        "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
 4        "</svg>\n";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
 6    fileWriter.write(code);
 7}
 8
 9// Initialize an SVG document from the svg file.
10SVGDocument document = new SVGDocument("document.svg");
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
14
15// Convert SVG to PNG
16Converter.convertSVG(document, options, "output.png");

You can download the complete examples and data files from GitHub.

Save Options – ImageSaveOptions Class

The ImageSaveOptions class provides methods that give you full control over a wide range of parameters and improve the process of converting SVG to image file formats. You can specify the image format, page size, margins, compression level, media type, etc.

MethodDescription
setCompression(value)Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW.
getCssGets 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.

Convert SVG to PNG using ImageSaveOptions

If your scenario requires rendering an SVG document, for instance, to PNG file format with custom save options, the following example demonstrates how that is simple:

  1. Load an SVG file using the SVGDocument class.
  2. Create a new ImageSaveOptions object and specify the required save options. In the following example, we apply custom resolutions, set smoothing mode and background color for the resulting PNG image:
    • Use the setHorizontalResolution() and setVerticalResolution() methods to set the horizontal and vertical resolution of the image to 200.
    • Use the setBackgroundColor() method to set the background color for every page.
    • Use the setSmoothingMode() method to set the quality of the image smoothing to high.
  3. Use the convertSVG(document, options, savePath) method of Converter class to save SVG as a PNG image.
 1// Create an instance of the ImageSaveOptions class. Set up the SmoothingMode, resolutions, and change the background color to AliceBlue
 2ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
 3options.setHorizontalResolution(Resolution.to_Resolution(200));
 4options.setVerticalResolution(Resolution.to_Resolution(200));
 5options.setBackgroundColor(Color.getAliceBlue());
 6options.setSmoothingMode(SmoothingMode.HighQuality);
 7
 8// Initialize an SVG document from the file
 9final SVGDocument document = new SVGDocument($i("flower1.svg"));
10
11// Convert SVG to PNG
12Converter.convertSVG(document, options, $i("flower-options.png"));

Conclusion

The Aspose.HTML for Java library offers a powerful and efficient solution for converting SVG to various raster image formats, including PNG, JPG, BMP, TIFF, and GIF. Using the Converter and ImageSaveOptions classes allows the conversion of SVG and precise customization of output properties like image format, resolution, compression, and background color. This level of control enables the optimization of output quality and tailors the results to specific project needs, ensuring that the converted images meet both functional and aesthetic requirements.

By following the technical steps – loading an SVG document, configuring ImageSaveOptions, and invoking the convertSVG() method – you can achieve simple, quick, and quality format conversions.

Aspose.HTML offers a free online SVG to PNG Converter that converts SVG to PNG with high quality, easy and fast. This tool helps you create high-quality images and graphics that are compatible with a wide range of platforms and devices. So why not try? Just upload, convert your files and get the result in a few seconds!

Text “SVG to PNG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.