在每个页面的第一行删除FixedLeading [英] Remove FixedLeading at the first line on each page

查看:111
本文介绍了在每个页面的第一行删除FixedLeading的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在每页的第一行(100+)删除setFixedLeading

I want to remove setFixedLeading at the first line on each page (100+)

我读了一点文字(在帮助的同时还有100页).而且我将padding和margin设置为0,但是我仍然有最高缩进.为什么?请帮我吗?如何删除?

I read a bit text(more 100 page with help while). And I set padding and margin to 0 but I still have top indent. Why? Help me pls? How delete it?

public static final String DEST = "PDF.pdf";
public static void main(String[] args) throws FileNotFoundException {

    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
    Document doc = new Document(pdfDoc);
    doc.setMargins(0,0,0,0);
    for (int i = 0; i <20 ; i++) {
        Paragraph element = new Paragraph("p " + i);
        element.setPadding(0);
        element.setMargin(0);
        element.setFixedLeading(55);
        doc.add(element);
    }
    doc.close();

}

PDF档案: https://pdfhost.io/v/Byt9LHJcy_PDFpdf.pdf

推荐答案

在创建元素时,您不知道页面将终止于页面还是其最终位置.我认为没有一个属性可以让您根据它是否是页面的顶部元素来配置行为(该属性可能过于自定义,并且与特定的工作流程相关联).

At the time of element creation you don't know the page it will end up on nor its resultant position. I don't think there is a property that allows you to configure the behavior depending on whether it's the top element on a page (such property would be too custom and tied to a specific workflow).

幸运的是,布局机制非常灵活,您可以用几行代码实现所需的行为.

Fortunately, the layout mechanism is quite flexible and you can implement the desired behavior in a couple of lines of code.

首先,我们不要使用setFixedLeading而是设置所有段落的顶部边距:

First off, let's not use setFixedLeading and set the top margin for all paragraphs instead:

Document doc = new Document(pdfDocument);
doc.setMargins(0, 0, 0, 0);
for (int i = 0; i < 20; i++) {
    Paragraph element = new Paragraph("p " + i);
    element.setPadding(0);
    element.setMargin(0);
    element.setMarginTop(50);
    doc.add(element);
}
doc.close();

这几乎不会改变视觉结果中的任何内容-这只是另一种处理方式.

This does not pretty much change anything in the visual result - it's just another way of doing things.

现在,我们需要一个自定义渲染器来调整段落的行为(如果该段落显示在页面顶部).我们将重写layout方法,并检查给定的区域是否位于页面顶部-如果是,则不应用顶部空白:

Now, we need a custom renderer to tweak the behavior of a paragraph if it is rendered at the top of the page. We are going to override layout method and check if the area we are given is located at the top of the page - and if so, we will not apply the top margin:

private static class CustomParagraphRenderer extends ParagraphRenderer {

    Document document;

    public CustomParagraphRenderer(Paragraph modelElement, Document document) {
        super(modelElement);
        this.document = document;
    }

    @Override
    public IRenderer getNextRenderer() {
        return new ParagraphRenderer((Paragraph) modelElement);
    }

    @Override
    public LayoutResult layout(LayoutContext layoutContext) {
        if (layoutContext.getArea().getBBox().getTop() == document.getPdfDocument().getDefaultPageSize().getHeight()) {
            ((Paragraph)getModelElement()).setMarginTop(0);
        }
        return super.layout(layoutContext);
    }
}

现在我们唯一需要做的就是将自定义渲染器实例设置为循环中的每个段落:

Now the only thing we need to do is to set the custom renderer instance to each paragraph in the loop:

element.setNextRenderer(new CustomParagraphRenderer(element, doc));

视觉结果:

这篇关于在每个页面的第一行删除FixedLeading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆