Convert SVG to PNG in Java

Aspose.HTML for Java converts SVG code, files, URLs, and SVGDocument objects to PNG. Pass the source, ImageSaveOptions with ImageFormat.Png, and output path to a suitable Converter.convertSVG() overload.

SVG stores scalable vector graphics, while PNG is a lossless raster format that supports transparency. Convert SVG to PNG when an application, website, or document workflow requires a bitmap image at a defined resolution.

Convert an SVG File to PNG in One Line

The Converter.convertSVG() method can accept an SVG file path, image save options, and an output path in one call. The following example converts shapes.svg to convert-with-single-line.png.

1// Convert SVG to PNG in one line using Java
2
3// Invoke the convertSVG() method for SVG to PNG conversion
4Converter.convertSVG("shapes.svg", new ImageSaveOptions(ImageFormat.Png), "convert-with-single-line.png");

Convert an SVGDocument to PNG

Follow these steps to convert an SVG document to PNG:

  1. Load the SVG source into an SVGDocument.
  2. Create ImageSaveOptions and select ImageFormat.Png.
  3. Call Converter.convertSVG() with the document, save options, and output path.

The following example writes SVG markup to document.svg, loads it as an SVGDocument, and converts it to output.png.

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

Configure SVG to PNG Conversion with ImageSaveOptions

The ImageSaveOptions class controls raster output. The same class supports PNG, JPG, BMP, GIF, and TIFF by changing the ImageFormat value.

MethodPurpose
setFormat(value)Selects the output image format.
setPageSetup(value)Sets the output page size and margins.
setBackgroundColor(value)Sets the color behind the converted SVG content.
setHorizontalResolution(value)Sets horizontal output resolution in pixels per inch.
setVerticalResolution(value)Sets vertical output resolution in pixels per inch.
setSmoothingMode(value)Sets the smoothing quality used during rasterization.

To convert SVG to PNG with custom settings:

  1. Load the SVG source.
  2. Create ImageSaveOptions for PNG and configure the required resolution, background, and smoothing settings.
  3. Call Converter.convertSVG() with the document, configured options, and output path.

The next example converts flower1.svg to flower-options.png. It sets horizontal and vertical resolution to 200 dpi, fills the background with Alice Blue, and enables high-quality smoothing.

 1// Convert SVG to PNG in Java with custom settings
 2
 3// Create an instance of the ImageSaveOptions class. Set up the SmoothingMode, resolutions, and background color
 4ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
 5options.setHorizontalResolution(Resolution.to_Resolution(200));
 6options.setVerticalResolution(Resolution.to_Resolution(200));
 7options.setBackgroundColor(Color.getAliceBlue());
 8options.setSmoothingMode(SmoothingMode.HighQuality);
 9
10// Initialize an SVG document from a file
11final SVGDocument document = new SVGDocument("flower1.svg");
12
13// Convert SVG to PNG
14Converter.convertSVG(document, options, "flower-options.png");

Download complete Java examples and data files from GitHub. See Fine-Tuning Converters for additional conversion settings.

Related Resources

Other Platforms

Frequently Asked Questions

Can I convert SVG to PNG for free?

You can use the free online SVG to PNG Converter for occasional manual conversions. Aspose.HTML for Java is a commercial API; you can evaluate it before licensing it for production use.

Does SVG to PNG conversion preserve transparency?

PNG supports transparency. Transparent SVG areas can remain transparent unless the SVG content or ImageSaveOptions.setBackgroundColor() supplies an opaque background.

Try Online SVG to PNG Conversion

Use the free online SVG to PNG Converter for quick manual conversion without installing software. Use Aspose.HTML for Java when SVG to PNG conversion must run programmatically.

SVG to PNG Converter