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");
        PDFUtility.PDF_TO_SVG(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)
    {
        try
        {
            RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
            FileChannel channel = raf.getChannel();
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 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 = "http://www.w3.org/2000/svg";
                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);

                svgGenerator.stream(out, userCSS);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
}

Try it out and let me know what you think

Thanks!

4 comments:

  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. anyconv.com

    ReplyDelete
  2. 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

    ReplyDelete
  3. All these names are cost of|in command of|in management of} preserving things entertainment. More importantly, they’re known for developing trusted video games frequently examined by probably the most authoritative bodies. At TuskCasino we're committed to providing you with fast and useful customer help. If might have} a query please e-mail us and our customer help staff will give you a reply in 24 hours or much less. 온라인 카지노 I agree that I wish to obtain data about Hard Rock International products and particular provides by e-mail.

    ReplyDelete