Java如何在iText中设置段落行间距?
要设置的行间距在iText的段落可以通过传递线空间/领先的参数进行Paragraph构造。在下面的示例中,我们将行距设置为32。我们还可以通过调用此对象的setSpacingBefore()和setSpacingAfter()方法来设置段落之间的间隔。
package org.nhooo.example.itextpdf; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class ParagraphLineSpaceDemo { public static void main(String[] args) { Document doc = new Document(); try { FileOutputStream fos = new FileOutputStream("ParagraphLineSpace.pdf"); PdfWriter.getInstance(doc, fos); doc.open(); String content = "The quick brown fox jumps over the lazy dog"; //将段落行距设置为32 Paragraph para1 = new Paragraph(32); //在段落之前和之后设置空间 para1.setSpacingBefore(50); para1.setSpacingAfter(50); for (int i = 0; i < 10; i++) { para1.add(new Chunk(content)); } doc.add(para1); Paragraph para2 = new Paragraph(); for (int i = 0; i < 10; i++) { para2.add(new Chunk(content)); } doc.add(para2); doc.close(); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } } }
Maven依赖
<!-- http://repo1.maven.org/maven2/com/itextpdf/itextpdf/5.5.10/itextpdf-5.5.10.jar --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency>