Hi, ALL,

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

Could anybody help me to figure out how to write such code? Thanks in
advance.

    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    Document doc = impl.createDocument(svgNS, "svg", null);
    // Get the root element (the 'svg' element).
    Element svgRoot = doc.getDocumentElement();

    svgRoot.setAttributeNS(null, "width", "100");
    svgRoot.setAttributeNS(null, "height", "50");

    Element defs = doc.createElement("defs");
    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