Use batik to convert svg files in java

Source: Internet
Author: User

Svg is a vector image format used to store high-fidelity images. We can open svg in the editor. We can see that the svg file is actually an xml file, which can also be identified by the file browser. Therefore, you can use a ready-made browser to view svg. Fortunately, java also provides development kits to convert svg files. Currently, most of them are svg to pdf, jpg, and png files. this development kit is batik. If necessary, you can directly download it from Baidu and input batik. Next, let's share my recent experience with batik. for future use convenience, I have written the conversion operation as a tool class, as shown below. Before development, all the jar packages under the lib directory of batik will be imported to the project.

Package org. lxh; import java. io. bufferedOutputStream; import java. io. byteArrayInputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import org. apache. batik. transcoder. transcoder; import org. apache. batik. transcoder. transcoderException; import org. apache. batik. transcoder. transcoderInput; import org. apache. batik. transcoder. transcoderOutput; import org. apache. fop. svg. using Transcoder; import org. apache. batik. transcoder. image. PNGTranscoder;/*** svg Conversion Tool class (the following method is sufficient for development) * @ param svg * @ param pdf * @ throws IOException * @ throws TranscoderException */public class SvgUtil {// convert the svg File to public static void convertSvgFile2Pdf (File svg, File pdf) throws IOException, TranscoderException {InputStream in = new FileInputStream (svg); OutputStream out = new FileOutputStream (pdf); out = new BufferedOutputStream (out); convert2Pdf (in, out );} public static void convert2Pdf (InputStream in, OutputStream out) throws IOException, TranscoderException {Transcoder transcoder = new Transcoder (); try {TranscoderInput input = new TranscoderInput (in ); try {TranscoderOutput output = new TranscoderOutput (out); transcoder. transcode (input, output);} finally {out. close () ;}finally {in. close () ;}// convert svg to png public static void convertSvgFile2Png (File svg, File pdf) throws IOException, TranscoderException {InputStream in = new FileInputStream (svg ); outputStream out = new FileOutputStream (pdf); out = new BufferedOutputStream (out); convert2PNG (in, out);} public static void convert2PNG (InputStream in, OutputStream out) throws IOException, transcoderException {Transcoder transcoder = new PNGTranscoder (); try {TranscoderInput input = new TranscoderInput (in); try {TranscoderOutput output = new TranscoderOutput (out); transcoder. transcode (input, output);} finally {out. close () ;}finally {in. close () ;}// convert String to pdf public static void convertStr2Pdf (String svg, File pdf) throws IOException, TranscoderException {InputStream in = new ByteArrayInputStream (svg. getBytes (); OutputStream out = new FileOutputStream (pdf); out = new BufferedOutputStream (out); convert2Pdf (in, out );}}

 

2. Prepare the svg file below. You can find this file under the samples directory of the development kit. The usage is as simple as follows:
Package org. lxh; import java. io. file; public class ConvertSvg {public static void main (String [] args) throws Exception {// Note: if an svg String is used for conversion to pdf, an Encoding Error may occur, replace the UTF-8 in the string with GBK File f = new File ("src/sun. svg "); File destFile = new File (" src/sunbench "); SvgUtil. convertSvgFile2Pdf (f, destFile );}}

 

To see the converted file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.