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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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));
回答2:
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
}