Java如何在iText中设置段落缩进?
要在iText paragraph对象中设置段落缩进,可以使用setFirstLineIndent()、setIndentationLeft()和setIndentationRight()。这些方法接受浮点值作为参数。
setFirstLineIndent()方法设置段落的第一行缩进,而setIndentationLeft()和setIndentationRight()方法设置段落的左缩进和右缩进。下面是一个例子:
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 ParagraphIndentationDemo {
private static final String CONTENT = "Lorem ipsum dolor sit amet, " +
"consectetur adipisicing elit, sed do eiusmod tempor incididunt " +
"ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
"quis nostrud exercitation ullamco laboris nisi ut aliquip " +
"ex ea commodo consequat. Duis aute irure dolor in reprehenderit " +
"in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " +
"officia deserunt mollit anim id est laborum.";
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("ParaIndentation.pdf"));
document.open();
Paragraph paragraph = new Paragraph();
paragraph.add(new Chunk(ParagraphIndentationDemo.CONTENT));
//设置段落的第一行缩进
paragraph.setFirstLineIndent(75);
// 设置段落左侧缩进
paragraph.setIndentationLeft(50);
// 设置段落右侧缩进
paragraph.setIndentationRight(25);
document.add(paragraph);
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
} finally {
document.close();
}
}
}
Maven Dependencies
<!-- 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>
以上是 Java如何在iText中设置段落缩进? 的全部内容, 来源链接: utcz.com/z/355920.html