Friday, April 23, 2010

Open Source Java PDF Viewing Library Review

Here's a quick review of three open source Java based PDF libraries which can be used to view PDF documents in a Swing application. While there are some very good PDF generation libraries (iText and friends), there are fewer libraries which help render a PDF document into a type suitable for viewing in Swing.

JPedal
http://www.jpedal.org/

I've used JPedal in previous projects. It seems to work decently, but looking at the source code, the project is very disorganized and likely prone to bugs.

In my previous project, I had always exited my application by calling System.exit() or using the EXIT_ON_CLOSE parameter to the default close operation on a JFrame. It wasn't until playing with JPedal in a new project that I realized JPedal is hanging my swing app and preventing it from shutting down if my frames are simply disposed of. That is, if after having rendered a PDF document using JPedal, an application won't shutdown without having issued a System.exit().

I couldn't even find a single JPedal example where they used anything but System.exit(), so it seems they're probably aware of the problem and possibly hiding it? Don't know. I tried every combination I could find to cleanly close and dispose the JPedal resources to no avail. After looking at the source code, it doesn't necessarily surprise me.

I gave up on JPedal and was forced to move onto other choices.

PDF Renderer
https://pdf-renderer.dev.java.net/

This is/was a Sun sponsored open source project started by a few key Swing developers. I have met Richard Bair in person and he's a real great guy. So, this seemed like a logical next project to take a look at.

The API is by far the easiest to use. It's very simple to get a Swing based view of a PDF document up. However, the library currently only supports older PDF document versions. I have read that there is a patch available to allow rendering of new PDF. *bah* I was hoping for a solution that didn't require building the code myself. And, it somewhat looks like the project has stalled, which shouldn't be surprising given Sun's push on JavaFX and now Oracle.

Onto the next project.

Apache PDFBox
http://pdfbox.apache.org/

This turned out to be a good working solution. The API is a bit more confusing than the other two, and I couldn't seem to find an existing Swing component to render a PDF. But, the PDPage class of PDFBox has a convertToImage() method which returns a BufferedImage. So, it was fairly easy to take the generated image and draw onto the canvas of a JComponent.

My conversion code looks something like:
PDDocument doc = null;
try {
doc = PDDocument.load(inputStream);
final PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);

PipedInputStream pis = new PipedInputStream();
final PipedOutputStream pos = new PipedOutputStream(pis);

new Thread(new Runnable() {
@Override
public void run() {
try {
ImageIO.write(page.convertToImage(), "png", pos);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

// create image panel
JPanel panel = new ImagePanel(pis);

} finally {
if (doc != null) {
doc.close();
}
}
My ImagePanel requires an InputStream (for reasons specific to the application), thus why the use of the I/O pipes. I also wish I knew a better way to get just the first page of the PDF document (without a call to getAllPage()), since this is all I was interested in (the application is generating a preview of the first PDF page).

PDFBox renders the newest PDF documents (from what I can tell), so this is good. Like I said, the API is a bit more difficult to learn, though it's now seemingly straight forward (as can be seen from the above). PDFBox is also in the Maven central repository, which is great for Maven development. All in all, I'm pretty happy with PDFBox.

jPodRenderer
http://opensource.intarsys.de/home/en/index.php?n=JPodRenderer.HomePage

I just found jPod tonight, having skipped over it a few times when searching. It actually looks pretty promising, especially since they have a specific renderer which will render to both AWT and SWT. So, that's cool. Looks like they have dependency on quite a few jars, though, and they don't seem to be in the central Maven repository, which stinks because of the dependencies. Anyway, if anyone has a review of jPod, send a link my way; I'd like to read about it.

Updated May 28th, 2010.
ICEpdf
http://www.icepdf.org/

Thanks to Mark Stephens at JPedal for providing this link. I wasn't aware of the project.

Friday, April 16, 2010

JPedal LGPL Direct Links

I get tired of the hoops some companies expect you to go through to download the Free Software versions of their products. JPedal, a PDF viewing library for Java, is one such example. They have an LGPL version of their library, but you must fill out a form asking for email and name in order to get it. Big deal, right? Just make something up, right? Seems like they have someone manually processing these requests, which means delays in getting what you need.

This is Free Software, isn't it? So, here are the direct links to the JPedal LGPL licensed software:

Binary: http://www.jpedal.org/download/lgpl/jpedal_lgpl.jar

Source: http://www.jpedal.org/download/lgpl/jpedal_lgpl_src.zip

p.s. According to the source, the latest version (as of this writing) is: 4.11b76

Monday, February 15, 2010

Simple Java Event Bus

I've just released some code that has come in handy for quite a few projects I've worked on. It's called the Simple Java Event Bus and is released under the New BSD license. You can download the code here: http://code.google.com/p/simpleeventbus/

An event bus is a useful pattern for decoupling the components of a highly meshed application. Event bus is a substitute for the Observer pattern, useful when too many observers/observables exist to keep a clean design. Components can couple directly to the EventBus instead of to each other, allowing events or messages to be published to the bus. Listeners to the bus will then be notified about the event if they have interest in the event type.

I have used other event bus implementations in live code. However, I have found that the above library to be the most simple to use and to get started quickly with. Hope you can benefit from the code as well.

Tuesday, November 3, 2009

Economics in One Lesson

This is one of those links you tuck away and revisit when you need a fresh perspective or a reset of values when things are hazy...
Economics is haunted by more fallacies than any other study known to man. This is no accident. The inherent difficulties of the subject would be great enough in any case, but they are multiplied a thousandfold by a factor that is insignificant in, say, physics, mathematics or medicine-the special pleading of selfish interests. While every group has certain economic interests identical with those of all groups, every group has also, as we shall see, interests antagonistic to those of all other groups. ...
Economics in One Lesson

Tuesday, October 13, 2009

So You Want to Learn Japanese

You’ve eaten at a few Japanese restaurants, seen some anime, hosted an exchange student, and had a Japanese girlfriend. And now, somewhere in the back of your tiny brain, you think that Japanese would be a good language to learn…

http://pepper.idge.net/japanese/

Wednesday, June 24, 2009

How Much Student Loan Debt is Too Much?

How much student loan debt is too much? Anything over $0.00 is too much. It's that simple. There is absolutely no reason (and no return on investment) for a student to go high into debt at an expensive school than to stay locally and pay in-state tuition at a state school. The differentiation between the education at the "upper end" schools (which are exponentially more spendy) vs. the traditional state schools is insignificant.

On average, tuition for in-state schools is around $6000 / year (two semesters). That's literally $500 / month. Who can't afford that? Simple budgeting and cash flow affords that education. For $12000 / year, or $1000 / month, a student can stay at the dorms with a meal plan. What's exactly wrong with flipping a burger or delivering a pizza while attending school? How hard exactly is it to earn $1000 / month??

See, the "entitlement" mentality is infecting us, and debt has become the facilitator to these entitlements. It's absolutely criminal that parents are setting up their children for a large debt obligation right at the start of their adult life. Student loans are particularly nasty, since they don't disappear during bankruptcies, etc. How many people do you know in their 30's and 40's still paying off student loan debts? I actually knew this number at one point, but have forgotten, but it's exceptionally high.

My children are going to earn their way through school. I hope to have saved a little up for them, of course, and I hope they get scholarships. But, ultimately I hope they learn the value of hard work, budgeting, and staying off the American crack which we call debt.