How to offset an SVG with batik when converting to PNG?

18 Views Asked by At

I have an SVG I want to convert to an image. I have some Java code using Batik to convert it to a PNG, but part of the SVG is not shown. It is like the 0,0 coordinate of the image is positioned to the actual middle of the image and the rest to the left and top are cut off. Looking at the SVG, some of the coordinates are negative (below 0,0) and I believe batik is not displaying part of the image because of this. I am pretty sure the SVG is complete.

Example SVG:

<svg xmlns="http://www.w3.org/2000/svg" ><g id='ID'>
<line stroke="#8000FF" stroke-width="8.386506" stroke-opacity="1" stroke-linecap="round" stroke-linejoin="miter" stroke-miter-limit="4" fill-opacity="0.0" x1="-1552.226439" y1="-403.612102" x2="-1770.651831" y2="-403.612102"></line>

Java Code:

    public static void convertSVGToPNG(String svgFile, String pngFile) throws Exception {
        try (InputStream inputStream = new FileInputStream(svgFile);
                OutputStream outputStream = new FileOutputStream(pngFile)) {

            // Get a DOM Implementation
            DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();

            // Create a Document
            Document document = domImpl.createDocument(null, "svg", null);

            // Set up transcoding hints
            TranscodingHints hints = new TranscodingHints();
            hints.put(PNGTranscoder.KEY_WIDTH, Float.valueOf(5000));
            hints.put(PNGTranscoder.KEY_HEIGHT, Float.valueOf(5000));
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, "svg");
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, "http://www.w3.org/2000/svg");
            hints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, document.getImplementation());
            hints.put(ImageTranscoder.KEY_BACKGROUND_COLOR, java.awt.Color.WHITE);

            // Create a PNG transcoder
            PNGTranscoder transcoder = new PNGTranscoder();
            transcoder.setTranscodingHints(hints);

            // Set up input and output
            TranscoderInput input = new TranscoderInput(inputStream);
            TranscoderOutput output = new TranscoderOutput(outputStream);

            // Perform the conversion
            transcoder.transcode(input, output);
        }
    }

How can I offset the coordinates so I can see the whole picture?

0

There are 0 best solutions below