java itext生成word

  项目中需要生成word文档,由于最初做过excel的导入导出,使用的是apache的poi,本打算继续使用该组件,但是网上的相关资料实在太少,上官网看了下,发现poi更擅长的是处理execl,对于word一般只有读入却很有有写出的功能,而且api不完善,据官网介绍是开发word这一块的程序员走了,所以这一块也就烂掉了。只好另寻他策,于是就找到了itext。

  itext主要是用来处理pdf的,可以生成rtf格式的文档,而word本身支持rtf。

  所需jar:itext-2.0.7.jar(itext的核心jar)、iTextAsian.jar(用于支持中文)

  以下是我做的小例子:

  1 package com.lincoln.word;
  2 
  3 import java.awt.Color;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 
  7 import com.lowagie.text.Chunk;
  8 import com.lowagie.text.Document;
  9 import com.lowagie.text.DocumentException;
 10 import com.lowagie.text.Element;
 11 import com.lowagie.text.Font;
 12 import com.lowagie.text.FontFactory;
 13 import com.lowagie.text.PageSize;
 14 import com.lowagie.text.Paragraph;
 15 import com.lowagie.text.pdf.BaseFont;
 16 import com.lowagie.text.rtf.RtfWriter2;
 17 
 18 public class CreateWordUtil {
 19     public void createDocContext(String file) throws DocumentException,IOException 
 20     {
 21         String nextLine = "\n";//使用System.getProperty("line.separator");总生成乱码,不知为何
 22         
 23         // 设置纸张大小
 24         Document document = new Document(PageSize.A4);
 25         // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
 26         RtfWriter2.getInstance(document, new FileOutputStream(file));
 27         document.open();
 28         // 设置中文字体
 29         BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
 30         // 标题字体风格
 31         Font titleFont = new Font(bfChinese, 15, Font.BOLD);
 32         Paragraph title = new Paragraph("公司名称变更函");
 33         // 设置标题格式对齐方式
 34         title.setAlignment(Element.ALIGN_CENTER);
 35         title.setFont(titleFont);
 36         document.add(title);
 37         
 38         //正文
 39         Font contentFont = new Font(bfChinese, 12, Font.NORMAL);
 40         Font uderLineFont = FontFactory.getFont(FontFactory.COURIER, 12, Font.UNDERLINE,new Color(0, 0, 0)) ;
 41         Chunk longSpace = new Chunk("                         ", uderLineFont);
 42         Chunk shortSapce = new Chunk("      ",uderLineFont);
 43         
 44         Paragraph pp = new Paragraph(nextLine + nextLine + "致:上海****有限公司" + nextLine,contentFont);
 45         document.add( pp  );        
 46         
 47         Chunk paragraph1 = new Chunk( "    经上海市" , contentFont );
 48         document.add( paragraph1  );        
 49         document.add( longSpace );
 50         
 51         
 52         Chunk paragraph2 = new Chunk( "工商行政管理局核准,自" );
 53         document.add( paragraph2 );
 54         
 55         document.add( shortSapce );
 56         Chunk paragraph3 = new Chunk( "年" );
 57         document.add( paragraph3 );
 58         document.add( shortSapce );
 59         Chunk paragraph4 = new Chunk( "月" );
 60         document.add( paragraph4 );
 61         document.add( shortSapce );
 62         Chunk paragraph5 = new Chunk( "日" );
 63         document.add( paragraph5 );
 64         
 65         Chunk paragraph6 = new Chunk( "起,我公司名称已由" );
 66         document.add( paragraph6 );
 67         document.add( longSpace );
 68         
 69         Chunk paragraph7 = new Chunk( "变更为" );
 70         document.add( paragraph7 );
 71         document.add( longSpace );
 72         Chunk paragraph8 = new Chunk( "(见附《企业名称变更预先核准通知书》复印件)。"+nextLine+nextLine );
 73         document.add( paragraph8 );
 74         
 75         Chunk paragraph9 = new Chunk( "    我公司将继续全面履行与你单位于" );
 76         document.add( paragraph9 );
 77         document.add( shortSapce );
 78         document.add( paragraph3 );
 79         document.add( shortSapce );
 80         document.add( paragraph4 );
 81         document.add( shortSapce );
 82         document.add( paragraph5 );
 83                     
 84         Chunk paragraph10 = new Chunk( "签署的《海上国际集装箱运输电子数据交换协议》。" +nextLine+nextLine );
 85         document.add( paragraph10 );            
 86         
 87                     
 88         StringBuffer sb2 = new StringBuffer() ;
 89         sb2.append("    我公司的办公地址,联系方式与联系人具体为:"+nextLine+nextLine)
 90             .append("地址:"+nextLine)
 91             .append("邮编:"+nextLine)
 92             .append("电话:"+nextLine)
 93             .append("传真:"+nextLine)
 94             .append("联系人:"+nextLine+nextLine)
 95             .append("    特此函告。"+nextLine)
 96             .append("\t\t\t\t\t\t\t\t原公司名称公章:"+nextLine+nextLine+nextLine+nextLine)
 97             .append("\t\t\t\t\t\t\t\t新公司名称公章:"+nextLine+nextLine+nextLine+nextLine)
 98             .append("\t\t\t\t\t\t\t\t日期:       年        月       日");
 99         
100         Paragraph paragraph11 = new Paragraph( sb2.toString() );
101         document.add( paragraph11 );            
102         document.close();
103     }
104 
105     /**
106      * @param args
107      */
108     public static void main(String[] args) {
109         CreateWordUtil word = new CreateWordUtil();
110         String file = "e:/easipass.doc";
111         try {
112             System.out.println("it is start ...");
113             word.createDocContext(file);
114             System.out.println("it is OK!");
115         } catch (DocumentException e) {
116             e.printStackTrace();
117         } catch (IOException e) {
118             e.printStackTrace();
119         }
120 
121     }
122 }
View Code

 

刚翻了下书,查看了下spring的源码,发现spring中的生成pdf的功能实际上就是集成了iText。而spring中生成execl实际上就是集成了POI。

posted @ 2013-07-15 16:37  lincolnPei  阅读(825)  评论(0编辑  收藏  举报