通过编程为新的svgdocument渲染视图框



我有一个SVGDocument,我从数据库连接中编程将其作为byte[]检索。<svg>元素包含一个适当的viewBox属性,该属性涵盖SVGDocument的一部分,现有过程需要作为PDF呈现。

使用以下(简单(代码,我能够正确设置viewBox属性:

Element rootElement = svgDocument.getRootElement();
String viewBox = rootElement.getAttribute("viewBox");
log.debug("viewBox={}", viewBox);
// viewBox=-612 0 1224 792

我的目标是使用Batik getEnclosureList()方法检索NodeList并构建一个新的(裁剪(SVGDocument,我可以将其发送到传统过程,该过程将呈现PDF。

我尝试使用的代码在下面列出:

SVGRect rectangle = svgDocument.getRootElement().createSVGRect();
rectangle.setX(minX);  // -612
rectangle.setY(minY); // 0
rectangle.setWidth(startingX); // 1224
rectangle.setHeight(startingY); // 792
NodeList croppedNodes = svgDocument.getRootElement().getEnclosureList(rectangle, null);

我的问题是,当我使用此方法时,SVGSVGContext是无效的。

我试图找到如何设置SVGSVGContext的尝试尚未成功,这就是为什么我决定在此处发布问题。

我没有在使用Apache batik来解决此解决方案时出售,但是getEnclosureList()方法似乎可以返回我完成任务所需的内容。

从挖掘大量源代码,i think 我找到了关于我需要做的事情的答案,在initSvgDom()方法中详细介绍了:

private void someMethod(SVGDocument svgDocument) {
   initSvgDom(svg);
   Element rootElement = svg.getRootElement();
   String viewBox = rootElement.getAttribute("viewBox");
   log.debug("viewBox={}", viewBox);
   String[] viewBoxArray = viewBox.split(" ");
   float minX = Float.valueOf(viewBoxArray[0]);
   float minY = Float.valueOf(viewBoxArray[1]);
   float startingX = Float.valueOf(viewBoxArray[2]);
   float startingY = Float.valueOf(viewBoxArray[3]);
   SVGRect rectangle = svgDocument.getRootElement().createSVGRect();
   rectangle.setX(minX);
   rectangle.setY(minY);
   rectangle.setWidth(startingX);
   rectangle.setHeight(startingY);
   NodeList nodes = svgDocument.getRootElement().getEnclosureList(rectangle, null);
   // nodes contains a list of elements within the specified rectangle, which matches the value of the viewBox within the svgDocument.
  ... do stuff with nodes
}
private void initSvgDom(Document document) {
   UserAgent userAgent = new UserAgentAdapter();
   DocumentLoader loader = new DocumentLoader(userAgent);
   BridgeContext bridgeContext = new BridgeContext(userAgent, loader);
   bridgeContext.setDynamicState(BridgeContext.DYNAMIC);
   (new GVTBuilder()).build(bridgeContext, document);
}

最新更新