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

java - IText .setLeading(nn) giving unexpected Page Layout - Stack Overflow

programmeradmin4浏览0评论

I'm trying to understand how to layout a Page in iText.

I'm using iText 9.1.0 with JDK 21 under Windows 10 Pro.

The code & a Screenshot follow.

The Subject is separated from the address as expected.
But why is the 1st Detail Line so far below the Subject?

Here's the code:

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

Here's the result:

发布评论

评论列表(0)

  1. 暂无评论