2
votes

I use batik for drawing svg, and save it. That work.

But now i try to load my svg file and add somthing.

I use this fonction for load my svg file

private static SVGDocument loadSVGDocument(String uri) {
    String parser = XMLResourceDescriptor.getXMLParserClassName();
    SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
    SVGDocument svgDocument = null;
    try {
        //svgDocument = factory.createSVGDocument(IMAGE_SVG);
        svgDocument = factory.createSVGDocument(uri);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    return svgDocument;
}

and this for have SVGGraphics2D

SVGDocument svgDocument = loadSVGDocument(TEST_SVG);
SVGGraphics2D g = new SVGGraphics2D(svgDocument);

When i debug my SVGDocument have all children and attribut null And when i generate image, is empty and size is 400x400

Where is problem when i load my SVG file?

1
Only a small hint: When you create an SVGGraphics2D with a document, then the document is the target for the painting operations. E.g. when you do something like g.fillRect(10,10,20,30), then the document will contain a rectangle. But if the document already contains something, it will (probably) be overwritten. I don't know from the tip of my head how to use the SVGGraphics to add something to an existing document. It should be possible, but I would have to try it out. - Marco13
Thx for your reply. For this time i juste try to load on SVGGraphics2D and save it at jpg. But my jpg is empty. If i save in svg, is empty to. - zulot
You should try to clarify: 1. Do you want to load an SVG and save it as JPG? or 2. Do you want to load an SVG, add some custom elements (by painting into the SVGGraphics) and save the result as SVG? - Marco13
point 2. During my last test i see XMLResourceDescriptor.getXMLParserClassName(); return null; It's normal ? - zulot
Sorry, I'm not deeply familiar with batik (only used it once for github.com/javagl/SvgGraphics ). The main purpose of my comment was to clarify the question. - Marco13

1 Answers

0
votes

I every body I find a solution for my case

I compare my generation and my file. First think i have svg root and one child g only. svg root don t have with and height

I go search width and height

        Element elSVG = svgDocument.getRootElement();
    String width = elSVG.getAttribute("width");
    String height = elSVG.getAttribute("height");

and after root

NodeList nodes = elSVG.getElementsByTagName("g");
    Node node = nodes.item(0);
    Element el = null;
    if(Node.ELEMENT_NODE == node.getNodeType()) {
        el = (Element)node;
    }

and for the end add everything to graphics

        SVGGraphics2D g = new SVGGraphics2D(svgDocument);
    g.setTopLevelGroup(el);
    g.setSVGCanvasSize(new Dimension(Integer.parseInt(width), Integer.parseInt(height)));

And that works for only one child to svg. For me is OK for now.

And i try to add an other think like rectangle and that work to.