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("1、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(); } }