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?
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 MyDocument document: doc)
{
if (doc.getDocumentLines() != null)
{
final DocumentLines documentLines = doc.getLetterLines();
for (final String line : documentines.getDocumentLine())
{
document.add(new Paragraph(line,font));
}
}
}
I have used below line to reduce the space between lines.
Paragraph p = new Paragraph(12,line,font);
When I used leading with value 12 in my code, blank space between lines got reduced and more lines from page2 got shifted to page1. But the issue is when I gave Paragraph p = new Paragraph(12,line,font)
lines got shifted to page1 but I can see blank space/blank lines are in page2 as shown in the image below.
In the image above, ADDITIONAL INFORMATION line should be printed from top of the page, we can notice the blank space in page 2 in the above image.
When I set the leading with value 11 Paragraph(11,line,font)
, I noticed their is no additional space left as shown in the image below. But ADDITIONAL INFORMATION line was also in page1 which I was expecting in page2. For me leading with 12 will give the expected output, but as shown in image1 there are blank lines in page2 which is an issue to resolve. How can I overcome this blank space issue?
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));