itextsharp(一)Chunk、Phrase、Paragraph的应用——PDF

一,Chunk : 块,PDF文档中描述的最小原子元素
       Phrase : 短语,Chunk的集合
       Paragraph : 段落,一个有序的Phrase集合
二,
实例一:

package lession3;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.pdf.PdfWriter;
public class FirstMain {
public static void main(String[] args) throws Exception {
   Document doc = new Document();
   PdfWriter.getInstance(doc , new FileOutputStream("f:\\target.pdf"));
   doc.open();
 
   //定义一个块
   Chunk chunk = new Chunk("Cat");
   //设置块的背景色
   chunk.setBackground(Color.blue);
 
   //字体
   Font font = FontFactory.getFont(FontFactory.TIMES_BOLD);
   font.setColor(Color.white);
   chunk.setFont(font);
 
   //增加块到文档
   doc.add(chunk);
 
   chunk = new Chunk("DOG");
   doc.add(chunk);
   doc.close();
}
}

 

实例二:

package lession3;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class FirstMain {
public static void main(String[] args) throws Exception {
   Document doc = new Document();
   PdfWriter.getInstance(doc , new FileOutputStream("f:\\target.pdf"));
   doc.open();
 
   //建块
   Chunk chunk1 = new Chunk("Cat");
   Chunk chunk2 = new Chunk("DOG");
 
   //建短语
   Phrase phrase = new Phrase();
   phrase.add(chunk1);
   phrase.add(chunk2);
   phrase.add("Hello world");
 
   doc.add(phrase);
 
   //新建一行
   doc.add(Chunk.NEWLINE); 
   doc.add(new Chunk("new line"));
 
   doc.close();
}
}

实例三
package lession3;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class FirstMain {
public static void main(String[] args) throws Exception {
   Document doc = new Document();
   PdfWriter.getInstance(doc , new FileOutputStream("f:\\target.pdf"));
   doc.open();
 
   //建块
   Chunk chunk1 = new Chunk("Cat");
   Chunk chunk2 = new Chunk("DOG");
 
   //建短语
   Phrase phrase = new Phrase();
   phrase.add(chunk1);
   phrase.add(chunk2);
   phrase.add("Hello world");
 
   //建段落
   Paragraph paragraph = new Paragraph();
   paragraph.add(phrase);
   paragraph.add("Hello World");
 
   //设置段落对齐方式
   paragraph.setAlignment(Element.ALIGN_LEFT);
   //设置缩进
   paragraph.setIndentationLeft(100f);
 
   Paragraph paragraph1 = new Paragraph();
   paragraph1.add("AA");
 
   //注意增加段落时会自动换行
   doc.add(paragraph);
   doc.add(paragraph1);
 
   doc.close();
}
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值