Convert SVG to PDF by using iText in Java (ZT)

Source: Internet
Author: User

As promised here is a very simple PDF that contains a SVG-based image.

The SVG contains the following data:

<? Xml version = "1.0" standalone = "no"?>
<! DOCTYPE svg PUBLIC "-// W3C // dtd svg 1.1 // EN"
Http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd>
<Svg width = "300" height = "300" version = "1.1"
Xmlns = "http://www.w3.org/2000/svg">
<Rect x = "40" y = "20" rx = "20" ry = "20" width = "250" height = "250"
Style = "fill: red; stroke: black; stroke-width: 1;"/>
</Svg>

Here is the Java code:

Public static void main (String [] args ){

Document document = new Document ();
Try {
Using writer = Using writer. getInstance (document,
New FileOutputStream ("svgworkflow "));
Document. open ();
Document. add (new Paragraph ("SVG Example "));

Int width = 250;
Int height = 250;
Required contentbyte cb = writer. getDirectContent ();
Repeated template = cb. createTemplate (width, height );
Graphics2D g2 = template. createGraphics (width, height );

PrintTranscoder prm = new PrintTranscoder ();
TranscoderInput ti = new TranscoderInput ("file: // c: \ java \ svg. xml ");
Prm. transcode (ti, null );

PageFormat pg = new PageFormat ();
Paper pp = new Paper ();
Pp. setSize (width, height );
Pp. setImageableArea (0, 0, width, height );
Pg. setPaper (pp );
Prm. print (g2, pg, 0 );
G2.dispose ();

ImgTemplate img = new ImgTemplate (template );
Document. add (img );

} Catch (incluentexception e ){
System. err. println (e );
} Catch (IOException e ){
System. err. println (e );
}
Document. close ();

}

Keep in mind that you will need the Batik and Xerces libraries in addition to the iTExt jar file.

Http://xml.apache.org/batik/
Http://www.lowagie.com/iText/
Http://xml.apache.org/xerces2-j/

From: http://jroller.com/page/ghillert? Entry = svg_and_pdf_example

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.