Itext 学习笔记(四) Paragraph (段)的用法
Itext的com.itextpdf.text.Paragraph 类是段落的处理。在一个段落中,你可以设置段落的对齐方式,缩进和间距。
例子一代码如下
import java.io.FileNotFoundException;import java.io.FileOutputStream;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;public class DocumentExample { public static void main(String[] args) { //创建文本 Document document = new Document(); try { //写入文本到文件中 PdfWriter.getInstance(document, new FileOutputStream("Paragraph.pdf")); //打开文本 document.open(); //定义段落 Paragraph paragraph = new Paragraph(); //插入十条文本块到段落中 int i=0; for(i=0; i<10; i++){ Chunk chunk = new Chunk("This is a sentence which is long " + i + ". "); paragraph.add(chunk); } //添加段落 document.add(paragraph); //关闭文本 document.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }}
Paragraph paragraph = new Paragraph(50);
paragraph.setSpacingAfter(50);paragraph.setSpacingBefore(50);
paragraph.setAlignment(Element.ALIGN_LEFT);paragraph.setAlignment(Element.ALIGN_CENTER);paragraph.setAlignment(Element.ALIGN_RIGHT);
paragraph.setIndentationLeft(50);paragraph.setIndentationRight(50);
import java.io.FileNotFoundException;import java.io.FileOutputStream;import com.itextpdf.text.Chunk;import com.itextpdf.text.Document;import com.itextpdf.text.DocumentException;import com.itextpdf.text.Element;import com.itextpdf.text.Paragraph;import com.itextpdf.text.pdf.PdfWriter;public class DocumentExample { public static void main(String[] args) { //创建文本 Document document = new Document(); try { //写入文本到文件中 PdfWriter.getInstance(document, new FileOutputStream("Paragraph.pdf")); //打开文本 document.open(); //定义段落 Paragraph paragraph = new Paragraph(); //设置段落前后间距 paragraph.setSpacingAfter(25); paragraph.setSpacingBefore(25); //设置段段落居中 paragraph.setAlignment(Element.ALIGN_CENTER); //设置缩进 paragraph.setIndentationLeft(50); paragraph.setIndentationRight(50); //插入十条文本块到段落中 int i=0; for(i=0; i<10; i++){ Chunk chunk = new Chunk("This is a sentence which is long " + i + ". "); paragraph.add(chunk); } //添加段落 document.add(paragraph); //关闭文本 document.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }}