When you want to create a visible signature using PDFBox you need to create a Rectangle2D object.
Rectangle2D humanRect = new Rectangle2D.Float(100, 200, 150, 50);
I would like to know if it is possible to find all the white spaces(rectangles) in the document(or from the first/last page) of a certain size (width x height). I would like to choose one of these positions for my signature form.
I would like to use it as in the following example:
Rectangle2D humanRect = new Rectangle2D.Float(foundX, foundY, width, height);
As already confirmed in a comment to the question, you essentially are looking for a port of the functionality of the
FreeSpaceFinder
andFreeSpaceFinderExt
classes for iText from this answer to PDFBox. This is the focus of this answer:If you want to determine something from the content stream instructions of a page with PDFBox, you usually will create a class based on
PDFStreamEngine
or one of its subclasses. For anything that's not focusing on text extraction most often thePDFGraphicsStreamEngine
is the base class of choice.Based on that we can essentially copy the functionality of the mentioned iText based classes:
(FreeSpaceFinder)
Using this
FreeSpaceFinder
you can find empty areas with given minimum dimensions in a method like this:(DetermineFreeSpaces method
find
)Applied to the same PDF page as was the iText centric solution with minimum width 200 and height 50, we get:
Comparing to the analogous screen shot for the iText variant, we see that we get more possible rectangles here.
This is due to the iText solution using the font-level ascender and descender while we here use the individual glyph bounding boxes.