最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

itext - iText9 Bug: paragraph.setFixedLeading(...) ->> incorrect spacing between Paragraphs - Stack Overflow

programmeradmin0浏览0评论

iText v9.1.0 seems to have a problem with Paragraph leading.

I expect leading to insert space between the previous Paragraph & the current one.
(I guess it could also be interpreted as between the current Paragraph & the next one, which would seem counter-intuitive, but either before or after & certainly not both)

But with new Paragraph().setFixedLeading(188) I am getting a large spacing both before and after the Paragraph.

In the following example, I would expect the Detail line to follow the Subject line with just a little gap, but that is not the case.

Please find below the Source code & some sample output.

import java.io.*;
import java.nio.file.*;
import java.util.stream.Stream;

import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.properties.TabAlignment;

public class Layout {

    private static final TabStop TAB_400_R = new TabStop(400, TabAlignment.RIGHT);

    public static void main(final String[] args) throws IOException {

        final     var wp        = new WriterProperties();
        ;             wp.setPdfVersion(PdfVersion.PDF_2_0);

        final     var pdfOst   = new ByteArrayOutputStream();
        try(          pdfOst;
            final var pdfWtr   = new PdfWriter  (pdfOst, wp);

            final var pdfDoc   = new PdfDocument(pdfWtr);
            final var doc      = new Document   (pdfDoc) )
        {
            generate (pdfDoc, doc);
        }
        Files.write(Path.of("my.pdf"), pdfOst.toByteArray(), StandardOpenOption.CREATE);
    }

    private static void generate(final PdfDocument pdfDoc, final Document doc) {

        doc.setMargins(142, 59, 44, 55);

        Stream.of(
                "Full Name",
                "77, Croydon Avenue",
                "Redhill",
                "Surrey",
                "RH11 8AB").forEach(tx -> doc.add(new Paragraph(tx).addTabStops(TAB_400_R).add(new Tab()).add(tx)));

        doc.add(new Paragraph("S U B J E C T   .setFixedLeading(188)").setFixedLeading(188).setUnderline(0.75f, -1.5f));

        Stream.of(
                "Detail 1, .setFixedLeading(6). Why is this line so far below the Subject?",
                "Detail 2, .setFixedLeading(6). ",
                "Detail 3, .setFixedLeading(6). ").forEach(tx -> doc.add(new Paragraph(tx).setFixedLeading(6)));
    }
}

发布评论

评论列表(0)

  1. 暂无评论