iText操作Word工具类

package com.loongtao.report.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
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.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.field.RtfPageNumber;
import com.lowagie.text.rtf.field.RtfTotalPageNumber;
import com.lowagie.text.rtf.style.RtfParagraphStyle;

/**
 * iText导出Word工具类
 * 
 * @author Mr.gao
 *
 */
public class WordUtil {
	
	
	
	public enum ExportPlan{
		One,TWO
	}
	
	/**
	 * Word中常用标题级别
	 * @author Mr.gao
	 *
	 */
	public enum FontLevel{
		ONE,TWO,THREE
	}
	
	/**
	 * 导出Word
	 * @param doc
	 * @param path
	 * @throws FileNotFoundException
	 */
	public void exportWord(Document doc,String path) throws FileNotFoundException{
		
		FileOutputStream fos = new FileOutputStream(path);
		
		RtfWriter2.getInstance(doc, fos);
		
	}
	
	/**
	 * 设置Word页眉或者页脚内容及对齐方式
	 * 
	 * @param headerContext
	 * 			页眉/页脚信息
	 * @param alignment
	 * 			对齐方式,例如:Rectangle.ALIGN_CENTER(居中)
	 * @return HeaderFooter
	 */
	public HeaderFooter setHeaderFooter(String context,int alignment){
		
		HeaderFooter hf = new HeaderFooter(new Phrase(context),false);
		
		hf.setAlignment(alignment);
		
		return hf;
	}
	
	/**
	 * 设置Word中标题级别,查看FontLevel
	 * @param level
	 * 			1-3级
	 * @return RtfParagraphStyle
	 */
	public RtfParagraphStyle setFontLevel(FontLevel level,String fontName){
		
		RtfParagraphStyle r1 = null;
		switch (level) {
		case ONE:
			r1 = RtfParagraphStyle.STYLE_HEADING_1;
			r1.setStyle(Font.BOLD);
			r1.setSpacingAfter(5);
			break;
		case TWO:
			r1 = RtfParagraphStyle.STYLE_HEADING_2;
			r1.setStyle(Font.NORMAL);
			r1.setSpacingBefore(15);
			r1.setSpacingAfter(5);	
			
			break;
		case THREE:
			r1 = RtfParagraphStyle.STYLE_HEADING_3;
			r1.setStyle(Font.NORMAL);
			r1.setIndentLeft(25);
			r1.setSpacingAfter(15);
			break;
		}
		r1.setFontName(fontName);
		return r1;
		
	}
	
	/**
	 * 设置Word基础字体
	 * @param bf
	 * 			BaseFont
	 * @param size
	 * 			字号
	 * @param fontStyle
	 * 			字体样式,例如:com.lowagie.text.Font.BOLD
	 * @return com.lowagie.text.Font
	 */
	public Font setFont(BaseFont bf,int size,int fontStyle){
		
		Font f = new Font(bf,size,fontStyle);
		
		return f;
	}
	
	public Paragraph setPageNumber(){
		RtfPageNumber number = new RtfPageNumber();
		Paragraph parafooter = new Paragraph();
		parafooter.add(new Phrase("第"));
		parafooter.add(number);
		parafooter.add(new Phrase("页 共"));
		parafooter.add(new RtfTotalPageNumber());
		parafooter.add(new Phrase("页"));
		
		return parafooter;
	}
	
	/**
	 * 在Word中创建段落及格式设置
	 * 
	 * 注意:<1>如果设置为标题,则将paragraphIdent,rowIndent设置为0即可,所创见的段落如果是一行,那么缩进量=paragraphIdent+rowIdent。
	 * 
	 * 		<2>其中paragraphIdent为段落缩进,指整个段落的所尽量,而rowIdent为第一行的缩进量
	 * 
	 * 		<3>建议缩进量加起来范围在【0f-500f】,一般情况建议范围【30f】为两个汉字的缩进量。
	 * 
	 * 		<4>超出500f则文字继续向后对齐方式的反方向推移,超出Word纸张所显示的范围。
	 * 
	 * @param context
	 * 			页面内容
	 * @param font
	 * 			字体
	 * @param alignment
	 * 			对齐方式,例如:Element.ALIGN_CENTER
	 * @param paragraphIdent
	 * 			设置段落缩进量
	 * @param rowIdent
	 * 			设置行缩进量
	 * @param spacingBefore
	 * 			段前距离,例如:50f
	 * @param spacingAfter
	 * 			段后距离
	 * 
	 * @return Paragraph
	 */
	public Paragraph createParagraph(String context,Font font,int alignment,float paragraphIndent,float rowIndent,float spacingBefore,float spacingAfter){
		
		Paragraph pg = new Paragraph(context,font);
		pg.setAlignment(alignment);
		pg.setIndentationLeft(paragraphIndent);  
        pg.setFirstLineIndent(rowIndent); 
		pg.setSpacingBefore(spacingBefore);
        pg.setSpacingAfter(spacingAfter);  
        
		return pg;
	}
	
	/**
	 * 添加指定路径图片
	 * 
	 * 如需通过网络,或者其他方式添加,查看Image.getInstance();
	 * @param filePath
	 * 			图片路径
	 * @param x
	 * 			长度
	 * @param y
	 * 			宽度
	 * @param alignment
	 * 			对其方式,例如:Image.MIDDLE
	 * @return
	 * @throws IOException 
	 * @throws MalformedURLException 
	 * @throws BadElementException 
	 */
	public Image addImage(String filePath,float x,float y,int alignment) throws BadElementException, MalformedURLException, IOException{
		
        Image image = Image.getInstance(filePath);  

        image.scaleAbsolute(x, y);  
        
        image.setAlignment(alignment); 
        
        return image;
	}
	
	/**
	 * 创建表格
	 * @param row
	 * 			行
	 * @param column
	 * 			列
	 * @param alignment
	 * 			对齐方式,例如:Table.ALIGN_CENTER
	 * @param widths
	 * 			每个列的宽度,列数组长度根据所建列数相同。例如:3行2列,那么数组长度为2;6行8列,那么数组长度为8
	 * @return Table
	 * @throws DocumentException
	 */
	public Table createTable(int row,int column,int alignment,int []widths) throws DocumentException{
		
        Table table = new Table(column,row);  
        
        table.setBorderWidth(1f);  
        // table.setAbsWidth("120px");  
        table.setAlignment(alignment);  
        
        table.setWidths(widths);  
        
        return table;
	}
	
	/**
	 * 创建列
	 * @param e
	 * 			Word中的Element元素,例如:图片、Table等
	 * @param headerFlag
	 * 			标题头部开关
	 * @param rowSpan
	 * 			如开启,则需写跨行,否则可以写0
	 * @param colSpan
	 * 			如开启,则需写跨列
	 * @return
	 * @throws BadElementException
	 */
	public Cell createCell(Element e,boolean headerFlag,int rowSpan,int colSpan) throws BadElementException{
		
        Cell cell = new Cell(e);  
        
        cell.setBorder(0);  
        if(headerFlag){
        	 cell.setHeader(true);  
             cell.setColspan(rowSpan);
             cell.setRowspan(colSpan);
        }
        
        return cell;
	}
	
	/**
	 * 创建列
	 * @param context
	 * 			列中填充的内容
	 * @param headerFlag
	 * 			标题头部开关
	 * @param rowSpan
	 * 			如开启,则需写跨行,否则可以写0
	 * @param colSpan
	 * 			如开启,则需写跨列,否则可以写0
	 * @return
	 * @throws BadElementException
	 */
	public Cell createCell(String context,boolean headerFlag,int rowSpan,int colSpan) throws BadElementException{
		
        Cell cell = new Cell(context);  
        
        cell.setBorder(1);
        
        if(headerFlag){
        	 cell.setHeader(true);  
             cell.setColspan(colSpan);
             cell.setRowspan(rowSpan);
        }
        
        return cell;
	}
	
	public static void main(String[] args) throws DocumentException, IOException, com.lowagie.text.DocumentException{
		String filePath = "E:\\iText导出Word.doc";
		exportWord(filePath);
	}
	public static void exportWord(String filePath) throws DocumentException, IOException, com.lowagie.text.DocumentException{
		
		//创建Document实例
		Document document = new Document(PageSize.A4);
		
		WordUtil ew = new WordUtil();
		
		ew.exportWord(document,filePath);
		//打开文档
		document.open();
		
		document.setHeader(ew.setHeaderFooter("这是页眉", Rectangle.ALIGN_CENTER));
		
		document.setFooter(ew.setHeaderFooter("这是页脚",Rectangle.ALIGN_CENTER));
		
		//设置基础字体
		Font baseFont = ew.setFont(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,BaseFont.EMBEDDED), 24, Font.BOLD);
		
//		BaseFont baseFont = BaseFont.createFont();
//		Font contextFont = new Font(baseFont,12,Font.NORMAL);
//		Font titleFont = new Font(baseFont,24,Font.BOLD);
//		Font font = new Font(baseFont,15,Font.BOLD);
		
		document.add(ew.createParagraph("Itext", baseFont, Element.ALIGN_CENTER, 0f, 0f, 100f, 100f));
		document.add(ew.createParagraph("这是新增的Paragraph", baseFont, Element.ALIGN_LEFT, 100f, 100f, 0f, 0f));
		
		//正文内容
		document.newPage();
		document.add(ew.createParagraph("一、iText简介",  ew.setFontLevel(FontLevel.ONE,"宋体"),Element.ALIGN_CENTER,200f, 200f, 100f, 100f));
		document.newPage();
		
		Paragraph p1 = ew.createParagraph("iText是著名的开放源码的站点sourceforge一个项目,是用于生产PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、HTML文件转化为PDF文件。" , ew.setFont(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,BaseFont.EMBEDDED), 14, Font.NORMAL),Element.ALIGN_CENTER,30f,0f,0f,0f);
		//p1.setLeading(25f);
		document.add(p1);
		
		Table table = ew.createTable(6, 8, Table.ALIGN_CENTER, new int[]{50,50,50,50,50,50,50,50});
		
		table.addCell(ew.createCell("8", true, 0, 8));
		
		table.endHeaders();
		
		table.addCell(ew.createCell("这是第二个表格", false, 0, 0));
		
		table.addCell(ew.createCell("这是第三个表格", false, 0, 0));
		
		table.addCell(ew.createCell("这是第四个表格", false, 0, 0));
		
		document.add(table);
		
		document.close();

        
		
	}
}


转载于:https://my.oschina.net/u/1163293/blog/202627

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值