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: