Using JRuby with Adobe PDF Library’s Java Interface

Using JRuby with Adobe PDF Library’s Java Interface

Last month, I started exploring using JRuby with our PDF Java Toolkit. But since we also have another product with a Java interface, I couldn’t resist experimenting with making JRuby work with it as well.

So for this round, I re-implemented the Annotations sample from Adobe PDF Library’s (APDFL) Java interface. The Annotations sample lists the annotations on the first page of a given document. The advantage of this sample app is that it’s a nice and tight little program which allows us to concentrate on the configuration of JRuby and APDFL together.

Because com.datalogics.PDFL.jar is an interface to APDFL, you could potentially have a ruby application running in one directory, the jar stored in another directory, the APDFL DLLs in a third directory, and resource files stored in yet another set of directories. And this time around, I didn’t want to hard-code the path to the jar inside of my JRuby program.

After that, the java_import section, which is similar, ha, to Java’s Import section, I found I only needed to import the classes I was going to be instantiating from scratch. This reduces the size of this section quite substantially.

Document and Library will need to be imported with pretty much every APDFL program. LibraryFlags, and EnumSet, I only needed because of the Library constructor I used.

Here, because I’ve placed APDFL’s Resources folder in a non-standard location on my hard drive, we need to tell the Library where to find everything.

The first argument to the Library constructor is supposed to be a list of font directories. This list could be more than one directory. But at minimum, it should contain one directory path with the fonts that ship with APDFL.

The CMap and Unicode directories are straightforward variations, but the new-ish ColorProfilePath parameter breaks the pattern slightly by requiring a path that’s one deeper than the prior parameters. Just because.

Lastly, Coming Soon, Any Minute Now, the LibraryFlags enumeration will have a new NO_FLAGS value. But in case you are not using the bleeding edge of APDFL versions, the IGNORE_CURRENT_DIRECTORY (when searching for fonts) is pretty safe to use.

Now, here we have my feeble attempt to provide a meaningful default input PDF file. It didn’t work. So please specify a valid input file on the command-line when trying this sample app.

Finally, the meat-and-potatoes of the application:

The original Annotations sample app, upon which this code is based, only described the annotations of the first page. In this version, we nest the annotation iteration loop inside a page iteration loop.

We also add a tiny bit more information about each Annotation. More specifically, we include a page number for each Annotation. And it’s sub-type. And the rectangle within which resides that Annotation on the page. See Below for an example of the output.

output of Jruby ListAnnotation.rb with default input

All in all, a simple demonstration app showing how to use APDFL’s Java interface with JRuby.

Leave a Reply

Your email address will not be published. Required fields are marked *