Saturday Sep 01, 2012

PDF to Image Conversion in Java

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;
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« September 2012 »
SunMonTueWedThuFriSat
      
2
16
19
      
Today