33
votes

We are planning to migrate our pdf generation utilities from iText to PDFBox (Due to licensing issues in iText). With some effort, I was able to write and position text, draw lines etc. But creating Tables with text embedded in Table cells is a challenge, I went through the documentation, examples, Google, Stackoverflow couldn't find a thing. Was wondering if PDFBox provides native support for creating Tables with embedded text. My last resort would be to use this link https://github.com/eduardohl/Paginated-PDFBox-Table-Sample

2
As far as document creation is concerned, PDFBox mainly is an equivalent to the low level API of iText. What you are missing is something replacing iText's high level API on top of that. I'm not aware of something like that available to the public.mkl
Yeah, you nailed it. High level API for PDFBox for creating all these tables is what is missing. A lot of dev community is migrating from iText to other open source pdf libraries and I am sure someone will have an elegant solution.Anil
@TilmanHausherr I'm afraid all those samples IMO meely are proofs of concept, probably of use in limited use cases but by far not for generic use. PDFBox has its strengths, e.g. a quite versatile content extraction framework and a content rendering capability, but the absence a proper layouting API is a serious weakness.mkl

2 Answers

88
votes

Since I also needed table drawing functionality for a side project, I implemented a small "table drawer" library myself, which I uploaded to github.

In order to produce such a table – for instance – ... enter image description here

... you would need this code. In the same file you find the code for that table as well:

enter image description here

The current "feature list" includes:

  • set font and font size on table level as well as on cell level
  • define single cells with bottom-, top-, left- and right-border width separately
  • define the background color on row or cell level
  • define padding (top, bottom, left, right) on cell level
  • define border color (on table, row or cell level)
  • specify text alignment (vertical and horizontal)
  • cell spanning and row spanning
  • text wrapping and line spacing

Also it should not be too hard to add missing stuff like having different border colors for borders on top, bottom, left and right-borders, if needed.

30
votes

Thanks to the links provided by Tilman. Using the boxable API (https://github.com/dhorions/boxable) I was able to create the table I wanted to. Just an FYI I wanted to create the table with variable number of cells. For example row 1 would have 2 cells, row 2 could have 5 cells and row 3 could have just 3 cells. I was able to do with ease. I followed Example1.java in the link mentioned above.