This brief topic is about how to convert SVG to PNG using Java by providing detailed steps required to configure the environment and execute the code. Using Java SVG to PNG can be easily managed using a few lines of code and it is suitable for all types of Java-based applications running on Windows, Linux or macOS platforms.
Steps to Convert SVG to PNG using Java
- Configure the project by adding the Aspose.Imaging library from the Maven Repository to convert SVG to PNG
- Import Aspose.Imaging namespace to use API calls in the code
- Load the sample SVG image using Image class load() method
- Initialize SvgRasterizationOptions object to set the SVG image height and width
- Create PngOptions object for the desired PNG
- Save the converted PNG file on disk
The aforementioned steps in Java convert SVG to PNG with the help of simple API interface and commence with loading of the source SVG file using the load() method exposed by the Image class. The SvgRasterizationOptions class will be used to set the SVG page width and height. In the end, the SvgRasterizationOptions object will be passed on to the PngOptions instance for rendering and saving the SVG as a PNG image.
Code to Convert SVG to PNG using Java
For developing the converter for SVG to PNG Java codebase has been effectively utilized with the help of simple API calls. We can perform the alteration in the output by setting additional properties of SvgRasterizationOptions class including set border, scaling factors, and background color. Also, the PngOptions class exposes the options to set the resolution settings, compression level and filter types, etc.
The example taught that in order to convert SVG to PNG Java based applications can be developed on any platform including MS Windows, macOS or Linux. If you want to learn about setting a watermark for a Tiff image, refer to the article on how to set watermark in TIFF using Java.