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 Alex Chew <ch...@gmail.com> on 2006/02/09 03:28:10 UTC

how can i manipulate an existing svg file?

Hi,
  I want to modify an existing SVG file use SVGGraphics2D, but i cannot get
anything in output file.
I have read several similar subjects and followed the tips, but I still
cannot get want i want.

please show me the right way, thanks.

code fragments:

   //create document from existing svg file
   String parser = XMLResourceDescriptor.getXMLParserClassName();
   SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
   String filename = "D:\\test\\old.svg";
   File file = new File(filename);
   String uri=file.toURI().toString();
   Document doc = f.createDocument(uri);

   //manipulate
   SVGGraphics2D generator = new SVGGraphics2D(doc);
   Shape circle = new Ellipse2D.Double(0,0,50,50);
   generator.setPaint(Color.red);
   generator.fill(circle);
   generator.draw(circle);
   generator.setPaint(Color.CYAN);
   generator.drawString("Dance on SVG",60,60);

   //append the generator to the existing svg file
   generator.getRoot(doc.getDocumentElement());

    //output as new file
   OutputStream os = new FileOutputStream("D:\\test\\new.svg");
   Writer out = new OutputStreamWriter(os, "UTF-8");
   generator.stream(out, true);
   os.close();
   out.close();

Re: how can i manipulate an existing svg file?

Posted by Alex Chew <ch...@gmail.com>.
I've made it work well. thanks.

     There are many stream methods.You can use stream method on
SVGGraphics2D that takes an Element parameter and pass your doc's document
element. Just like this,
generator.stream(doc.getDocumentElement(),out);
//not this one    generator.stream(out, true);


More details,
after you used the getRoot method, the 'content' of the SVGGraphics2D
has been moved over to your doc's document element and is no longer
contained in the SVGGraphics2D internal DOM structures.

ref:
http://mail-archives.apache.org/mod_mbox/xmlgraphics-batik-users/200303.mbox/%3C3E78A160.2070702@sun.com%3E

On 2/9/06, Alex Chew <ch...@gmail.com> wrote:
>
>  Hi,
>   I want to modify an existing SVG file use SVGGraphics2D, but i cannot
> get anything in output file.
> I have read several similar subjects and followed the tips, but I still
> cannot get want i want.
>
> please show me the right way, thanks.
>
> code fragments:
>
>    //create document from existing svg file
>    String parser = XMLResourceDescriptor.getXMLParserClassName();
>    SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
>    String filename = "D:\\test\\old.svg";
>    File file = new File(filename);
>    String uri=file.toURI().toString();
>    Document doc = f.createDocument(uri);
>
>    //manipulate
>    SVGGraphics2D generator = new SVGGraphics2D(doc);
>    Shape circle = new Ellipse2D.Double(0,0,50,50);
>    generator.setPaint(Color.red);
>    generator.fill(circle);
>    generator.draw (circle);
>    generator.setPaint(Color.CYAN);
>    generator.drawString("Dance on SVG",60,60);
>
>    //append the generator to the existing svg file
>    generator.getRoot(doc.getDocumentElement());
>
>     //output as new file
>    OutputStream os = new FileOutputStream("D:\\test\\new.svg");
>    Writer out = new OutputStreamWriter(os, "UTF-8");
>    generator.stream(out, true);
>    os.close();
>    out.close();
>
>
>



--
借鉴/思考/执行/检验/推广

how can i manipulate an existing svg file?

Posted by Alex Chew <ch...@gmail.com>.
>
>  Hi,
>   I want to modify an existing SVG file use SVGGraphics2D, but i cannot
> get anything in output file. I have read several similar subjects and
> followed the tips, but I still cannot get what i want.
>
> please show me the right way, thanks.
>
> code fragments:
>
>    //create document from existing svg file
>    String parser = XMLResourceDescriptor.getXMLParserClassName();
>    SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
>    String filename = "D:\\test\\old.svg";
>    File file = new File(filename);
>    String uri=file.toURI().toString();
>    Document doc = f.createDocument(uri);
>
>    //manipulate
>    SVGGraphics2D generator = new SVGGraphics2D(doc);
>    Shape circle = new Ellipse2D.Double(0,0,50,50);
>    generator.setPaint(Color.red);
>    generator.fill(circle);
>    generator.draw (circle);
>    generator.setPaint(Color.CYAN);
>    generator.drawString("Dance on SVG",60,60);
>
>    //append objects in existing svg to Generator
>    generator.getRoot(doc.getDocumentElement());
>
>     //output as new file
>    OutputStream os = new FileOutputStream("D:\\test\\new.svg");
>    Writer out = new OutputStreamWriter(os, "UTF-8");
>    generator.stream(out, true);
>    os.close();
>    out.close();
>