java 减少行间 space 的代码

java code to reduce space between lines

我正在创建一个包含一些内容的 pdf。我的要求是在创建 pdf 时减少行之间的 space,以便更多行数适合单个页面。

这是一个快速演示,看看它是否会给你正确的想法

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
}

两行之间的space称为前导

你用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));