一、Phrase
phrase就是包含了Chunk类的一个ArraryList,因此Phrase是一个组合的对象。是一系列以特定间距(两行之间的距离)作为参数的块。
Phrase行间距(备注:行间距指的是在一个短语内,多个chunk之间的行距可以通过构造方法传入的参数决定,也可以使用方法setLeading()来进行设定):
在iText中,如果没有显示的设置leading,Text会在加入到document中的Phrase或者Paragraph中查找其字体大小,
然后乘以1.5就是最后的leading。当一行中的内容大于doc文档的宽度时会自动换行。
public class Three {
public static void main(String[] args) throws Exception {
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("d:\\three.pdf"));
doc.open();
Phrase p = new Phrase();
p.setLeading(120);
p.add(new Chunk("sdfsdfdsfsdfsdf"));
p.add(new Chunk("sdfsdfdsfsdfsdf"));
p.add(new Chunk("sdfsdfdsfsdfsdf"));
p.add(new Chunk("sdfsdfdsfsdfsdf"));
p.add(new Chunk("sdfsdfdsfsdfsdf"));
p.add(new Chunk("sdfsdfdsfsdfsdf"));
p.add(new Chunk("sdfsdfdsfsdfsdf"));
p.add(new Chunk("sdfsdfdsfsdfsdf"));
p.add(new Chunk("sdfsdfdsfsdfsdf"));
doc.add(p);
doc.close();
}
}
误区:在添加多个Phrase时,对其中的Phrase进行行间距的设置是 “无用的”
public class Three {
public static void main(String[] args) throws Exception {
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("d:\\three.pdf"));
doc.open();
Phrase p1 = new Phrase();
p1.setLeading(50);
p1.add("fsdfsdfsdfsdf");
Phrase phrase1 = new Phrase("this is a phrase");
Phrase phrase2 = new Phrase(16, "this is a phrase with leading 16");
Phrase phrase5 = new Phrase(18, new Chunk("this is a phrase", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, BaseColor.RED)));
BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Phrase p6 = new Phrase("中俄文",new Font(baseFont));
doc.add(p1);
doc.add(phrase1);
doc.add(phrase1);
doc.add(phrase1);
doc.add(phrase1);
doc.add(phrase1);
doc.add(phrase2);
doc.add(phrase5);
doc.add(p6);
doc.close();
}
}