移除每页第一行的固定行距

huangapple go评论68阅读模式
英文:

Remove FixedLeading at the first line on each page

问题

我想要在每一页的第一行移除 `setFixedLeading`(共100+页)

我阅读了一些文本(超过100页,使用了帮助的 while 循环)。我将内边距和外边距都设置为0,但仍然有顶部缩进。为什么?请帮助我。如何删除它?

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

![第一页:][2]

![第二页][1]

注:翻译的内容已经过处理,只包含翻译好的部分,去除了多余的内容。

英文:

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

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 = &quot;PDF.pdf&quot;;
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 &lt;20 ; i++) {
        Paragraph element = new Paragraph(&quot;p &quot; + i);
        element.setPadding(0);
        element.setMargin(0);
        element.setFixedLeading(55);
        doc.add(element);
    }
    doc.close();

}

PDF file:
https://pdfhost.io/v/Byt9LHJcy_PDFpdf.pdf

移除每页第一行的固定行距

移除每页第一行的固定行距

答案1

得分: 0

在元素创建时,您不知道它将出现在哪个页面上,也不知道其最终位置。我认为没有一种属性可以根据其是否是页面上的顶部元素来配置行为(这样的属性将过于定制化并且与特定工作流程绑定)。

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

首先,让我们不要使用 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();

这在视觉结果上几乎没有改变 - 这只是另一种做事情的方式。

现在,我们需要一个自定义的渲染器来调整段落的行为,如果它在页面顶部呈现。我们将覆盖 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);
    }
}

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

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

视觉结果:

移除每页第一行的固定行距

英文:

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:

Document doc = new Document(pdfDocument);
doc.setMargins(0, 0, 0, 0);
for (int i = 0; i &lt; 20; i++) {
    Paragraph element = new Paragraph(&quot;p &quot; + 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:

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:

移除每页第一行的固定行距

huangapple
  • 本文由 发表于 2020年10月4日 21:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64195180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定