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.jar
  • STSONG.TTF在C:\WINDOWS\Fonts中

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值