Batik - 如何从SVGGraphics2D多次出口Svg

时间:2015-04-14 08:01:30

标签: java svg batik

我想要

  1. 操作svg文档,然后将其导出到svg文件
  2. 然后再次操作现有的svg文档并将其第二次导出到svg文件,依此类推......
  3. 我注意到在使用svgGenerator.stream(streamWriter, useCss)方法将svg文件导出到svg文件后,svg文档变为空。

    有些机构可以给我一个提示,我的代码有什么问题吗?

    public void svgExportExample() {
    
      final SVGGraphics2D svgGenerator = createSvgGenerator();
    
      drawRectangle(svgGenerator);
    
      drawText(svgGenerator);
    
      final String xml1 = toXml(svgGenerator);
    
      final String xml2 = toXml(svgGenerator);
    
      assertEquals("xml document was not changed and should be the same",
        xml1, xml2);
    
    }
    
    private SVGGraphics2D createSvgGenerator() {
      final Document document = initNewDocument();
    
      final SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
      return svgGenerator;
    }
    
    private Document initNewDocument() {
      // Get a DOMImplementation.
      final DOMImplementation domImpl = GenericDOMImplementation
        .getDOMImplementation();
    
      // Create an instance of org.w3c.dom.Document.
      final String svgNS = "http://www.w3.org/2000/svg";
      return domImpl.createDocument(svgNS, "svg", null);
    }
    
    private String toXml(final SVGGraphics2D svgGenerator) {
    
      try (ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStreamWriter streamWriter = new OutputStreamWriter(os)) {
    
        final boolean useCss = true;
        // this method clears the content of the xnl-document
        svgGenerator.stream(streamWriter, useCss);
    
        return os.toString(StandardCharsets.UTF_8.name());
    
      } catch (final IOException e) {
        throw new IllegalStateException(
          "a proble during printing to svg file was occured", e);
      }
    
    }
    
    private void drawText(final Graphics2D g) {
      final String text = "My Text inside Svg";
      final int fontSize = 10;
      final int width = 10;
      final int height = 10;
      final Graphics g1 = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_RGB).getGraphics();
      final int style = 0;
      final Font font = new Font(g1.getFont().toString(), style, fontSize);
      g1.dispose();
    
      g.setFont(font);
    
      g.setColor(Color.GREEN);
    
      g.drawString(text, 50, 100);
    }
    
    private void drawRectangle(final Graphics2D g) {
      g.setColor(Color.RED);
      final int width = 500;
      final int height = 300;
      g.fillRect(0, 0, width, height);
    }
    

1 个答案:

答案 0 :(得分:0)

您的代码没有错。 stream会淡化您的svgGenerator

如果您想将其导出到文件中然后再次使用它,我建议

  1. 定义文档的工作副本
  2. 努力工作
  3. 将您的工作副本复制到temp svg文档
  4. 将temp svg文档导出到文件
  5. 重复步骤2.-4。

这是您进行修改的方法(非常简单):

private String toXml(final SVGGraphics2D svgGenerator) {

  try (ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStreamWriter streamWriter = new OutputStreamWriter(os)) {

    final boolean useCss = true;

    // create new temp SVGGraphics2D
    SVGGraphics2D svgToExport = (SVGGraphics2D) svgGenerator.create();
    // use it to export (and empty) it
    svgToExport.stream(streamWriter, useCss);

    return os.toString(StandardCharsets.UTF_8.name());

  } catch (final IOException e) {
    throw new IllegalStateException(
      "a problem during printing to svg file occured", e);
  }
}