Remove the first and last lines properties in the paper Itext7

♀尐吖头ヾ 提交于 2019-12-13 03:27:42

问题


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

            }
        }

    }

}

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

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

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


回答1:


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.

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

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

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

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



来源:https://stackoverflow.com/questions/56350909/remove-the-first-and-last-lines-properties-in-the-paper-itext7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!