posts - 198,  comments - 1481,  trackbacks - 6

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"
<svg width="300" height="300" version="1.1"
<rect x="40" y="20" rx="20" ry="20" width="250" height="250"
="fill:red;stroke:black;stroke-width:1;" />
Here is the Java code:

public static void main(String[] args) {

        Document document 
= new Document();
try {
            PdfWriter writer 
= PdfWriter.getInstance(document,
new FileOutputStream("svg.pdf"));
new Paragraph("SVG Example"));

int width = 250;
int height = 250;
            PdfContentByte cb 
= writer.getDirectContent();
            PdfTemplate template 
= cb.createTemplate(width,height);         
            Graphics2D g2 
= template.createGraphics(width,height);          
            PrintTranscoder prm 
= new PrintTranscoder();
            TranscoderInput ti 
= new TranscoderInput("file:///c:\\java\\svg.xml");
            PageFormat pg 
= new PageFormat();
            Paper pp
= new Paper();
            pp.setSize(width, height);
00, width, height);
            prm.print(g2, pg, 

            ImgTemplate img 
= new ImgTemplate(template);           
catch (DocumentException e) {
catch (IOException e) {


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


posted on 2006-08-09 13:16 RubyPDF 阅读(...) 评论(...) 编辑 收藏