`
reymont
  • 浏览: 526169 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

itext使用rtf格式生成word,使用image为页眉

    博客分类:
  • PDF
阅读更多

起因:

 

在生成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();
	}
}

 相关资源:

 

 

 

 

 

 

  • 大小: 17.8 KB
  • 大小: 4.1 KB
  • 大小: 14.1 KB
分享到:
评论
5 楼 kisbo110 2013-07-03  
找不到 jar包啊,我看现在是itext-pdf 啊
4 楼 reymont 2012-06-07  
这个还要要对ByteArrayOutputStream 做些处理。ByteArrayOutputStream 主要作用是在内存中创建缓冲区,数据将被写入byte数组。而FileOutputStream主要作用是将数据写入文件或者一个FileDescriptor,也可以通过FileOutputStream 来包含image的数据。可以看到这个两个OutputStream处理数据时不同的,可以利用如下形式将ByteArrayOutputStream转换为FileOutputStream:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = bos.toByteArray();
File someFile = new File("java2.doc");
FileOutputStream fos = new FileOutputStream(someFile);
fos.write(bytes);
3 楼 liujian85 2012-06-06  
对的  但是我在尝试的时候发现  生成的word文件就会发生乱码了 感觉是图片放进流以后  再导出到word已经不再被识别  不知道有没有方法解决啊?
2 楼 reymont 2012-06-06  
查看其iText-2.0.8的API,第二个参数是OutputStream。而ByteArrayOutputStream extends OutputStream,所以这样做是可以的

public static RtfWriter2 getInstance(Document doc, OutputStream os)
1 楼 liujian85 2012-06-05  
你好,请问一下,当我RtfWriter2的第二个参数使用的是ByteArrayOutputStream时,页眉和页脚还能设置吗?

相关推荐

Global site tag (gtag.js) - Google Analytics