I'm working in an app to generate some PDFs and I'm doing some POC with itext-pdf library.
I'm facing some issues finding the correct line spacing. I generated manually an example of what I want to achieve and it should look like this:
However the closest I've got is like this:
The code to generate the second one is like this:
@Test
void testDocumentWithCustomFont() throws FileNotFoundException {
inDocument(CUSTOM_FONT_DESTINATION, document -> {
try {
FontProgram fontProgram = FontProgramFactory.createFont(CUSTOM_FONT);
PdfFont customFont = PdfFontFactory.createFont(fontProgram, PdfEncodings.IDENTITY_H,
PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
Text title = new Text("THIS IS A TITLE").setFont(customFont);
title.setFontSize(18);
document.add(new Paragraph(title).setTextAlignment(TextAlignment.CENTER).setMarginBottom(12));
Text chordLine = new Text("THIS IS A RED LINE").setFont(customFont);
chordLine.setFontSize(12);
chordLine.setFontColor(ColorConstants.RED);
document.add(new Paragraph(chordLine).setMarginBottom(0));
Text lyricLine = new Text("THIS IS A BLACK LINE THAT SHOULD BE NEAR TO THE RED").setFont(customFont);
lyricLine.setFontSize(12);
document.add(new Paragraph(lyricLine));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
});
}
private void inDocument(String destination, Consumer<Document> documentConsumer) throws FileNotFoundException {
try (Document document = new Document(new PdfDocument(new PdfWriter(destination)))){
documentConsumer.accept(document);
}
}
How should I change the code in order to achieve the desired result? Thanks in advance for your comments
You can use setMultipliedLeading on both paragraphs to decrease the space between paragraphs. This is the result for the following code:
Text title = new Text("THIS IS A TITLE");
title.setFontSize(18);
document.add(new Paragraph(title).setTextAlignment(TextAlignment.CENTER).setMarginBottom(12));
Text chordLine = new Text("THIS IS A RED LINE");
chordLine.setFontSize(12);
chordLine.setFontColor(ColorConstants.RED);
document.add(new Paragraph(chordLine).setMarginBottom(0).setMultipliedLeading(0.7f));
Text lyricLine = new Text("THIS IS A BLACK LINE THAT SHOULD BE NEAR TO THE RED");
lyricLine.setFontSize(12);
document.add(new Paragraph(lyricLine).setMultipliedLeading(0.7f));