Sunday, April 6, 2014

Eclipse - "path/to/project" overlaps the location of another project: "your project"

Sometimes in eclipse we want to open a project that can't be imported because it still doesn't have Eclipse's configuration files (.project file)
The thing is we want to put it in ou workspace and then click on File >> New >> Java Project 
Then we uncheck the "Use default location" box and select our project folder in the workspace and there comes the surprise:


This beautiful small error message. I've recently found out how to deal with it and it's definitely not intuitive.
To avoid it, create the project as you would before (using  File >> New >> Java Project ) but leave "Use default location" checked and in the Project Name field use the name of the folder you put in your workspace. It's going to create the project without complaints and use your existing code.


Creating paginated table with PDFBox

I've recently needed to modify a file generation that was offered in CSV files to PDF format. I'll enumerate some of challenges on doing that:

Finding the right API to do the heavy work
PDF is a complex format created to display documents. It supports texts, graphics and a whole set of features, and has an 747 pages specification that can be bought here: ISO 32000-1 specification. One does not simply start writing to a PDF file, as unlike a text file, they usually contain non-ASCII binary characters and should always be considered as binary files.

Using an off the shelf API can greatly reduce the burden of trying to do that kind of file creation or modification manually. After some quick research I've realized that most o the available libs on the web were paid. The most used standard is iText, which needs a commercial license if being used for commercial purposes (http://itextpdf.com/salesfaq) The best free solution I found was PDFBox, which immediately drew my attention for being an Apache project. It is currently on the 1.8.4 release, is stable and has a fairly extensive amount of documentation on it's website and forums.

Now the thing is, I went through mailing lists and documentation and it doesn't come with any ready-made feature for tables generation. That requires for the developer to handle the drawing of the table's columns and rows. The following code performs that task and also handles paginating the table to multiple pages in case it doesn't fit.

Output sample:


I've meant to do just an essay here, so there are many optimizations to be done, but you can get some ideas from it to adapt to your needs. (full working code: https://github.com/eduardohl/Paginated-PDFBox-Table-Sample)