IText中有三个处理text的类com.lowagie.text.Chunk,com.lowagie.text.Phrase,com.lowagie.text.Paragraph。
它们之间有什么区别呢?
Chunk
Chunk是IText最小的文本编辑单位。不能再拆分更小的单元。其他高级的文本对象都是基于Chunk的。请看下图
(注此图,来源自http://www.cnblogs.com/LifelongLearning/archive/2011/03/30/2000072.html)
- package org.study.itext.text;
- import java.awt.Color;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.lowagie.text.Chunk;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Font;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.pdf.PdfWriter;
- /**
- * @blog http://reymont.iteye.com/
- * @author reymont.li
- * @version create time:2011-7-14 上午11:49:01
- */
- public class ChunkTest {
- public static void main(String[] args) {
- Document document = new Document();
- try {
- PdfWriter.getInstance(document, new FileOutputStream(
- "resource/ChunkTest.pdf"));
- document.open();
- BaseFont songbfChinese = BaseFont.createFont("resource/STSONG.TTF",
- BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font songFont = new Font(songbfChinese, 18, Font.UNDERLINE);
- Chunk love = new Chunk("我们的爱", songFont);
- love.setTextRise(10);
- love.setBackground(Color.CYAN);
- BaseFont xingkabfChinese = BaseFont.createFont(
- "resource/STXINGKA.TTF", BaseFont.IDENTITY_H,
- BaseFont.EMBEDDED);
- Font xingkaFont = new Font(xingkabfChinese, 18, Font.ITALIC);
- Chunk simple = new Chunk("简单", xingkaFont);
- simple.setBackground(Color.BLUE);
- simple.setTextRise(-10);
- BaseFont kaibfChinese = BaseFont.createFont("resource/STKAITI.TTF",
- BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font kaiFont = new Font(kaibfChinese, 18, Font.BOLD);
- Chunk song = new Chunk("如歌", kaiFont);
- song.setBackground(Color.RED);
- document.add(love);
- document.add(simple);
- document.add(song);
- } catch (DocumentException de) {
- System.err.println(de.getMessage());
- } catch (IOException ioe) {
- System.err.println(ioe.getMessage());
- }
- document.close();
- }
- }
生成的文本为
在水平方向,Chunk的字符满一行,就会从头开始。请注意,这是从头开始,而不是另起一行。对于Chunk来说,行间距默认为0,那么当文档中只有Chunk时,这些字符永远只会出现再第一行。
- package org.study.itext.text;
- import java.awt.Color;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.lowagie.text.Chunk;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Font;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.pdf.PdfWriter;
- /**
- * @blog http://reymont.iteye.com/
- * @author reymont.li
- * @version create time:2011-7-14 上午11:49:14
- */
- public class ChunkTest2 {
- public static void main(String[] args) {
- Document document = new Document();
- try {
- PdfWriter.getInstance(document, new FileOutputStream(
- "resource/ChunkTest2.pdf"));
- document.open();
- BaseFont songbfChinese = BaseFont.createFont("resource/STSONG.TTF",
- BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font songFont = new Font(songbfChinese, 18, Font.UNDERLINE);
- Chunk love = new Chunk("我们的爱", songFont);
- love.setTextRise(10);
- love.setBackground(Color.CYAN);
- BaseFont xingkabfChinese = BaseFont.createFont(
- "resource/STXINGKA.TTF", BaseFont.IDENTITY_H,
- BaseFont.EMBEDDED);
- Font xingkaFont = new Font(xingkabfChinese, 18, Font.ITALIC);
- Chunk simple = new Chunk("简单", xingkaFont);
- simple.setBackground(Color.BLUE);
- simple.setTextRise(-10);
- BaseFont kaibfChinese = BaseFont.createFont("resource/STKAITI.TTF",
- BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font kaiFont = new Font(kaibfChinese, 18, Font.BOLD);
- Chunk song = new Chunk("如歌", kaiFont);
- song.setBackground(Color.RED);
- for (int i = 0; i < 10; i++) {
- document.add(love);
- document.add(simple);
- document.add(song);
- }
- } catch (DocumentException de) {
- System.err.println(de.getMessage());
- } catch (IOException ioe) {
- System.err.println(ioe.getMessage());
- }
- document.close();
- }
- }
生成的文档为:
可以看到后面的字符串覆盖了前面的字符串。怎么才能解决这个问题呢?
Phrase
我们可以将Chunk当成固定的块,Phrase当成由Chunk组成的字符串。让我们做一些小改动。
- package org.study.itext.text;
- import java.awt.Color;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.lowagie.text.Chunk;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Font;
- import com.lowagie.text.Phrase;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.pdf.PdfWriter;
- /**
- * @blog http://reymont.iteye.com/
- * @author reymont.li
- * @version create time:2011-7-14 上午11:49:24
- */
- public class PhraseTest {
- public static void main(String[] args) {
- Document document = new Document();
- try {
- PdfWriter.getInstance(document, new FileOutputStream(
- "resource/PhraseTest.pdf"));
- document.open();
- BaseFont songbfChinese = BaseFont.createFont("resource/STSONG.TTF",
- BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font songFont = new Font(songbfChinese, 18, Font.UNDERLINE);
- Chunk love = new Chunk("我们的爱", songFont);
- love.setTextRise(10);
- love.setBackground(Color.CYAN);
- BaseFont xingkabfChinese = BaseFont.createFont(
- "resource/STXINGKA.TTF", BaseFont.IDENTITY_H,
- BaseFont.EMBEDDED);
- Font xingkaFont = new Font(xingkabfChinese, 18, Font.ITALIC);
- Chunk simple = new Chunk("简单", xingkaFont);
- simple.setBackground(Color.BLUE);
- simple.setTextRise(-10);
- BaseFont kaibfChinese = BaseFont.createFont("resource/STKAITI.TTF",
- BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font kaiFont = new Font(kaibfChinese, 18, Font.BOLD);
- Chunk song = new Chunk("如歌", kaiFont);
- song.setBackground(Color.RED);
- Phrase phrase = new Phrase(30);
- phrase.add(love);
- phrase.add(simple);
- phrase.add(song);
- for (int i = 0; i < 10; i++)
- document.add(phrase);
- document.add(Chunk.NEWLINE);
- document.add(phrase);
- phrase.add("\n");
- for (int i = 0; i < 3; i++) {
- document.add(phrase);
- }
- } catch (DocumentException de) {
- System.err.println(de.getMessage());
- } catch (IOException ioe) {
- System.err.println(ioe.getMessage());
- }
- document.close();
- }
- }
Document添加了phrase。这样,Phrase就会自动换行。
还有两种方式也可以换行 。
document.add(Chunk.NEWLINE);
和
phrase.add("\n");
貌似这样就解决了字符串换行的问题,但这些都要写一些额外的代码,有什么类封装这些动作呢?
Paragraph
Paragraph继承自Phase。
- package org.study.itext.text;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.lowagie.text.Chunk;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Element;
- import com.lowagie.text.Font;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.Phrase;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.pdf.PdfWriter;
- /**
- * @blog http://reymont.iteye.com/
- * @author reymont.li
- * @version create time:2011-7-14 上午11:49:18
- */
- public class ParagraphTest {
- public static void main(String[] args) {
- Document document = new Document();
- try {
- PdfWriter.getInstance(
- document,
- new FileOutputStream("resource/ParagraphTest.pdf"));
- document.open();
- String text = "我们的爱简单如歌";
- Phrase phrase1 = new Phrase(text);
- Phrase phrase2 = new Phrase(new Chunk(text, new Font(
- Font.TIMES_ROMAN)));
- BaseFont songbfChinese = BaseFont.createFont("resource/STSONG.TTF",
- BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font font = new Font(songbfChinese, 18, Font.NORMAL);
- Phrase phrase3 = new Phrase(text, font);
- Paragraph paragraph = new Paragraph();
- paragraph.add(phrase1);
- paragraph.add(phrase2);
- paragraph.add(phrase3);
- document.add(paragraph);
- document.add(paragraph);
- paragraph.setAlignment(Element.ALIGN_LEFT);
- document.add(paragraph);
- paragraph.setAlignment(Element.ALIGN_CENTER);
- document.add(paragraph);
- paragraph.setAlignment(Element.ALIGN_RIGHT);
- document.add(paragraph);
- paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
- document.add(paragraph);
- paragraph.setSpacingBefore(10);
- document.add(paragraph);
- paragraph.setSpacingBefore(0);
- paragraph.setSpacingAfter(10);
- document.add(paragraph);
- paragraph.setIndentationLeft(20);
- document.add(paragraph);
- paragraph.setIndentationRight(20);
- document.add(paragraph);
- } catch (DocumentException de) {
- System.err.println(de.getMessage());
- } catch (IOException ioe) {
- System.err.println(ioe.getMessage());
- }
- document.close();
- }
- }
每段文字上面和下面的空间,可使用setSpacingBefore() 和setSpacingAfter() 来指定。
每段文字的缩进可使用setIndentationLeft()和setIndentationRight()。
以下为个人模仿:
package test;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.poi.hwpf.extractor.WordExtractor;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class Test {
public static void createDocContext(String file) throws DocumentException,
IOException {
// 设置纸张大小
Document document = new Document(PageSize.A4.rotate());
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document, new FileOutputStream(file));
document.open();
// 设置中文字体
// BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
// "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 标题字体风格
Font titleFont = new Font(bfChinese, 26, Font.BOLD);
//标签字体样式
Font labelFont = new Font(bfChinese,15,Font.BOLD);
// 正文字体风格
Font contextFont = new Font(bfChinese, 14, Font.NORMAL);
Font contextBoldFont = new Font(bfChinese, 14, Font.BOLD);
// 设置标题格式对齐方式
Paragraph title = new Paragraph("江苏中烟工业系统月度快报说明",titleFont);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 设置日期
Paragraph title2 = new Paragraph("(二Ο一四年五月)",new Font(bfChinese,16,Font.BOLD));
title2.setAlignment(Element.ALIGN_CENTER);
document.add(title2);
document.add(new Paragraph(""));
//标签1
Paragraph label = new Paragraph(" 一、全系统实现税利情况",labelFont);
label.setLeading(30);
label.setAlignment(Element.ALIGN_LEFT);
document.add(label);
Chunk _label = new Chunk(" 工业公司本级:",contextBoldFont);
Chunk _context = new Chunk("5月份实现税利35.75亿元,同比29.62亿元增长20.69%," +
"其中实现利润8.15亿元,同比7.52亿元上升8.39%。1-5月份累计实现税利215.77亿元, 同比197.84亿元增长9.06%," +
"其中累计实现利润44.77亿元,同比43.13亿元增长3.78%。",contextFont);
// Phrase _l_c = new Phrase();
Paragraph _l_c = new Paragraph();
_l_c.setLeading(30);
_l_c.add(_label);
_l_c.add(_context);
document.add(_l_c);
_label = new Chunk(" 全系统:",contextBoldFont);
_context = new Chunk("5月份实现税利35.98亿元,同比29.75亿元增长20.94%,其中实现利润8.29亿元,同比7.6亿元增长9.05%。1-5月份累计实现税利216.37亿元," +
"同比198.54亿元增长8.98%,其中实现利润45.27亿元,同比43.69亿元增长3.63%。",contextFont);
_l_c = new Paragraph();
_l_c.setLeading(30);
_l_c.add(_label);
_l_c.add(_context);
document.add(_l_c);
//标签1
label = new Paragraph(" 二、卷烟工业企业产量、销量及实现税利情况",labelFont);
label.setLeading(30);
label.setAlignment(Element.ALIGN_LEFT);
document.add(label);
_l_c = new Paragraph(" 1、5月份生产卷烟17.5万箱,同比15.56万箱增加1.9万箱,增长12.44%。1-5月份累计生产卷烟89.08万箱,同比85.56万箱增加3.5万箱,增长4.1%。",contextFont);
_l_c.setLeading(30);
document.add(_l_c);
_l_c = new Paragraph(" 2、5月份自产卷烟销售17.55万箱,同比15.83万箱增加1.72万箱,增长10.86%。1-5月份累计自产卷烟销售94.68万箱,同比90.87万箱增加3.81万箱,增长4.19%。",contextFont);
_l_c.setLeading(30);
document.add(_l_c);
_l_c = new Paragraph(" 3、5月份自产卷烟销售额38.52亿元,同比32.64亿元增长18.01%。1-5月份累计自产卷烟销售额233.21亿元,同比212.77亿元增长9.6%。",contextFont);
_l_c.setLeading(30);
document.add(_l_c);
_l_c = new Paragraph(" 4、5月份自产卷烟销售成本7.88亿元,同比6.49亿元增长21.52%。1-5月份累计自产卷烟销售成本43.29亿元,同比38.35亿元增长12.87%。",contextFont);
_l_c.setLeading(30);
document.add(_l_c);
_l_c = new Paragraph(" 4、5月份自产卷烟销售成本7.88亿元,同比6.49亿元增长21.52%。1-5月份累计自产卷烟销售成本43.29亿元,同比38.35亿元增长12.87%。",contextFont);
_l_c.setLeading(30);
document.add(_l_c);
_l_c = new Paragraph(" 6、1-5月份卷烟累计税利增加17.93亿元,其中销量增加3.81万箱,增加税利8.29亿元,单箱税利结构提升1017元,增加税利9.64亿元。",contextFont);
_l_c.setLeading(30);
document.add(_l_c);
label = new Paragraph(" 三、工业公司本级成本费用情况",labelFont);
label.setLeading(30);
label.setAlignment(Element.ALIGN_LEFT);
document.add(label);
_l_c = new Paragraph();
_l_c.setLeading(30);
_context = new Chunk(" 1-5月份累计期间费用61628万元,同比62377万元下降1.2%。其中:",contextFont);
_label = new Chunk("营业费用",contextBoldFont);
_l_c.add(_context);
_l_c.add(_label);
_context = new Chunk("21357万元,同比18877万元上升13.14%,",contextFont);
_label = new Chunk("管理费用",contextBoldFont);
_l_c.add(_context);
_l_c.add(_label);
_context = new Chunk("56992万元,同比61123万元下降6.76%。综合两大费用来看总体减少1651万,下降2.06%。其中由于工资费用增长1600万,实则其他费用的下降影响在3200多万。重点费用的使用情况:其中宣传促销费下降70%,车辆运行费用下降30%,业务招待费下降82.29%,本月由于派驻阿根廷人员差费报销,涉外费用是上升的。",contextFont);
_l_c.add(_context);
document.add(_l_c);
document.close();
}
public static void main(String[] args) {
String file = "d:/demo1.doc";
try {
createDocContext(file);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}