itext基本编辑单元

在itext的com.lowagie.text包中有Chunk,Phrase,Paragraph这三种text编辑单元

其中Chunk是最小的单元

也就是说Paragraph可以包含多个Phrase,Phrase可以包含多个Chunk


下面是Chunk的例子

public class Chunk_Example {

public Chunk_Example() throws Exception{

Document document = new Document();
PdfWriter.getInstance(document,
new FileOutputStream("chunk_example.pdf"));
document.open();

Font font = new Font(Font.COURIER, 10, Font.BOLD); // 设置font
font.setColor(new Color(0x92, 0x90, 0x83));

Chunk chunk =
new Chunk("testing text element", font); // 初始化Chunk

chunk.setBackground(new Color(0xff, 0xe4, 0x00)); // 设置背景

document.add(chunk); // 添加chunk到文档

document.close();

}

public static void main(String[] args) {
try{
Chunk_Example textExample = new Chunk_Example();
}catch(Exception e){
System.out.println(e);
}
}
}



我们一般不使用Chunk生成文本,因为Chunk不能自动换行,而是使用Phrase代替


public class Phrase_Example {


public Phrase_Example() throws Exception{

Document document = new Document();
PdfWriter.getInstance(document,
new FileOutputStream("phrase_example.pdf"));
document.open();

Font font = new Font(Font.COURIER, 10, Font.BOLD);
font.setColor(new Color(0x92, 0x90, 0x83));
Chunk chunk = new Chunk("testing text element ", font);
chunk.setBackground(new Color(0xff, 0xe4, 0x00));

Phrase phrase =
new Phrase(20, "This is initial text. ");
//初始化Phrase,有20像素的间距
for(int i=0; i < 10; i++)
{
phrase.add(chunk); // 添加110个Chunk到Phrase
}

document.add(phrase);

document.close();
}


public static void main(String[] args) {
try{
Phrase_Example textExample = new Phrase_Example();
}catch(Exception e){
System.out.println(e);
}
}

}


Phrase已经满足大部分需求,但是如果我们想要一个分成几段来显示文本,可以使用
[b]phrase.add("\n");[/b]
也可以通过Paragraph来实现


public class Paragraph_Example {


public Paragraph_Example() throws Exception{

Document document = new Document();
PdfWriter.getInstance(document,
new FileOutputStream("paragraph_example.pdf"));
document.open();

Font font = new Font(Font.COURIER, 10, Font.BOLD);
font.setColor(new Color(0x92, 0x90, 0x83));
Chunk chunk = new Chunk("testing text element ", font);
chunk.setBackground(new Color(0xff, 0xe4, 0x00));

Phrase phrase = new Phrase(20, "This is initial text. ");

for(int i=0; i < 10; i++)
{
phrase.add(chunk);
}

Paragraph paragraph = new Paragraph();
paragraph.add(phrase);//添加phrase对象

document.add(paragraph); //添加 paragraph
document.add(paragraph); //添加 paragraph

document.close();
}


public static void main(String[] args) {
try{
Paragraph_Example textExample =
new Paragraph_Example();
}catch(Exception e){
System.out.println(e);
}
}

}



[quote]http://www.geek-tutorials.com/java/itext/insert_control_text.php[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值