This is a small project that builds upon Apache's PDFBox (>= 3.0.0) and should allow you to create tables in a fairly simple way. It emerged from the need in another project. Therefore, it also may miss some crucial features. Nevertheless, there is:
One can also override classes that are responsible for table/cell drawing, i.e. their drawing behaviour can be customized to a pretty high extent.
It is also possible to draw a table over multiple pages (even with the header row being repeated on every new page) or to draw a large table's overflow next to the already existing table on the same page (see below for examples).
Add this to your pom.xml
:
<dependency>
<groupId>com.github.vandeseer</groupId>
<artifactId>easytable</artifactId>
<version>1.0.2</version>
</dependency>
Or checkout the repository and install it locally with maven (e.g. for thedevelop
branch):
mvn clean install -DskipTests -Dgpg.skip -Ddependency-check.skip=true
There is a minimal full working example which should help you to get started.
For a bit more involved tables have a look at this code which is needed for creating a PDF document with the following two tables:
For the next example have a look at the SettingsTest.java:
The last one illustrates the use of vertical text in text cells. The code for it can be found here:
Drawing the overflow of a large table on the same page is also possible:
If you run the tests with mvn clean test
there also some PDF documents created which you can find in the target
folder.
The corresponding sources (in order to understand how to use the code) can be found in the test package.
float
spom.xml
Every version of easytable < 1.0.0 is built on PDFBox 2.x.x.
Note that easytable 1.0.0 and below do basically only differ in the support PDFBox version, but not in their feature set. Only exception being the experimental support for paragraph cells which had to be dropped with the upgrade to PDFBox 3.
Also note that easytable >= 1.0.0 is built for Java 11 and higher, whereas lower versions are using Java 8.
Yep, you can customize the cell drawers itself or (depending on your use case) you can just create a custom cell.
For using a customized cell drawer, have a look at CustomCellDrawerTest.
In case you want to create your own type of cell (which shouldn't really be necessary since the
drawing can be completely adapted) you will need to use Lombok's @SuperBuilder
annotation. Again, just have a look at the code:
CustomCellWithCustomDrawerUsingLombokTest
Yes, have a look at TableOverSeveralPagesTest.java.
Just use startY(...)
and endY(..)
in order to restrict the vertical part of the page
where the table should be drawn:
RepeatedHeaderTableDrawer.builder()
.table(createTable())
.startX(50)
.startY(100F)
.endY(50F) // <-- If the table is bigger, a new page is started
.build()
Depending on whether you want to repeat the header row or not you
should use RepeatedHeaderTableDrawer
or TableDrawer
respectively.
y
coordinate of the end of a drawn table?Yes. Just use the .getFinalY()
method. Also see FinalYTest.java.
Yes. Or you can upvote this answer on stackoverflow. Or: