itext使用rtf格式生成word,使用image为页眉
起因:
?
在生成word文档时,一般都要求生成固定的页眉。页眉,即可以是文字也可以图片。对于程序员来说,图片可以简化开发的复杂度,对于用户来说可以丰富页眉的样式。于是我尝试使用itext生成rtf格式来获得包含图片页眉的word文档。
?
尝试1:直接使用document.add(Image),可以看到图片在文本内,就算使用Image.setAbsolutePosition(),图片的位置仍然没有变化。
?
?
尝试2:使用new HeaderFooter(Phrase,false)。貌似可以达到预期效果,但是你一看代码,就知道这是以代码的复杂性为代价的,而且会额外的多处两个回车符。
?
?
尝试3:使用com.lowagie.text.rtf.headerfooter.RtfHeaderFooter。终于达到预期效果了。
?
?
?
?
package org.study.itext.rtf;import java.awt.Color;import java.io.FileOutputStream;import com.lowagie.text.Document;import com.lowagie.text.Font;import com.lowagie.text.HeaderFooter;import com.lowagie.text.Image;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.rtf.RtfWriter2;import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;/** * @blog http://reymont.iteye.com/ * @author reymont.li * @version create time:2011-7-21 下午04:02:59 */public class RtfWithImageHeader {public static void main(String[] args) {test1();test2();test3();}public static void test1(){Document document1 = new Document(PageSize.A4, 36, 36, 100, 36);try {RtfWriter2.getInstance(document1,new FileOutputStream("resource/RtfWithImageHeader1.doc"));Image headerImage = Image.getInstance("resource/reymont.png");BaseFont bfChinese = BaseFont.createFont("resource/STSONG.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);document1.open();document1.add(headerImage);Font topFont = new Font(bfChinese, 24, Font.NORMAL, Color.RED);Paragraph para = new Paragraph("这是个将图片作为Word页眉的例子!",topFont);document1.add(para);} catch (Exception e) {e.printStackTrace();} document1.close();}public static void test2(){Document document2 = new Document(PageSize.A4, 36, 36, 100, 36);try {RtfWriter2 writer = RtfWriter2.getInstance(document2,new FileOutputStream("resource/RtfWithImageHeader2.doc"));BaseFont bfChinese = BaseFont.createFont("resource/STSONG.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);document2.open();Font topFont1 = new Font(bfChinese, 10, Font.NORMAL, Color.BLUE);Paragraph headerPara1 = new Paragraph("@blog http://reymont.iteye.com/", topFont1);Paragraph headerPara2 = new Paragraph("@author reymont.li", topFont1);Paragraph headerPara3 = new Paragraph("@MSN reymont.li@hotmail.com", topFont1);Paragraph headerPara = new Paragraph();headerPara.add(headerPara1);headerPara.add(headerPara2);headerPara.add(headerPara3);HeaderFooter header = new HeaderFooter(headerPara,false);writer.setHeader(header);document2.add(header);Font topFont2 = new Font(bfChinese, 24, Font.NORMAL, Color.RED);Paragraph para = new Paragraph("这是个将图片作为Word页眉的例子!",topFont2);document2.add(para);} catch (Exception e) {e.printStackTrace();} document2.close();}public static void test3(){Document document3 = new Document(PageSize.A4, 36, 36, 100, 36);try {RtfWriter2 writer = RtfWriter2.getInstance(document3,new FileOutputStream("resource/RtfWithImageHeader3.doc"));document3.open();Image headerImage = Image.getInstance("resource/reymont.png");RtfHeaderFooter header = new RtfHeaderFooter(headerImage);writer.setHeader(header);BaseFont bfChinese = BaseFont.createFont("resource/STSONG.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Font topFont = new Font(bfChinese, 24, Font.NORMAL, Color.RED);Paragraph para = new Paragraph("这是个将图片作为Word页眉的例子!",topFont);document3.add(para);} catch (Exception e) {e.printStackTrace();} document3.close();}}?相关资源:itext-2.0.8.jarSTSONG.TTF在C:\WINDOWS\Fonts中
?
?
?
?
?
?
1 楼 liujian85 2012-06-05 你好,请问一下,当我RtfWriter2的第二个参数使用的是ByteArrayOutputStream时,页眉和页脚还能设置吗? 2 楼 reymont 2012-06-06 查看其iText-2.0.8的API,第二个参数是OutputStream。而ByteArrayOutputStream extends OutputStream,所以这样做是可以的