Using Batik to manipulate SVG

Source: Internet
Author: User

Batik Introduction

Batik is a Java toolkit for applications that want to use SVG to implement a variety of functions. With batik, you can manipulate SVG documents anywhere you use Java, or you can use the Batik module to generate, manipulate, and convert SVG images in applications or applets.

With batik, it's easy to manipulate SVG content, allowing Java applications to have a simple output image formatted as SVG using the Batik svggernerate module, with batik svg viewing Component enables Java applications to integrate SVG viewing and interactivity capabilities, as well as to convert SVG formats to other formats, such as JPEG and PDF, using Batik's modules.

Batik Use

Use instance one: Generate SVG documents with batik

Package test;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.Rectangle;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import java.io.UnsupportedEncodingException;
 
Import Java.io.Writer;
Import org.apache.batik.dom.GenericDOMImplementation;
Import Org.apache.batik.svggen.SVGGraphics2D;
Import org.apache.batik.svggen.SVGGraphics2DIOException;
Import org.w3c.dom.DOMImplementation;
 
 
Import org.w3c.dom.Document; public class Testsvggener {public void paint (graphics2d g2d) {g2d.setpaint (color.red);//Draw a rectangle G2d.fill (new Rectangle (1
0,10,100,100));
} public static void Main (string[] args) throws Unsupportedencodingexception, Svggraphics2dioexception {
Domimplementation domimpl=genericdomimplementation.getdomimplementation ();
String svguri= "Http://www.w3.org/2000/svg";
Create SVG file document Doc=domimpl.createdocument (Svguri, "SVG", null);
Svggraphics2d svggener=new svggraphics2d (DOC);
Testsvggener test=new Testsvggener ();
Test.paint (Svggener); Boolean uSecss=true;
Output SVG file Writer out =new outputstreamwriter (System.out, "UTF-8");
Svggener.stream (OUT,USECSS);
  }
}


Results:


Use example two: Convert SVG documents to PDF and PNG using batik

Package test;
Import Java.io.BufferedOutputStream;
Import Java.io.ByteArrayInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
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.batik.transcoder.image.PNGTranscoder;
 
Import Org.apache.fop.svg.PDFTranscoder; public class Svgutil {public static void Convertsvgfile2pdf (File svg,file pdf) throws ioexception{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{Transcoder tr=new pdftranscoder (); t ry {transcoderinput INPUt=new transcoderinput (in); try {transcoderoutput output=new transcoderoutput (out); Tr.transcode (input, output);} catch (Transcoderexception e) {e.
Printstacktrace (); }finally{out.close ();}} catch (Exception e) {e.printstacktrace ();}
finally{in.close ();}}
public static void Convertsvgfile2png (File svg,file pdf) throws ioexception{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{Transcoder tr=new pngtranscoder (); t ry {transcoderinput input =new transcoderinput (in), try {transcoderoutput output=new transcoderoutput (out); tr.transcod
E (input, output); } catch (Exception e) {e.printstacktrace ();} finally{out.close ();}} catch (Exception e) {e.printstacktrace ();}
finally{in.close ();}} public static void Convertstr2pdf (String svg,file pdf) throws ioexception{InputStream in=new BytearrayinputstreaM (Svg.getbytes ());
OutputStream out=new FileOutputStream (pdf);
Out=new Bufferedoutputstream (out);
Convert2pdf (in,out);
} public static void Main (string[] args) throws IOException {file F=new file ("Src/barchart.svg");
File Destfile=new file ("Src/sun.png");
Svgutil svgutil=new svgutil ();
Svgutil.convertsvgfile2png (f, destfile); }
}


Results:



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.