Java如何使用iText段落类?
该com.itextpdf.text.Paragraph级代表PDF文档中的段落。下面的示例向您展示如何创建一个简单的段落。首先,我们创建一个Paragraph对象,然后使用该Chunk对象向其中添加一些文本。
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 ParagraphDemo { public static void main(String[] args) { Document doc = new Document(); try { FileOutputStream fos = new FileOutputStream("ParagraphDemo.pdf"); PdfWriter.getInstance(doc, fos); doc.open(); String content = "The quick brown fox jumps over the lazy dog"; Paragraph paragraph = new Paragraph(); for (int i = 0; i < 20; i++) { Chunk chunk = new Chunk(content); paragraph.add(chunk); } doc.add(paragraph); 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>