さて、これが私がやったことです。java.awt.Image
確かに行き止まりだった。PdfTemplate
aを anでラップして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();
}
}
}