PDF to Image Conversion in Java

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Sun, 2 Sep 2012 05:35:42 +0000 Indexed on 2012/09/02 9:45 UTC
Read the original article Hit count: 157

Filed under:

In the past, I created a NetBeans plugin for loading images as slides into NetBeans IDE. That means you had to manually create an image from each slide first. So, this time, I took it a step further. You can choose a PDF file, which is then automatically converted to an image for each page, each of which is presented as a node that can be clicked to open the slide in the main window.

As you can see, the remaining problem is font rendering. Currently I'm using PDFBox. Any alternatives that render font better?

This is the createKeys method of the child factory, ideally it would be replaced by code from some other library that handles font rendering better:

@Override
protected boolean createKeys(List<ImageObject> list) {
    mylist = new ArrayList<ImageObject>();
    try {
        if (file != null) {
            ProgressHandle handle = ProgressHandleFactory.createHandle(
                    "Creating images from " + file.getPath());
            handle.start();
            PDDocument document = PDDocument.load(file);
            List<PDPage> pages = document.getDocumentCatalog().getAllPages();
            for (int i = 0; i < pages.size(); i++) {
                PDPage pDPage = pages.get(i);
                mylist.add(new ImageObject(pDPage.convertToImage(), i));
            }
            handle.finish();
        }
        list.addAll(mylist);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    return true;
}

The import statements from PDFBox are as follows:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE