java - 使用 Apache Batik 对 SVG 文件进行正确缩进(漂亮的打印)
问题描述
我需要了解 apache batik 如何缩进库生成的 SVG 文件。我有以下代码:
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
for(Drawable savedDrawable : cVwDrawLocal.getSavedDrawables()){
if(savedDrawable instanceof GeoDrawable){
savedDrawable.copyToGraphics(svgGenerator);
}
}
boolean useCSS = true; // we want to use CSS style attributes
String svgFile = svgFileName + ".svg";
try {
OutputStream outputStream = new FileOutputStream(svgFile);
Writer outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
svgGenerator.stream(outputStreamWriter, useCSS);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
生成的 SVG 文件为:
<g
><g style="fill:white; stroke-miterlimit:1; stroke-dasharray:1; stroke-dashoffset:1; stroke:white;"
><line y2="432" style="fill:none;" x1="873" x2="873" y1="432"
/></g
><g style="fill:rgb(0,252,253); stroke-miterlimit:1; stroke-dasharray:0.1,5; stroke-width:2; stroke-dashoffset:1; stroke:rgb(0,252,253);"
><line y2="479" style="fill:none;" x1="901" x2="910" y1="490"
/></g
>
但我需要这样的东西:
<g>
<g style="fill:white; stroke-miterlimit:1; stroke-dasharray:1; stroke-dashoffset:1; stroke:white;">
<line y2="432" style="fill:none;" x1="873" x2="873" y1="432"/>
</g>
<g style="fill:rgb(0,252,253); stroke-miterlimit:1; stroke-dasharray:0.1,5; stroke-width:2; stroke-dashoffset:1; stroke:rgb(0,252,253);">
<line y2="479" style="fill:none;" x1="901" x2="910" y1="490"/>
</g>
</g>
我怎样才能得到这个?先感谢您!
解决方案
好的。我找到了一个适合我的解决方案:
public static boolean prettyPrintXml(File newFile) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setValidating(false);
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(newFile);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StreamResult streamResult = new StreamResult(newFile);
DOMSource domSource = new DOMSource(document);
transformer.transform(domSource, streamResult);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
谢谢!
推荐阅读
- java - Maven 不使用更新的本地构建/安装的依赖项
- python - 尝试将 URL 参数传递给 CBV 以过滤并返回查询集
- javascript - Quill insertText 产生 TypeError: n.appendChild is not a function
- bulma - 如何在 bulma 徽章上打上复选标记?
- excel - 一年的中值和另一年的中值,其中日期在同一列中
- python - 打开 Excel 并使用 Python 加载 .vsto 加载项
- javascript - 这是我的递归函数中的问题吗?
- python - 使用 matplotlib 绘制分段函数会导致 ValueError:具有多个元素的数组的真值不明确
- python - 使用 Python 的 win32com 和 Foxpro(VFP OLEDB 提供程序)
- git - 你什么时候会使用 .git/info/exclude 而不是 ~/.gitignore (core.excludesFile) 来排除文件?