起因:
在生成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
分享到:
相关推荐
使用IText生成PDF和WORD文档 包括源码和IText所需jar包
java导出word文档,使用itext包,可以生成pdf,word,生成段落,插入图图片,页码
java操作pdf和rtf文件所需的jar包。包含: itext-2.1.7.jar itext-rtf-2.1.7.jar
itext-rtf-2.1.7.jar itext-rtf-2.1.7.pom itext-rtf-2.1.7.pom.sha1
itext-rtf-2.1.7.jar,iTextAsian.jar,iText-5.0.6.jar,itext-2.1.7.jar
itext2.1.7.jar itext-rtf-2.1.7.jar
itext-rtf-2.1.4.jar是用itext导出WORD格式报表的扩展包
iText1.2.7、iText-rtf2.1.7、iTextAsian
itext 操作PDF需要的jar包
文档POI只支持往生成的中填入文本,对于图片根本就不支持。使用itext 生成rtf格式的直接保存为word
iText-2.1.3 iTextAsian iText-rtf-2.1.3 linux下生成DOC word文档 完美工具
里面包含两个demo实例,分别是itext-pdf生成pdf的,一个是itext-rtf生成word的。其中还附有学习笔记一份,上述生成的文档包括对字体的选择(本地或者iTextAsian里面的),有对图片的添加,有对样式的调整,有对table...
iText-2.1.7.jar+iTextAsian.jar+iTextAsianCmaps.jar+itext-rtf-2.1.7.jar.zip
java 用iText生成Word文件 java 用iText生成Word文件
这是一个使用iText生成Word的一个实例,实例中有字体的设置,A4纸的纵横显示的控制,表格的生成(Table),多页的控制等。里面有详细的注释,这也是我在项目开发中使用到的几个类。
java 使用iText生成word文档,包中带有源码与所用jar包,生成文档格式标准。可分页,可带页眉,可生成表格、图片和文字等word元素内容,文字带字号,字体,颜色等。
iText-2.1.5.jar,iTextAsian.jar,iText-rtf-2.1.4.jar及execl生成代码实现
itext7 html转换为pdf;iText7页码、页眉、页脚,itext 的复杂表格实现;完整springboot项目代码
NULL 博文链接:https://housen1987.iteye.com/blog/1335036
itext导出word必需的包。是itext-2.1.7的扩展包