xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zarr <zarrandr...@web.de>
Subject Batik - How to Export Svg Many Times from SVGGraphics2D
Date Wed, 15 Apr 2015 07:46:20 GMT
Hello,

I want

 1. To manipulate a svg-document and then export it to svg file
 2. Then manipulate existing svg-document again and export it second
    time to a svg file and so on...

I noticed that after a svg-document was exported to an svg file with 
|svgGenerator.stream(streamWriter, useCss)| method the svg document 
become empty.

Can some body give me a hint, what is wrong in my code?

I posted my question on stack-overflow, but didn't get any answer, so 
you are my last hope :-)

|public  void  svgExportExample()  {

   finalSVGGraphics2D  svgGenerator=  createSvgGenerator();

   drawRectangle(svgGenerator);

   drawText(svgGenerator);

   finalString  xml1=  toXml(svgGenerator);

   finalString  xml2=  toXml(svgGenerator);

   assertEquals("xml document was not changed and should be the same",
     xml1,  xml2);

}

private  SVGGraphics2D  createSvgGenerator()  {
   finalDocument  document=  initNewDocument();

   finalSVGGraphics2D  svgGenerator=  new  SVGGraphics2D(document);
   return  svgGenerator;
}

private  Document  initNewDocument()  {
   // Get a DOMImplementation.
   finalDOMImplementation  domImpl=  GenericDOMImplementation
     .getDOMImplementation();

   // Create an instance of org.w3c.dom.Document.
   finalString  svgNS=  "http://www.w3.org/2000/svg";
   return  domImpl.createDocument(svgNS,  "svg",  null);
}

private  String  toXml(finalSVGGraphics2D  svgGenerator)  {

   try  (ByteArrayOutputStream  os=  new  ByteArrayOutputStream();  OutputStreamWriter  streamWriter=
 new  OutputStreamWriter(os))  {

     final boolean useCss=  true;
     // this method clears the content of the xnl-document
     svgGenerator.stream(streamWriter,  useCss);

     return  os.toString(StandardCharsets.UTF_8.name());

   }  catch  (finalIOException  e)  {
     throw  new  IllegalStateException(
       "a proble during printing to svg file was occured",  e);
   }

}

private  void  drawText(finalGraphics2D  g)  {
   finalString  text=  "My Text inside Svg";
   finalint  fontSize=  10;
   finalint  width=  10;
   finalint  height=  10;
   finalGraphics  g1=  new  BufferedImage(width,  height,
     BufferedImage.TYPE_INT_RGB).getGraphics();
   finalint  style=  0;
   finalFont  font=  new  Font(g1.getFont().toString(),  style,  fontSize);
   g1.dispose();

   g.setFont(font);

   g.setColor(Color.GREEN);

   g.drawString(text,  50,  100);
}

private  void  drawRectangle(finalGraphics2D  g)  {
   g.setColor(Color.RED);
   finalint  width=  500;
   finalint  height=  300;
   g.fillRect(0,  0,  width,  height);
}|


Mime
View raw message