user3633028 user3633028 - 3 months ago 19
Java Question

java code to reduce space between lines

I am creating a pdf with some content in it. My requirement is to reduce the space between lines while creating the pdf so that more number of lines fit in single page.How can i reduce the space between lines,please suggest.

Document document = null;
PdfWriter.getInstance(document,new FileOutputStream("Paragraph2.pdf"));
document.open();

Font font = new Font(Font.FontFamily.COURIER,12);

//code to get the content to show in the document
.....

for (final String line : documentines.getDocumentLine())
{
document.add(new Paragraph(line,font));
}

Answer

The space between two lines is called the leading.

You change it with the setleading() method.

For instance:

p.setleading(15);

Note: by default, iText takes 1.5 times the font size. If the font size is 12, the leading will be 18 by default.

You can also set the leading in the constructor: http://itextsupport.com/apidocs/itext5/latest/com/itextpdf/text/Paragraph.html#Paragraph-float-java.lang.String-com.itextpdf.text.Font-

document.add(new Paragraph(15, line, font));