private static URI createSvg ( final Dimension targetSize , final RasterReference rasterReference , final Double rotation , final Color backgroundColor , final File workingDir ) throws IOException { // load SVG graphic final SVGElement svgRoot = parseSvg ( rasterReference . inputStream ) ; // create a new SVG graphic in which the existing graphic is embedded (scaled and rotated) DOMImplementation impl = SVGDOMImplementation . getDOMImplementation ( ) ; Document newDocument = impl . createDocument ( SVG_NS , "svg" , null ) ; SVGElement newSvgRoot = ( SVGElement ) newDocument . getDocumentElement ( ) ; newSvgRoot . setAttributeNS ( null , "width" , Integer . toString ( targetSize . width ) ) ; newSvgRoot . setAttributeNS ( null , "height" , Integer . toString ( targetSize . height ) ) ; setSvgBackground ( backgroundColor , targetSize , newDocument , newSvgRoot ) ; embedSvgGraphic ( svgRoot , newSvgRoot , newDocument , targetSize , rotation ) ; File path = writeSvgToFile ( newDocument , workingDir ) ; return path . toURI ( ) ; }
With the Batik SVG library it is only possible to create new SVG graphics but you can not modify an existing graphic . So we are loading the SVG file as plain XML and doing the modifications by hand .