Hi Meccain,

赵刚 <mecc...@gmail.com> wrote on 11/11/2010 04:26:38 AM:

> I am try to convert the svg with css into image, however, I haven't 
> got a way to convert the expected, the css style is lost in the 
> converted image .The test code is as following, the rect should be 
> with fill color, however, the converted image doesn't have the fill 
color

>     String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
[...]
>     Element defs = doc.createElement("defs");

        You need to use 'createElementNS(svgNS, <element name>)'
instead of just createElement(<element name>).  If you just use 
createElement you will not get "SVG" elements, so they won't be 
rendered (the problem was more than just CSS). 

        This applies to every place you create an element.
Thomas DeWeese | CDG Advanced Development | 
Eastman Kodak Company | 343 State Street | Rochester, NY 14650-0128 | 
thomas.dewe...@kodak.com | 585 724-0294 | 
www.kodak.com 


>     svgRoot.appendChild(defs);
>     Element style = doc.createElement("style");
>     style.setAttribute("type", "text/css");
>     style.setAttribute("MEDIA","screen");
>     style.appendChild(doc.createTextNode("@media screen{\n.graphic
> {fill:#ffff99;}\n}"));
>     defs.appendChild(style);
>     Element rect = doc.createElement("rect");
>     rect.setAttribute("class", "graphic");
>     rect.setAttribute("x", "0");
>     rect.setAttribute("y", "0");
>     rect.setAttribute("width", "100");
>     rect.setAttribute("height", "50");
>     svgRoot.appendChild(rect);
>     ImageTranscoder coder = new PNGTranscoder();
> 
>     coder.addTranscodingHint(ImageTranscoder.KEY_MEDIA, "screen");
>     SVGTranscoder coder2 = new SVGTranscoder();  
>     TranscoderInput input = new TranscoderInput(doc);
>     OutputStream ostream = null;
>     try
>     {
>       ostream = new FileOutputStream("out.png" );
>       TranscoderOutput output = new TranscoderOutput(ostream);
>       coder.transcode(input, output);
>       try
>       {
>         Writer writer = new FileWriter( "out.svg" );
>         TranscoderOutput output2 = new TranscoderOutput(writer);
>         coder2.transcode(input, output2);
>       }
>       catch (IOException e)
>       {
>         // TODO Auto-generated catch block
>         e.printStackTrace();
>       }
> 
>     }
>     catch (FileNotFoundException e)
>     {
>       // TODO Auto-generated catch block
>       e.printStackTrace();
>     }
>     catch (TranscoderException e)
>     {
>       // TODO Auto-generated catch block
>       e.printStackTrace();
>     }
>     finally
>     {
>       try
>       {
>         ostream.flush();
>         ostream.close();
>       }
>       catch (IOException e)
>       {
>         // TODO Auto-generated catch block
>         e.printStackTrace();
>       }
>     }

Reply via email to