iText PDFドキュメントのSVG画像レンダリングに取り組んできました。これを行うには、SVG を画像形式に変換するために SVGSalamander を使用しました。正常に動作しますが、一部の SVG 画像が正しくレンダリングされず、一部が正しくレンダリングされないという奇妙な動作があります。間違ってレンダリングされた svg は、実際の画像と一致しません。試してみましたが、一部の画像でのみ発生する理由がわかりませんでした。
誰かがこれを解決するのを手伝ってくれたら本当に感謝しています。
Java コード:
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コード
<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>
実像
上記のコードからの出力画像