java code to reduce space between lines

时间:2016-08-31 18:31:28

标签: java pdf itext

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.

2 个答案:

答案 0 :(得分:2)

两行之间的空格称为前导

您可以使用setleading()方法更改它。

例如:

p.setleading(15);

注意:默认情况下,iText占用字体大小的1.5倍。如果字体大小为12,则默认情况下前导为18。

您还可以在构造函数中设置前导: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));

答案 1 :(得分:0)

Here is a quick demo, see if that will give you the right idea

import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ParagraphSpaceDemo {
public static void main(String[] args) {
    Document document = new Document();

    try {
        String name = "ParagraphSetting.pdf";
        FileOutputStream fos = new FileOutputStream(name);
        PdfWriter.getInstance(document, fos);
        document.open();

        String content = "The quick brown fox jumps over the lazy dog";

        // Setting paragraph line spacing to 32
        Paragraph para1 = new Paragraph(32);

        // Setting the space before and after the paragraph
        para1.setSpacingBefore(50);
        para1.setSpacingAfter(50);
        for (int i = 0; i < 10; i++) {
            para1.add(new Chunk(content));
        }
        document.add(para1);

//irrelevant code
}