读书人

完整的iText生成PdfPTable资料并且转

发布时间: 2012-12-22 12:05:05 作者: rapoo

完整的iText生成PdfPTable文件,并且转化成图片类

import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; /** * 功能:生成pdf表格 、转化成图片 * * @author Shizw * @CreateTime 2011-12-08 */ public class CreatePdfToImage { /** * 中文字体(默认黑体) * 具体字体可到C:\\WINDOWS\FONTS下去拷 */ public String character_font_ch = "simhei.TTF"; // 黑体 /** * 左边距 */ public float margin_left = 2;; /** * 右边距 */ public float margin_right = 2; /** * 上边距 */ public float margin_top = 8; /** * 下边距 */ public float margin_bottom = 2; /** * 字体显示(水平或垂直)默认水平显示 */ public String identity = BaseFont.IDENTITY_H; /** * 字体大小(默认16) */ public int size = 16; /** * 字体样式(默认NORMAL) */ public int style = Font.NORMAL; /** * 字体颜色(默认黑色) */ public BaseColor color = BaseColor.BLACK; /** * 设置表格所占pdf宽度的比例 默认100% */ public float widthPercentage = 100; /** * 表格的单元格高度(默认28) */ public float table_height = 28; /** * 单元格内容与左边距间隔(默认0) */ public float padding_left = 0; /** * pdftable */ private PdfPTable pdfTable; /** * ColorList */ private List colorList = new ArrayList();// 设置字体颜色 /** * 设置字体位置List */ private List backList = new ArrayList(); /** * 设置字体align */ public int align = Element.ALIGN_CENTER; /** * 默认行背景色 */ public BaseColor back_color = BaseColor.WHITE; /** * 行背景色list */ private List backgroundList = new ArrayList(); /** * 自定义背景颜色 */ private BaseColor back_color_def; /** * 自定义列字体位置 */ private int align_def; private Document document = null; /** * 生成pdf并且生成图片 * * @param fileName *            生成的.pdf文件路径及名称 * @param path *            图片生成路径及名称(jpg 或png) * @param width *            图片宽度 * @param rowHeight *            行高 * @param cell_width *            列及列的宽度比例 * @param content *            结果集 * */ public void createTabToImage(String fileName, String path, float width, float rowHeight, float[] cell_width, String[][] content) { try { int rownum = content.length; this.table_height=rowHeight; Rectangle rec = createHeWidth(width,rowHeight,rownum); createDocument(fileName,rec); BaseFont f = createChineseFont(); Font font = createFont(f, this.size, this.style, this.color); this.pdfTable = createTable(cell_width); PdfPCell cell; Font fonts = null; for (int i = 0; i < content.length; i++) { // 自定义行颜色 for (int t = 0; t < backgroundList.size(); t++) { Map m = (Map) backgroundList.get(t); int row = ((Integer) m.get("row")).intValue(); BaseColor back_color = (BaseColor) m.get("color"); if (row == (i + 1)) { back_color_def = back_color; break; } else { back_color_def=this.back_color; } } // 自定义列的字体位置 for (int j = 0; j < content[i].length; j++) { for (int c = 0; c < backList.size(); c++) { Map m = (Map) backList.get(c); int cols = ((Integer) m.get("cols")).intValue(); int align = ((Integer) m.get("align")).intValue(); if (cols == (j + 1)) { align_def = align; break; } else { align_def=this.align ; } } if (content[i].length < cell_width.length) {// 列数不足时,自动把最后一个cell // 合并单元格 if (j == content[i].length - 1) { // 自定义字体的颜色 for (int k = 0; k < colorList.size(); k++) { Map m = (Map) colorList.get(k); int row = ((Integer) m.get("row")).intValue(); int cols = ((Integer) m.get("cols")).intValue(); BaseColor colors = (BaseColor) m.get("color"); if ((i + 1) == row && (j + 1) == cols) { fonts = new Font(f, this.size, this.style, colors); break; } else { fonts = font; } } cell = addToTable(content[i][j], fonts); cell.setColspan(cell_width.length - content[i].length + 1); this.pdfTable.addCell(cell); } else { for (int k = 0; k < colorList.size(); k++) { Map m = (Map) colorList.get(k); int row = ((Integer) m.get("row")).intValue(); int cols = ((Integer) m.get("cols")).intValue(); BaseColor colors = (BaseColor) m.get("color"); if ((i + 1) == row && (j + 1) == cols) { fonts = new Font(f, this.size, this.style, colors); break; } else { fonts = font; } } cell = addToTable(content[i][j], fonts); this.pdfTable.addCell(cell); } } else { for (int k = 0; k < colorList.size(); k++) { Map m = (Map) colorList.get(k); int row = ((Integer) m.get("row")).intValue(); int cols = ((Integer) m.get("cols")).intValue(); BaseColor colors = (BaseColor) m.get("color"); if ((i + 1) == row && (j + 1) == cols) { fonts = new Font(f, this.size, this.style, colors); break; } else { fonts = font; } } cell = addToTable(content[i][j], fonts); this.pdfTable.addCell(cell); } } } addTable(this.pdfTable); closeDocument(); setup(fileName, path); } catch (Exception ex) { ex.printStackTrace(); } } /** * 自定义字体颜色3 * * @param row *            行 * @param cols *            列 * @param color *            颜色 */ public void setCellStyle(int row, int cols, BaseColor color) { try { Map m = new HashMap(); m.put("row", Integer.valueOf(row)); m.put("cols", Integer.valueOf(cols)); m.put("color", color); colorList.add(m); } catch (Exception ex) { ex.printStackTrace(); } } /** * 设置单元格的对齐方式,只能指定列 * * @param cols * @param Element.ALIGN_CENTER,Element.ALIGN_LEFT *            等 */ public void setAlign(int cols, int align) { Map m = new HashMap(); m.put("cols", Integer.valueOf(cols)); m.put("align", Integer.valueOf(align)); backList.add(m); } /** * 设置背景色,只能指定行 * * @param row * @param color */ public void setBackgroundColor(int row, BaseColor color) { Map m = new HashMap(); m.put("row", Integer.valueOf(row)); m.put("color", color); backgroundList.add(m); } /** * 创建pdf文档 * * @param fileName *            pdf文件 * @param rectangle *            页面大小 */ private void createDocument(String fileName, Rectangle rectangle) { File file = new File(fileName); FileOutputStream out = null; document = new Document(rectangle, this.margin_left, this.margin_right, this.margin_top, this.margin_bottom); try { out = new FileOutputStream(file); PdfWriter.getInstance(document, out); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } document.open(); } /** * 创建pdf的宽度\高度 返回一个Rectangle ,createDocument时用 * * @param rowHeight *            行高 * @param rownum *            表格的行数 * @param width *            pdf宽度 * @return Rectangle */ private Rectangle createHeWidth(float width, float rowHeight, int rownum) { return new Rectangle(width, rowHeight * (rownum + 1)); } /** * 功能:返回支持中文的字体,字体样式等 * * @return Font */ private BaseFont createChineseFont() { BaseFont bfChinese = null; try { bfChinese = BaseFont.createFont(this.character_font_ch, this.identity, BaseFont.EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException io) { io.printStackTrace(); } return bfChinese; } /** * 创建字体样式 * * @param font * @param size *            大小 * @param style *            Font.NORMAL 等 * @param color *            颜色 * @return */ private Font createFont(BaseFont font, int size, int style, BaseColor color) { return new Font(font, size, style, color); } /** * 功能:返回支持中文的字体,字体样式等 * * @param size *            字体大小 * @param style *            字体 * @param font *            Font.NORMAL,Font.BOLD等 * @param color *            字体颜色 */ private Font createChineseFont(int size, int style, BaseColor color) { BaseFont bfChinese = null; Font font = null; try { bfChinese = BaseFont.createFont(this.character_font_ch, this.identity, BaseFont.EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException io) { io.printStackTrace(); } font = new Font(bfChinese, size, style, color); return font; } /** * 功能:创建一个空表格 * * @param cell_perct *            数组,设定列的数目,及每列所占表格比例 *            例:5列及比例cell_widths[]={0.4f,0.1f,0.15f,0.15f,0.15f}; */ private PdfPTable createTable(float[] cell_width) { // 生成一个表格 PdfPTable table = new PdfPTable(cell_width); table.setWidthPercentage(this.widthPercentage); return table; } /** * 创建单元格,添加到表格,默认字体居中,背景色白色 * */ private PdfPCell addToTable(String content, Font fonts) { PdfPCell cell; cell = new PdfPCell(new Paragraph(content, fonts)); cell.setFixedHeight(this.table_height); cell.setHorizontalAlignment(this.align_def); cell.setBackgroundColor(this.back_color_def); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPaddingLeft(this.padding_left); return cell; } /** * 把表格添加到pdf中 */ private void addTable(PdfPTable table) { try { if (document != null) { if(table!=null){ document.add(table); } } } catch (Exception ex) { ex.printStackTrace(); } } /** * 把表格添加到pdf中 */ private void addTable() { try { if (document != null) { if (this.pdfTable != null) { document.add(this.pdfTable); } } } catch (Exception ex) { ex.printStackTrace(); } } /** * 功能:关闭文档 */ private void closeDocument() { if (document != null) { document.close(); } } /** * 生成图片 * * @return */ private void setup(String fileName, String path) throws IOException { File file = new File(fileName); RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel .size()); PDFFile pdffile = new PDFFile(buf); for (int i = 1; i <= pdffile.getNumPages(); i++) { PDFPage page = pdffile.getPage(i); java.awt.Rectangle rect = new java.awt.Rectangle(0, 0, (int) page .getBBox().getWidth(), (int) page.getBBox().getHeight()); Image img = page.getImage(rect.width, rect.height, // width & // // height rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); BufferedImage tag = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height, null); FileOutputStream out; if(i>1){ System.out.println(path.substring(0,path.length()-4)); out = new FileOutputStream(path.substring(0,path.length()-4)+i+path.substring(path.length()-4)); }else{ out = new FileOutputStream(path); } JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); // JPEG out.close(); } } public float getMargin_left() { return margin_left; } /** * 设置左边距 * * @param margin_left */ public void setMargin_left(float margin_left) { this.margin_left = margin_left; } public float getMargin_right() { return margin_right; } /** * 设置右边距 * * @param margin_right */ public void setMargin_right(float margin_right) { this.margin_right = margin_right; } public float getMargin_top() { return margin_top; } /** * 设置上边距 * * @param margin_top */ public void setMargin_top(float margin_top) { this.margin_top = margin_top; } public float getMargin_bottom() { return margin_bottom; } public void setMargin_bottom(float margin_bottom) { this.margin_bottom = margin_bottom; } public String getCharacter_font_ch() { return character_font_ch; } /** * 设置中文字体(仿宋、黑体等) * * @param character_font_ch */ public void setCharacter_font_ch(String character_font_ch) { this.character_font_ch = character_font_ch; } public String getIdentity() { return identity; } /** * 字体显示(水平或垂直)BaseFont.IDENTITY_H 等 * * @param identity */ public void setIdentity(String identity) { this.identity = identity; } public int getSize() { return size; } /** * 设置字体大小 * * @param size */ public void setSize(int size) { this.size = size; } public int getStyle() { return style; } /** * 设置字体样式 * * @param style */ public void setStyle(int style) { this.style = style; } public BaseColor getColor() { return color; } /** * 设置字体颜色 * * @param color */ public void setColor(BaseColor color) { this.color = color; } public float getWidthPercentage() { return widthPercentage; } /** * 设置表格宽度 * * @param widthPercentage */ public void setWidthPercentage(float widthPercentage) { this.widthPercentage = widthPercentage; } public float getTable_height() { return table_height; } /** * 设置表格单元格的高度 * * @param table_height */ public void setTable_height(float table_height) { this.table_height = table_height; } public float getPadding_left() { return padding_left; } public void setPadding_left(float padding_left) { this.padding_left = padding_left; } public int getAlign() { return align; } public void setAlign(int align) { this.align = align; } public BaseColor getBack_color() { return back_color; } public void setBack_color(BaseColor back_color) { this.back_color = back_color; } }  

?

读书人网 >PowerDesigner

热点推荐