Java只生成带有Batik的SVG而没有任何DOCTYPE

时间:2017-05-17 21:06:03

标签: java svg batik

我正在尝试生成svg,我将在以后用于从HTML文件生成PDF。我使用OpenHtmlToPdf,可以将内联svg与其他HTML代码一起放到pdf中。

问题是Batik示例会生成svg以及<DOCTYPE><?xml>标记。

我只想要<svg>标签及其内容。

这是我用于svg生成的代码:

public class TestSVGGen {

public void paint(Graphics2D g2d) {
    g2d.fill(new Rectangle(10, 10, 400, 5));
    g2d.fill(new Rectangle(10, 15, 10, 125));
    g2d.fill(new Rectangle(110, 15, 10, 125));
    g2d.fill(new Rectangle(210, 15, 10, 125));
    g2d.fill(new Rectangle(310, 15, 10, 125));
    g2d.fill(new Rectangle(400, 15, 10, 125));
    g2d.drawString("test",20,150);
}

public static void main(String[] args) throws IOException {

    // Get a DOMImplementation.
    DOMImplementation domImpl =
            GenericDOMImplementation.getDOMImplementation();

    // Create an instance of org.w3c.dom.Document.
    String svgNS = "http://www.w3.org/2000/svg";
    Document document = domImpl.createDocument(svgNS, "svg", null);

    // Create an instance of the SVG Generator.
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // Ask the test to render into the SVG Graphics2D implementation.
    TestSVGGen test = new TestSVGGen();
    test.paint(svgGenerator);

    // Finally, stream out SVG to the standard output using
    // UTF-8 encoding.
    boolean useCSS = true; // we want to use CSS style attributes
    Writer out = new FileWriter("test-svg.xhtml");
    svgGenerator.stream(out, useCSS);
}

}

这就是我得到的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
          'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; text-rendering:auto; stroke:black; stroke-linecap:square; stroke-miterlimit:10; shape-rendering:auto; stroke-opacity:1; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12px; stroke-dashoffset:0; image-rendering:auto;" xmlns="http://www.w3.org/2000/svg"
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs"
  /><g
  ><g
    ><rect x="10" width="400" height="5" y="10" style="stroke:none;"
      /><rect x="10" width="10" height="125" y="15" style="stroke:none;"
      /><rect x="110" width="10" height="125" y="15" style="stroke:none;"
      /><rect x="210" width="10" height="125" y="15" style="stroke:none;"
      /><rect x="310" width="10" height="125" y="15" style="stroke:none;"
      /><rect x="400" width="10" height="125" y="15" style="stroke:none;"
      /><text x="20" xml:space="preserve" y="150" style="stroke:none;"
      >test</text
    ></g
  ></g
></svg
>

这是我需要得到的:

<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; text-rendering:auto; stroke:black; stroke-linecap:square; stroke-miterlimit:10; shape-rendering:auto; stroke-opacity:1; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12px; stroke-dashoffset:0; image-rendering:auto;" xmlns="http://www.w3.org/2000/svg"
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs"
  /><g
  ><g
    ><rect x="10" width="400" height="5" y="10" style="stroke:none;"
      /><rect x="10" width="10" height="125" y="15" style="stroke:none;"
      /><rect x="110" width="10" height="125" y="15" style="stroke:none;"
      /><rect x="210" width="10" height="125" y="15" style="stroke:none;"
      /><rect x="310" width="10" height="125" y="15" style="stroke:none;"
      /><rect x="400" width="10" height="125" y="15" style="stroke:none;"
      /><text x="20" xml:space="preserve" y="150" style="stroke:none;"
      >test</text
    ></g
  ></g
></svg
>

我试图将SVGGraphics2D传递给一个空文档实例,但这不起作用:

public class TestSVGGen {

public void paint(Graphics2D g2d) {
    g2d.fill(new Rectangle(10, 10, 400, 5));
    g2d.fill(new Rectangle(10, 15, 10, 125));
    g2d.fill(new Rectangle(110, 15, 10, 125));
    g2d.fill(new Rectangle(210, 15, 10, 125));
    g2d.fill(new Rectangle(310, 15, 10, 125));
    g2d.fill(new Rectangle(400, 15, 10, 125));
    g2d.drawString("test",20,150);
}

public static void main(String[] args) throws IOException {
    Document document = newEmptyDocument();

    // Create an instance of the SVG Generator.
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // Ask the test to render into the SVG Graphics2D implementation.
    TestSVGGen test = new TestSVGGen();
    test.paint(svgGenerator);

    // Finally, stream out SVG to the standard output using
    // UTF-8 encoding.
    boolean useCSS = true; // we want to use CSS style attributes
    Writer out = new FileWriter("test-svg.xhtml");
    svgGenerator.stream(out, true);
}

public static Document newEmptyDocument() {
    DocumentBuilderFactory factory = null;
    DocumentBuilder builder = null;
    Document ret;

    try {
        factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    ret = builder.newDocument();

    return ret;
}

0 个答案:

没有答案