我有一些现有代码,看起来很像Swing & Batik: Create an ImageIcon from an SVG file?
上的解决方案但我的图像的目的地是PDF,这让我觉得当你放大PDF时,你会看到像素。如果源数据和目标数据都是矢量图形,则应该可以直接渲染。
我们正在使用的库(iText)需要一个java.awt.Image,但我似乎无法弄清楚如何获得一个呈现SVG的java.awt.Image。 Batik有办法做到这一点吗?
答案 0 :(得分:1)
嗯,这就是我最终做的事情。 java.awt.Image
当然是一个死胡同。有一个解决方案是在PdfTemplate
中整理ImgTemplate
,以便将其用作iText Image
。
(我必须把它放在知道它的大小的东西上,因为它被用在一张桌子里,否则布局会变得非常疯狂。Image
似乎知道这一点。)
public class SvgHelper {
private final SAXSVGDocumentFactory factory;
private final GVTBuilder builder;
private final BridgeContext bridgeContext;
public SvgHelper() {
factory = new SAXSVGDocumentFactory(
XMLResourceDescriptor.getXMLParserClassName());
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
bridgeContext = new BridgeContext(userAgent, loader);
bridgeContext.setDynamicState(BridgeContext.STATIC);
builder = new GVTBuilder();
}
public Image createSvgImage(PdfContentByte contentByte, URL resource,
float maxPointWidth, float maxPointHeight) {
Image image = drawUnscaledSvg(contentByte, resource);
image.scaleToFit(maxPointWidth, maxPointHeight);
return image;
}
public Image drawUnscaledSvg(PdfContentByte contentByte, URL resource) {
GraphicsNode imageGraphics;
try {
SVGDocument imageDocument = factory.createSVGDocument(resource.toString());
imageGraphics = builder.build(bridgeContext, imageDocument);
} catch (IOException e) {
throw new RuntimeException("Couldn't load SVG resource", e);
}
float width = (float) imageGraphics.getBounds().getWidth();
float height = (float) imageGraphics.getBounds().getHeight();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics = template.createGraphics(width, height);
try {
// SVGs can have their corner at coordinates other than (0,0).
Rectangle2D bounds = imageGraphics.getBounds();
//TODO: Is this in the right coordinate space even?
graphics.translate(-bounds.getX(), -bounds.getY());
imageGraphics.paint(graphics);
return new ImgTemplate(template);
} catch (BadElementException e) {
throw new RuntimeException("Couldn't generate PDF from SVG", e);
} finally {
graphics.dispose();
}
}
}