Reputation: 644
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
Upvotes: 3
Views: 503
Reputation: 644
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
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-svggen</artifactId>
<version>1.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/batik/batik-transcoder -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/batik/batik-rasterizer -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-rasterizer</artifactId>
<version>1.11</version>
</dependency>
Java code to reflect same result as above :
private static Image createSVGImage(PdfWriter pdfWriter, String imageEntry) throws BadElementException, IOException {
Image image = null;
final BufferedImage[] imagePointer = new BufferedImage[1];
PdfContentByte pdfCB = new PdfContentByte(pdfWriter);
try {
TranscoderInput input = new TranscoderInput(new FileInputStream(imageEntry));
ImageTranscoder t = new ImageTranscoder() {
@Override
public BufferedImage createImage(int w, int h) {
return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
}
@Override
public void writeImage(BufferedImage img, TranscoderOutput output) throws TranscoderException {
// TODO Auto-generated method stub
imagePointer[0] = img;
}
};
t.addTranscodingHint(ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE,
Boolean.FALSE);
t.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white);
t.transcode(input, null);
}
catch (TranscoderException ex) {
// Requires Java 6
ex.printStackTrace();
throw new IOException("Couldn't convert ");
}
image = Image.getInstance(pdfCB, imagePointer[0], 1);
return image;
}
Upvotes: 2