You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by AdrianSevan <ad...@yahoo.com> on 2006/11/29 07:44:20 UTC

SVG file from multiple SVG files

Hi,
I am a newbie in Batik and I’m struggling to create a SVG file from multiple
SVG files and save it as a new file. I succeeded to open a SVG document and
draw on it a rectangle and save the new file, but I cannot merge 2 SVG files
and save the result.
I’m stuck in the merging phase. If someone could help…

Here is the code I’m using:


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.dom.svg.SVGGraphicsElement;
import org.apache.batik.dom.util.DOMUtilities;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.ImageNode;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.xerces.util.DOMUtil;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.svg.SVGSVGElement;
import org.w3c.dom.Node;

public class Principala
{

	public static void main(String[] args)
	{
		JFrame f = new JFrame("Batik");
		JPanel p = new JPanel(new BorderLayout());
		JSVGCanvas svgCanvas = new JSVGCanvas();

		p.add(svgCanvas);
		f.add(p);
		f.setSize(400,400);
		f.setLocationRelativeTo(null);
		f.setVisible(true);
		
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		SVGDocument doc = null;
		SVGDocument doc2 = null;
		Element x = null;
		
		try {
		    String parser = XMLResourceDescriptor.getXMLParserClassName();
		    SAXSVGDocumentFactory sax = new SAXSVGDocumentFactory(parser);
		    String uri = "file:///D:/work/xBatik1.6/samples/sizeOfSun.svg";
		    
		    SAXSVGDocumentFactory sax2 = new SAXSVGDocumentFactory(parser);
		    String uri2 = "file:///D:/work/xBatik1.6/samples/anne.svg";
		    
		    doc = sax.createSVGDocument(uri);
		    doc2 = sax.createSVGDocument(uri2);
		    
		    x = doc2.getDocumentElement();
		    x.setNodeValue("svg");		    
		    
		} catch (IOException ex) {
		     System.err.println("Error");
		}
		svgCanvas.setDocument(doc);
		//doc.importNode(x,true);
		doc.adoptNode(x.cloneNode(true));
		p.add(svgCanvas);
		svgCanvas.setVisible(true);
		f.validate();
		try
		{
			FileOutputStream outStream = new FileOutputStream("D:/test.svg");
			Writer outf = new OutputStreamWriter(outStream,"UTF-8");
			DOMUtilities.writeDocument(doc, outf);
			outf.flush();
			outf.close();
		}
		catch(IOException e)
		{
			System.err.println("Error");
		}
	}
}	
-- 
View this message in context: http://www.nabble.com/SVG-file-from-multiple-SVG-files-tf2723175.html#a7594201
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: SVG file from multiple SVG files

Posted by AdrianSevan <ad...@yahoo.com>.
thank you very much. Now it works. I'm very grateful.
-- 
View this message in context: http://www.nabble.com/SVG-file-from-multiple-SVG-files-tf2723175.html#a7598305
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: SVG file from multiple SVG files

Posted by th...@kodak.com.
Hi Adrian,

AdrianSevan <ad...@yahoo.com> wrote on 11/29/2006 01:44:20 AM:

> I succeeded to open a SVG document and
> draw on it a rectangle and save the new file, but I cannot merge 2 SVG 
files
> and save the result.

>           doc = sax.createSVGDocument(uri);
>           doc2 = sax.createSVGDocument(uri2);
> 
>           x = doc2.getDocumentElement();

            x = doc.importNode(x, true);
                doc.getDocumentElement().appendChild(x);

>           /* remove this, not sure what it was for  */ 
>           // x.setNodeValue("svg"); 

   The code setting up the canvas should be reordered, from:

>       svgCanvas.setDocument(doc);
>       p.add(svgCanvas);
>       svgCanvas.setVisible(true);
>       f.validate();

   To:
          p.add(svgCanvas);
        f.validate();
          svgCanvas.setVisible(true); // probably not needed.
          svgCanvas.setDocument(doc);


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org