IText中有三个处理text的类com.lowagie.text.Chunk,com.lowagie.text.Phrase,com.lowagie.text.Paragraph。
它们之间有什么区别呢?
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(); } }
生成的文档为:
可以看到后面的字符串覆盖了前面的字符串。怎么才能解决这个问题呢?
我们可以将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继承自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()。