Java如何在iText中设置段落对齐方式?
要设置段落对象的对齐方式,可以使用段落.setAlignment()方法。我们可以传递常量,例如段落左对齐, 段落.u居中对齐, 段落右对齐setAlignment()方法。
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 ParagraphAlignment {
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("ParaAlign.pdf"));
document.open();
//创建对段落内容的检查
Chunk chunk = new Chunk(ParagraphAlignment.CONTENT);
// 创建段落并设置段落的对齐方式。
// 我们使用段落 Paragraph.ALIGN_LEFT, Paragraph.ALIGN_CENTER
// 以及 Paragraph.ALIGN_RIGHT
Paragraph para1 = new Paragraph(chunk);
para1.setAlignment(Paragraph.ALIGN_LEFT);
para1.setSpacingAfter(50);
document.add(para1);
Paragraph para2 = new Paragraph(chunk);
para2.setAlignment(Paragraph.ALIGN_CENTER);
para2.setSpacingAfter(50);
document.add(para2);
Paragraph para3 = new Paragraph(chunk);
para3.setAlignment(Paragraph.ALIGN_RIGHT);
document.add(para3);
document.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>
以上是 Java如何在iText中设置段落对齐方式? 的全部内容, 来源链接: utcz.com/z/326320.html