PDF to Image Conversion in Java
- by Geertjan
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;