When I call
setFixedHeight() on a
PdfPCell, and add more text than fits in the given height,
iText seems to print the prefix of the string which fits.
Can I control this clipping algorithm? For example:
Are any of these possible? Does iText give me any control over how text is clipped? Thanks.
I have written a proof of concept, ClipCenterCellContent,
where we try to fit the text
"D2 is a cell with more content than we can fit into the cell." in a cell that is too small.
We add the content using a
CellRenderer and overriding the
In iText 7 you would need to manipulate with table renderer a bit:
IRenderer pr = content.createRendererSubTree().setParent(this); LayoutResult textArea = pr.layout(new LayoutContext( new LayoutArea(0, new Rectangle(getOccupiedAreaBBox().getWidth(), 1000))));
A renderer is created, and
layout() is called for an area which is clearly big enough to fit the cell, afterwards you can get the actual occupied area of a cell:
float spaceneeded = textArea.getOccupiedArea().getBBox().getHeight(); System.out.println(String.format("The content requires %s pt whereas the height is %s pt.", spaceneeded, getOccupiedAreaBBox().getHeight()));
We now know the needed height and we can add the content using an offset:
float offset = (getOccupiedAreaBBox().getHeight() - textArea.getOccupiedArea().getBBox().getHeight()) / 2; System.out.println(String.format("The difference is %s pt; we'll need an offset of %s pt.", -2f * offset, offset)); PdfFormXObject xObject = new PdfFormXObject(new Rectangle(getOccupiedAreaBBox().getWidth(), getOccupiedAreaBBox().getHeight())); Canvas layoutCanvas = new Canvas(new PdfCanvas(xObject, drawContext.getDocument()), drawContext.getDocument(), new Rectangle(0, offset, getOccupiedAreaBBox().getWidth(), spaceneeded)); layoutCanvas.add(content); drawContext.getCanvas().addXObject(xObject, occupiedArea.getBBox().getLeft(), occupiedArea.getBBox().getBottom());
In this case, I used a
PdfFormXObject to clip the content.
Click this link if you want to see how to answer this question in iText 5.