iText 7:如何在Div中允许溢出?

时间:2022-10-11 21:07:49

I have a Div with a certain height:

我有一个具有一定高度的Div:

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:

如果,对于Div,我添加一个段落,其中有几行占据比Div高的区域,我收到以下警告:

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.

除此之外,省略了段落的行。即使段落可能溢出Div的底部边界,它也会在边界之上结束。

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.

但是尽管有警告我并不在乎,我甚至需要段落以隐藏的方式溢出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>.)

(可以通过在HTML

上设置overflow:hidden来实现我所需行为的CSS等效。)

2 个解决方案

#1


5  

You can consider using a custom DivRenderer for those DIVs.

您可以考虑为这些DIV使用自定义DivRenderer。

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;
}

(OverflowHiddenDivRenderer)

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)

(RenderDivOverflowHidden测试testOverflowHiddenDivRenderer)

for Document document you get

您获得的文档文档

iText 7:如何在Div中允许溢出?

iText 7:如何在Div中允许溢出?

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).

请注意,即使我已经在iText 7上使用了很长时间,这是我第一次尝试创建自定义DivRenderer,我可能已经忘记了一些特殊情况。我特别想到旋转内容(在super.getOc​​cupiedAreaBBox()中有影响)或区域中断的上下文中的问题(我没有在OverflowHiddenDivRenderer中设置具有适应高度的下一个渲染器)。

Some people more proficient in this stuff may come up with some improvements ...

有些人对这些东西更精通可能会有一些改进......

#2


3  

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. iText 7:如何在Div中允许溢出?

是否可以将带有alpha的渐变叠加到div的底部?然后,即使文本在整数行被截断,它也清楚地表明文本被切断而不仅仅是结束。

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) iText 7:如何在Div中允许溢出?

有可能有一种方法,但最简单的方法是只保存一个用于此目的的图像并将其与div的底部对齐,因为看起来div的大小是固定的。像这样的东西(但有阿尔法)

If its not fixed, it should be easy enough to generate dynamically.

如果它没有修复,它应该很容易动态生成。

#1


5  

You can consider using a custom DivRenderer for those DIVs.

您可以考虑为这些DIV使用自定义DivRenderer。

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;
}

(OverflowHiddenDivRenderer)

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)

(RenderDivOverflowHidden测试testOverflowHiddenDivRenderer)

for Document document you get

您获得的文档文档

iText 7:如何在Div中允许溢出?

iText 7:如何在Div中允许溢出?

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).

请注意,即使我已经在iText 7上使用了很长时间,这是我第一次尝试创建自定义DivRenderer,我可能已经忘记了一些特殊情况。我特别想到旋转内容(在super.getOc​​cupiedAreaBBox()中有影响)或区域中断的上下文中的问题(我没有在OverflowHiddenDivRenderer中设置具有适应高度的下一个渲染器)。

Some people more proficient in this stuff may come up with some improvements ...

有些人对这些东西更精通可能会有一些改进......

#2


3  

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. iText 7:如何在Div中允许溢出?

是否可以将带有alpha的渐变叠加到div的底部?然后,即使文本在整数行被截断,它也清楚地表明文本被切断而不仅仅是结束。

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) iText 7:如何在Div中允许溢出?

有可能有一种方法,但最简单的方法是只保存一个用于此目的的图像并将其与div的底部对齐,因为看起来div的大小是固定的。像这样的东西(但有阿尔法)

If its not fixed, it should be easy enough to generate dynamically.

如果它没有修复,它应该很容易动态生成。