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