Thursday, February 20, 2014

PDF to SVG - Java

I have been working with SVG formats recently. Mainly, trying to find a decent swing component for creating and modifying SVGs. As a result of such escapade, I decided to write a PDF to SVG converter. Now, I haven't fully tested it, but with the PDFs I have tried, it converted successfully.

The libraries I used were Batik 1.7 and PDF-Renderer v1.0.5

Here is My Main

public class ConversionMain
    public ConversionMain()
        File pdf = new File("ss12.pdf");

    public static void main(String[] args)
        new ConversionMain();

Here is my PDFUtility function for taking PDF, reading it and then converting to SVG

public class PDFUtility
    public static void PDF_TO_SVG(File pdfFile)
            RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
            FileChannel channel = raf.getChannel();
            ByteBuffer buf =, 0, channel.size());
            PDFFile pdf = new PDFFile(buf);

            DOMImplementation domImp = GenericDOMImplementation.getDOMImplementation();
            for (int i = 1; i <= pdf.getNumPages(); i++)
                PDFPage page = pdf.getPage(i);
                // image dimensions
                int width = (int) page.getWidth();  //Can be changed to whatever you like
                int height = (int) page.getHeight();

                Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
                Image image = page.getImage(width, height, rect, null, true, true);

                String svgNS = "";
                Document document = domImp.createDocument(svgNS, "svg", null);

                SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
                Graphics graphics = svgGenerator.create();
                graphics.drawImage(image, 0, 0, null);

                boolean userCSS = true;
                FileOutputStream fos = new FileOutputStream(new File("Page " + i + ".svg"));

                Writer out = new OutputStreamWriter(fos);

      , userCSS);
        catch (Exception e)


Try it out and let me know what you think



  1. It's entirely irritating in light of the fact that on my PC I can simply download a swf player and everything is great. On my Mac, notwithstanding, I can't discover 'whatever' will play them. At the point when I attempt to play them it just opens Illustrator, which (obviously) sits idle.

  2. I understand this column. I realize You put a many of struggle to found this story. I admire your process.

  3. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. cfa practice exam