itext使用rtf格式生成word,使用image为页眉
来源:互联网 发布:淘宝购买处方药流程 编辑:程序博客网 时间:2024/06/08 16:04
起因:
在生成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 0
- itext使用rtf格式生成word,使用image为页眉
- 使用itext生成word格式的报表(by quqi99)
- doxygen生成rtf/word使用小心得
- Java使用iText生成word文 表格、图片、表格里插图片、页眉、页脚、图片页脚、这一次更全面
- 使用itext-rtl 生成word文档
- Java使用iText生成word文件的解决方案
- Java使用iText生成word文件的解决方案
- Java使用iText生成word文件的解决方案
- Java使用iText生成word文件的解决方案
- Java使用iText生成word文件的解决方案
- 使用itext-2.1.7生成word文档总结
- Java使用iText生成word文件的完美解决方案
- 生成pdf,使用itext
- 使用itext生成pdf
- 使用iText生成doc
- itext生成页眉页脚
- 使用itext导出word文档
- word存为txt文本、rtf格式的文件
- itext设置默认NO_BORDER表格
- cannot find class for bean with name的一种解决记录
- css设置图片逐渐消失
- itext pdftemplate使用
- oracle split_for_array 用法示例
- itext使用rtf格式生成word,使用image为页眉
- jquery tree循环渲染数据,控制checkbox显示与隐藏,点击无效效果
- 创建itext document&横向打印
- 第三章:3.6 使用 Session
- 抽象工厂方法
- 设置word07标题样式
- Android自定义权限与动态申请权限
- RACI模型在IT项目管理中应用
- Cannot find any registered HttpDestinationFactory from the Bus