在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已经满足大部分需求,但是如果我们想要一个分成几段来显示文本,可以使用
phrase.add("\n");
也可以通过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);
}
}
}
引用
http://www.geek-tutorials.com/java/itext/insert_control_text.php
分享到:
相关推荐
itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框.itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框
itext2.17
IText 和 ITextAsian 的开发包
多个版本的itext-jar包, itext-2.0.4/itext-2.1.5/itext-2.1.7
adobe pdf编辑器,解压安装即可使用,配合java利用itextpdf根据模板导出pdf文章中只做模板的方法;
itext 生成pdf 目录,itext 生成pdf 目录,itext 生成pdf 目录
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,...
Itext5.5.13 完整的全套包,包含bcpkix-jdk15on-1.47.jar bcprov-jdk15on-1.59.jar itext-asian-5.2.0.jar itext-hyph-xml-5.1.1.jar itext-pdfa-5.5.13-javadoc.jar itext-pdfa-5.5.13-sources.jar itext-pdfa-...
itext中文: public static void main(String[] args) { try { Document document = new Document(); OutputStream os = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\test.pdf")); ...
itext 操作PDF需要的jar包
iText1.2.7、iText-rtf2.1.7、iTextAsian
IText7jar集合
iText-5.0.6.jar iTextAsian.jar
itext 生成pdf的中文帮助文档,具体有:图片、块(下划线/删除线、上标/下标)、短句、段落等。
iText7jar和英文文档,以及itext5的一些文档。建议大家去官网看itext7,改动有点大。我也在摸索中。
赠送jar包:itext-2.1.7.jar; 赠送原API文档:itext-2.1.7-javadoc.jar; 赠送源代码:itext-2.1.7-sources.jar; 赠送Maven依赖信息文件:itext-2.1.7.pom; 包含翻译后的API文档:itext-2.1.7-javadoc-API文档-...
itext 包,itext.jar,itext 下载,汉化itext 包,itext.jar,itext 下载,汉化
itext-1.4.8
itext-1.01.jar和iTextAsian.jar这两个包是支持 HTML、RTF 和 XML 文档的生成,此外还能够生成 PDF 文档
itext-rtf-2.1.7.jar,iTextAsian.jar,iText-5.0.6.jar,itext-2.1.7.jar