本文介绍了删除纸Itext7中的第一行和最后几行属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果t.setRise(+-)离开字段文件,我需要删除Text(setRise)中的属性.

I need to remove property in Text (setRise) , if t.setRise(+-) gets out of fields paper.

    PdfDocument pdfDoc = new PdfDocument(pdfWriter);
    Document doc = new Document(pdfDoc, PageSize.A5);
    doc.setMargins(0,0,0,36);
    for (int i = 0; i <50 ; i++) {
        Text t = new Text("hello " + i);
        if(i ==0){
            t.setTextRise(7);
        }
        if(i==31){
            t.setTextRise(-35);
        }
    Paragraph p = new Paragraph(t);
    p.setNextRenderer(new ParagraphRen(p,doc));
    p.setFixedLeading(fixedLeading);

     doc.add(p);
    }
    doc.close();
}

class ParagraphRen extends ParagraphRenderer{
private float heightDoc;
private float marginTop;
private float marginBot;



public ParagraphRen(Paragraph modelElement, Document doc) {
    super(modelElement);
    this.heightDoc =doc.getPdfDocument().getDefaultPageSize().getHeight();
    this.marginTop = doc.getTopMargin();
   this.marginBot = doc.getBottomMargin();


}

@Override
public void drawChildren(DrawContext drawContext) {
    super.drawChildren(drawContext);
    Rectangle rect = this.getOccupiedAreaBBox();
    List<IRenderer> childRenderers = this.getChildRenderers();
    //check first line
    if(rect.getTop()<=heightDoc- marginTop) {
        for (IRenderer iRenderer : childRenderers) {
            if (iRenderer.getModelElement().hasProperty(72)) {
            Object property = iRenderer.getModelElement().getProperty(72);
            float v = (Float) property + rect.getTop();
            //check text  more AreaPage
            if(v >heightDoc){
                iRenderer.getModelElement().deleteOwnProperty(72);
            }

        }
    }
    }
    //check last line
      if(rect.getBottom()-marginBot-rect.getHeight()*2<0){
        for (IRenderer iRenderer : childRenderers) {


            if (iRenderer.getModelElement().hasProperty(72)) {
                Object property = iRenderer.getModelElement().getProperty(72);


                      //if setRise(-..) more margin bottom  setRise remove
                if(rect.getBottom()-marginBot-rect.getHeight()+(Float) property<0)
                    iRenderer.getModelElement().deleteOwnProperty(72);
                }

            }
        }

    }

}

在这里,我检查带有setRise的第一行是否有更多的纸张区域,我删除了setRise属性.

Here i check if first lines with setRise more the paper area I remove setRise property.

如果最后一行带有serRise(-35)的行比边缘底部多,我将其删除.

And if last lines with serRise(-35) more then margin bottom I remove it.

但是它不起作用.属性不会删除.

But it doesn't work. Properties don't remove.

推荐答案

您的问题如下:渲染完成后,调用drawChildren方法.在此阶段,iText通常不考虑任何元素的属性:它只是将元素放置在其占用的区域中,该区域是在layout()阶段之前计算出来的.

Your problem is as follows: drawChildren method gets called after rendering has been done. At this stage iText usually doesn't consider properties of any elements: it just places the element in its occupied area, which has been calculated before, at layout() stage.

您可以通过布局仿真来克服它.

You can overcome it with layout emulation.

让我们将所有段落添加到div中,而不是直接添加到文档中.然后模拟将此div添加到文档中:

Let's add all your paragraphs to a div rather than directly to the document. Then emulate adding this div to the document:

LayoutResult result = div.createRendererSubTree().setParent(doc.getRenderer()).layout(new LayoutContext(new LayoutArea(0, PageSize.A5)));

在上面的代码段中,我尝试将div布局在A5尺寸的文档上.

In the snippet above I've tried to layout our div on a A5-sized document.

现在,您可以考虑布局的结果并更改某些元素,然后将使用Document#add将其进行实际处理.例如,要获得第30个布局的段落,可以使用:

Now you can consider the result of layout and change some elements, which will be then processed for real with Document#add. For example, to get the 30th layouted paragraph one can use:

((DivRenderer)result.getSplitRenderer()).getChildRenderers().get(30);

更多提示:拆分渲染器代表iText可以放置在区域上的内容部分,即溢出的内容.

Some more tips:split renderer represent the part of the content which iText can place on the area, overflow - the content which overflows.

这篇关于删除纸Itext7中的第一行和最后几行属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 10:51