简体   繁体   English

删除每页第一行的 FixedLeading

[英]Remove FixedLeading at the first line on each page

I want to remove setFixedLeading at the first line on each page (100+)我想在每个页面的第一行删除setFixedLeading (100+)

I read a bit text(more 100 page with help while).我读了一点文字(在帮助下阅读了更多 100 页)。 And I set padding and margin to 0 but I still have top indent.我将填充和边距设置为 0,但我仍然有顶部缩进。 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 file: https://pdfhost.io/v/Byt9LHJcy_PDFpdf.pdf 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.幸运的是,布局机制非常灵活,您可以在几行代码中实现所需的行为。

First off, let's not use setFixedLeading and set the top margin for all paragraphs instead:首先,我们不要使用setFixedLeading设置所有段落的上边距:

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.这几乎不会改变视觉结果中的任何东西——这只是另一种做事方式。

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:我们将覆盖layout方法并检查我们给定的区域是否位于页面顶部 - 如果是,我们将不应用顶部边距:

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

Visual result:视觉效果:

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM