Posted on 2010-04-21 15:00
landor 阅读(3262)
评论(2) 编辑 收藏 所属分类:
导出与图表
用itext生成word,参考了以下两篇文章
生成简单的word图片
http://javacrazyer.javaeye.com/blog/623719
生成word同时流到客户端
http://www.cnblogs.com/modou/articles/1291024.html
图片想穿插在文字之间,而不是换行:
Image png = Image.getInstance(this.realPath+imgPath);
png.setAbsolutePosition(0, 0);
png.setAlignment(Image.TEXTWRAP);//Image.TEXTWRAP(图片包裹在文字中)、 Image.UNDERLYING(作为文字的背景)
context.add(new Phrase(new Chunk(png, 0, 0, true)));//Chunk区块,Phrase(词语)
举个例子:
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, new FileOutputStream(path));
document.open();
// 设置中文字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
Paragraph context = new Paragraph("");
context.setFont(contextFont);
Image png = Image.getInstance("D:/busy.gif");
png.setAbsolutePosition(0, 0);
png.setAlignment(Image.TEXTWRAP);
context.add("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
context.add(new Phrase(new Chunk(png, 0, 0, true)));
context.add("bbbbbbbbbbbbbbbbbbbbbbbbbbb");
context.add(new Phrase(new Chunk(png, 0, 0, true)));
context.add("ccccccccccccccccccccccccccc");
document.add(context);
document.close();