Reputation: 4101
I have a Div
with a certain height:
Div div = new Div();
div.setHeight(100);
If, to the Div
, I add a paragraph with several lines that would occupy an area higher than the Div
, I receive the following warning:
WARN com.itextpdf.layout.renderer.BlockRenderer - Element content was clipped because some height properties are set.
And in addition to that, lines of the paragraph are omitted. Even though the paragraph could overflow the Div
's bottom border, it ends above the border.
But despite the warning I do not care and I even need the paragraph to overflow in a hidden manner below the bottom border of the Div
.
How can I achieve such a behavior?
(The CSS equivalent of the behavior I need can be achieved by setting overflow: hidden
on an HTML <div>
.)
Upvotes: 7
Views: 2848
Reputation: 95918
You can consider using a custom DivRenderer
for those DIVs.
A proof-of-concept:
public class OverflowHiddenDivRenderer extends DivRenderer {
public OverflowHiddenDivRenderer(Div modelElement) {
super(modelElement);
}
@Override
public Rectangle getOccupiedAreaBBox() {
Rectangle rectangle = super.getOccupiedAreaBBox();
if (height != null) {
if (rectangle.getHeight() > height.getValue()) {
rectangle.moveUp(rectangle.getHeight() - height.getValue()).setHeight(height.getValue());
}
}
return rectangle;
}
@Override
public LayoutResult layout(LayoutContext layoutContext) {
height = getPropertyAsUnitValue(Property.HEIGHT);
deleteProperty(Property.HEIGHT);
LayoutResult layoutResult = super.layout(layoutContext);
LayoutArea layoutArea = layoutResult.getOccupiedArea();
if (layoutArea != null) {
layoutArea.setBBox(getOccupiedAreaBBox());
}
return layoutResult;
}
UnitValue height;
}
Using it like this:
for (int height = 100; height < 150; height += 5) {
Div div = new Div();
div.setProperty(Property.OVERFLOW_Y, OverflowPropertyValue.HIDDEN);
div.add(new Paragraph(height + " Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."));
div.setHeight(height);
div.setNextRenderer(new OverflowHiddenDivRenderer(div));
document.add(div);
}
(RenderDivOverflowHidden test testOverflowHiddenDivRenderer
)
for Document document
you get
Beware, even though I've had my hands on iText 7 for quite some time now, this is my first attempt to create a custom DivRenderer
and I may well have forgotten some special cases. I think in particular of problems in context with rotated content (which in super.getOccupiedAreaBBox()
is of influence) or area breaks (I don't set a next renderer in OverflowHiddenDivRenderer
with an adapted height).
Some people more proficient in this stuff may come up with some improvements ...
Upvotes: 5
Reputation: 504
Would it be possible to superimpose an gradient with alpha onto the bottom of the div? Then even though the text is cut off at an integer line, its clear that the text is being cut off and not just ending.
It's possible there's a method for that, but it might be simplest to just save an image to use for this purpose and align it to the bottom of the div, since it seems like the size of the div is fixed. Something like this (but with alpha)
If its not fixed, it should be easy enough to generate dynamically.
Upvotes: 3