I have worked for SVG image rendering for iText PDF document. to do this i used SVGSalamander for convert SVG to image format. it works fine but it has a strange behavior that some of the SVG images are not rendering correctly while some are doing. those wrongly rendered svg are not aligned with the real image. I tried but i couldn't figure out why its happening only for some images.
Really appreciate if someone help me to resolve this.
Java Code:
private static Image createSVGImage(PdfWriter pdfWriter, String imageEntry) throws BadElementException {
Image image = null;
Graphics2D g2dgraphics =null;
PdfTemplate template = null;
try{
SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram( new java.io.File( imageEntry ).toURI() );
template = pdfWriter.getDirectContent().createTemplate( diagram.getWidth(), diagram.getHeight());
diagram.setIgnoringClipHeuristic(true);
g2dgraphics= new PdfGraphics2D(template, diagram.getWidth(), diagram.getHeight());
diagram.render(g2dgraphics);
}catch( Exception e ){
e.printStackTrace();
} finally {
if( g2dgraphics != null ){
g2dgraphics.dispose();
image = Image.getInstance(template);
}
g2dgraphics.dispose();
}
return image;
}
SVG xml code that its not align
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<path d="M19,16a46,46 0,1,0 62,0l-8,8a34,34 0,1,1-46,0z" fill="#069"/>
<path d="M46,43v35a28,28 0,0,1-14-49zM54,43v35a28,28 0,0,0 14-49z" fill="#396"/>
<circle r="15" cx="50" cy="18" fill="#900"/>
</svg>
real image
output image from this code above
I really dont know why this is happening with this library, since there is no answer i have changed the SVGSalamendar to Batik Library and if someone needs, this is the working code for it
Maven Dependencies
Java code to reflect same result as above :