Hi Rana,
Your code mostly looks ok, a few things looked a little
odd to me however.
First you declare 'SVGGraphics2D svgGenerator' twice!
so I might guess that you stream the first one and not the second.
Also you flush 'fos', but not 'out', you should flush 'out', then
flush 'fos' (then close fos). You might also want to look at what
it actually outputs - for example it might embed an image which would
be written to D: (but it might not be able to find it). For
Swing components you might want to use 'SwingSVGPrettyPrint' because
it will try to ensure that you don't just get Swing's double buffer.
Rana Khartabil wrote:
I am trying to save a jgraph object as an SVG file and I’m having
problems with it. I spent so much time on it and so if someone
encountered a similar problem or knows a solution, I would greatly
appreciate it.
Basically, this method attempts to save the jgraph as SVG. The file is
produced, but 1) it’s blank white and 2) there are no scrolling
capabilities.
public void saveGraphAsSVG(JGraph graph) {
try {
/* Create file output stream. */
FileOutputStream fos = new FileOutputStream(new
File("D:\\final.svg"));
/* Created writer with UTF-8 encoding. */
Writer out = new OutputStreamWriter(fos, "UTF-8");
/* Get a DOMImplementation. */
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
/* Create an instance of Document. */
Document document = domImpl.createDocument(null, "svg",
null);
/* Create an instance of the SVG Generator. */
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
SVGGeneratorContext ctx =
SVGGeneratorContext.createDefault(
document);
ImageHandler ihandler = new ImageHandlerPNGEncoder(
"D:\\", null);
ctx.setImageHandler(ihandler);
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
svgGenerator.setSVGCanvasSize(new Dimension(2000, 2000));
graph.paint(svgGenerator);
/* Use CSS style attribute. */
boolean useCSS = true;
/* Finally, stream out SVG to the writer. */
svgGenerator.stream(out, useCSS);
/* Close the file output stream. */
fos.flush();
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks,
Rana
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]