Convert SVG to JPG in Java
In this article, you learn how to convert an SVG to JPG using Aspose.HTML for Java and how to apply
ImageSaveOptions. You can easily use Java examples to convert SVG to JPG, which are detailed here for converting
SVG to PNG, BMP, GIF, and TIFF images. Just set the required ImageFormat
to ImageSaveOptions!
Converting SVG to any supported image format follows the mandatory steps:
- Open an SVG file.
- Create an ImageSaveOptions object and specify the image format.
- Convert SVG to the chosen image format using the Converter class.
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, TIFF, and GIF. The default ImageFormat
is PNG.
Convert SVG to JPG with a few lines of code
Converting SVG to JPG gives you a raster image that can be easily shared, viewed, or emailed. 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 JPG in your Java application literally with a few lines of code!
In the example, we use the convertSVG(content, baseUri, options, outputPath)
method of the
Converter class that takes four parameters: string with SVG code to be converted, the base folder for the input SVG file, an instance of the ImageSaveOptions class, and the output file path where the converted image will be saved:
1// Prepare SVG code
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";
5
6// Invoke the convertSVG() method to convert SVG to image
7Converter.convertSVG(code, ".", new ImageSaveOptions(ImageFormat.Jpeg), "output.jpg");
Convert SVG to JPG
SVG files are great for website graphics, but not all web browsers support them. Converting SVG to JPG ensures that the image will load properly on any web browser, allowing for a better user experience. Moreover, JPG images are easy to share, send by email, embed in reports or presentations, etc. In the following Java example, we will walk through the step-by-step instructions for converting SVG to JPG with default save options:
- 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 use the SVGDocument() constructor to initialize an SVGDocumet instance.
- Use the ImageSaveOptions() constructor to create a new ImageSaveOptions object.
- Call one of the
convertSVG()
methods of the Converter class to save SVG as a JPG image. In the example, we use the convertSVG(document
,options
,outputPath
) method.
The following Java code snippet shows how to convert SVG to JPG using Aspose.HTML for Java:
1// Prepare SVG code and save it to a 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.Jpeg);
14
15// Convert SVG to JPG
16Converter.convertSVG(document, options, "output.jpg");
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.
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. |
You can download the complete examples and data files from GitHub.
To learn more about ImageSaveOptions please read Fine-Tuning Converters article.
Convert SVG to JPG using ImageSaveOptions
If your scenario requires rendering an SVG document, for instance, to JPG file format with custom save options, the following example demonstrates how that is simple:
- Load an SVG file using the SVGDocument class.
- Create a new
ImageSaveOptions object and specify the required save options. In the following example, we apply a custom page size, set smoothing mode and background color for the resulting JPG image:
- Use methods of the
Page
class to configure the output page. - 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.
- Use methods of the
- Use the
convertSVG(
sourcePath
,options
,outputPath
) method of Converter class to save SVG as a JPG image.
1// Prepare SVG code and save it to a file
2String code =
3 "<svg width=\"450\" height=\"450\" xmlns=\"http://www.w3.org/2000/svg\">" +
4 " <g fill=\"RoyalBlue\">" +
5 " <rect x=\"100\" y=\"100\" rx=\"25\" ry=\"25\" width=\"200\" height=\"56\" />" +
6 " <rect x=\"100\" y=\"100\" rx=\"25\" ry=\"25\" width=\"200\" height=\"56\" transform =\"rotate(90 200 128)\" />" +
7 " <rect x=\"100\" y=\"100\" rx=\"25\" ry=\"25\" width=\"200\" height=\"56\" transform =\"rotate(-45 200 128)\" />" +
8 " <rect x=\"100\" y=\"100\" rx=\"25\" ry=\"25\" width=\"200\" height=\"56\" transform =\"rotate(45 200 128)\" />" +
9 " </g>" +
10 " <circle cx=\"200\" cy=\"128\" r=\"28\" stroke=\"pink\" stroke-width=\"50\" stroke-dasharray=\"3 13\" fill=\"Orange\" />" +
11 " <circle cx=\"200\" cy=\"128\" r=\"5\" />" +
12 "</svg>";
13
14try (java.io.FileWriter fileWriter = new java.io.FileWriter("flower.svg")) {
15 fileWriter.write(code);
16}
17
18// Initialize ImageSaveOptions and set up smoothing mode, page size, and background color
19ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
20PageSetup pageSetup = new PageSetup();
21options.setSmoothingMode(SmoothingMode.HighQuality);
22Page anyPage = new Page();
23anyPage.setSize(new Size(Length.fromPixels(200), Length.fromPixels(200)));
24pageSetup.setAnyPage(anyPage);
25options.setPageSetup(pageSetup);
26options.setBackgroundColor(Color.getAliceBlue());
27
28// Call the convertSVG() method to convert the "flower.svg" file to a JPEG image
29Converter.convertSVG("flower.svg", options, "flower.jpg");
Check the quality of SVG to JPG conversion with our online SVG to JPG Converter. Upload, convert your files and get the result in a few seconds. Try our forceful SVG to JPG Converter for free now!