Search in sources :

Example 21 with SVGGraphics2D

use of org.apache.batik.svggen.SVGGraphics2D in project mzmine2 by mzmine.

the class SwingExportUtil method writeToSVG.

public static void writeToSVG(JComponent panel, File fileName) throws IOException {
    // print the panel to pdf
    int width = panel.getWidth();
    int height = panel.getWidth();
    logger.info(() -> MessageFormat.format("Exporting panel to SVG file (width x height; {0} x {1}): {2}", width, height, fileName.getAbsolutePath()));
    // Get a DOMImplementation
    DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
    org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    svgGenerator.setSVGCanvasSize(new Dimension(width, height));
    panel.print(svgGenerator);
    // we want to use CSS style attribute
    boolean useCSS = true;
    try (Writer out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8")) {
        svgGenerator.stream(out, useCSS);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) DOMImplementation(org.w3c.dom.DOMImplementation) SVGDOMImplementation(org.apache.batik.anim.dom.SVGDOMImplementation) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) OutputStreamWriter(java.io.OutputStreamWriter) Dimension(java.awt.Dimension) PdfWriter(com.itextpdf.text.pdf.PdfWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Aggregations

SVGGraphics2D (org.apache.batik.svggen.SVGGraphics2D)21 Document (org.w3c.dom.Document)14 DOMImplementation (org.w3c.dom.DOMImplementation)12 OutputStreamWriter (java.io.OutputStreamWriter)8 GenericDOMImplementation (org.apache.batik.dom.GenericDOMImplementation)8 Dimension (java.awt.Dimension)7 FileOutputStream (java.io.FileOutputStream)7 Writer (java.io.Writer)6 IOException (java.io.IOException)5 File (java.io.File)4 SVGGeneratorContext (org.apache.batik.svggen.SVGGeneratorContext)4 Rectangle (java.awt.Rectangle)3 Rectangle2D (java.awt.geom.Rectangle2D)3 FileNotFoundException (java.io.FileNotFoundException)3 SVGDOMImplementation (org.apache.batik.anim.dom.SVGDOMImplementation)3 Element (org.w3c.dom.Element)3 PdfWriter (com.itextpdf.text.pdf.PdfWriter)2 Font (java.awt.Font)2 BufferedOutputStream (java.io.BufferedOutputStream)2 PrintWriter (java.io.PrintWriter)2