iText中复杂布局表格单元格的创建

package com.bi.wms.fpn.util;

import java.awt.Color;
import java.io.IOException;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;

public class PdfUtil {

 /**
  * 创建文字
  * @param content内容
  * @param font字体
  * @param align对齐方式
  * @return
  */
   public static Paragraph newParagraph(String content,Font font, int align){
  Paragraph paragraph=new Paragraph(content, font);
  paragraph.setAlignment(align);
  return paragraph;
 }
 
 /**
  * 创建单元格(内容为图片)
  * @param bgColor 背景
  * @param border 边框
  * @param align 对齐方式
  * @param colspan 所占列数
  * @param obj  内容(文字或图片对象)
  * @return
  */
   public static PdfPCell newPdfPCell(Color bgColor,int border,int align,int colspan,Image image){
  PdfPCell cell=new PdfPCell();
  cell.setBorderColor(Color.BLACK);
  cell.setBorderColorLeft(Color.BLACK);
  cell.setBorderColorRight(Color.BLACK);
  cell.setBackgroundColor(bgColor);
  cell.setBorder(border);
  cell.setHorizontalAlignment(align);
  cell.setColspan(colspan);
  cell.addElement(image);
  return cell;
 }
 
 /**
  * 创建单元格(内容为文字)
  * @param bgColor
  * @param border
  * @param align
  * @param colspan
  * @param paragraph
  * @return
  */
   public static PdfPCell newPdfPCell(Color bgColor,int border,int align,int colspan,Paragraph paragraph){
  PdfPCell cell=new PdfPCell();
  cell.setBorderColor(Color.BLACK);
  cell.setBorderColorLeft(Color.BLACK);
  cell.setBorderColorRight(Color.BLACK);
  cell.setBackgroundColor(bgColor);
  cell.setBorder(border);
  cell.setHorizontalAlignment(align);
  cell.setColspan(colspan);
  cell.addElement(paragraph);
  return cell;
 }
 /**
  * 创建字体
  * @param size 大小
  * @param font 字体
  * @return
  * @throws Exception
  */
   public static Font newFont(int size, int font){
  BaseFont bfChinese=null;
  try {
   bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  } catch (DocumentException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return new Font(bfChinese,size,font);
 }
 /**
  * 创建字体
  * @param size大小
  * @param font字体
  * @param color颜色
  * @return
  * @throws Exception
  */
   public static Font newFont(int size, int font,Color color){
  Font f= newFont(size,font);
  f.setColor(color);
  return f;
 }
 
 /**
  * 创建图片
  * @param imgPath 图片路径
  * @param width 宽
  * @param height 高
  * @param align 对齐方式
  * @return
  */
   public static Image newImage(String imgPath, int width, int height,int align){
    Image img=null;
    try{
    img= Image.getInstance(imgPath);
    img.scaleAbsolute(width, height);
          img.setAlignment(align);
    }catch (Exception e) {
   
    e.printStackTrace();
    }
    return img;
 }

  /**
   * 创建一个跨多行的单元格
   * @param rows 所占行数
   * @param bgColor 背景色
   * @param paragraph 单元格内容文字
   * @param align 对齐方式
   */
   public static  PdfPCell newPdfPCellByRows(int rows,Color bgColor,Paragraph paragraph,int align){
    PdfPTable iTable=new PdfPTable(1);
    PdfPCell iCell=new PdfPCell();
    iCell.setFixedHeight(iCell.getFixedHeight()*rows);
    iTable.addCell(iCell);
    iCell.setBackgroundColor(bgColor);
    iCell.addElement(paragraph);
    iCell.setHorizontalAlignment(align);
    PdfPCell cell=new PdfPCell(iTable);
    return cell;
   }
  
   /**
    * 创建一个跨多列的单元格
    * @param colspan 所占列数
    * @param bgColor 背景色
    * @param paragraph 单元格内容文字
    * @param align 对齐方式
    */
    public static  PdfPCell newPdfPCellByColspan(int colspan,Color bgColor,Paragraph paragraph,int align){
     PdfPTable iTable=new PdfPTable(1);
     PdfPCell iCell=new PdfPCell();
     iCell.setColspan(colspan);
     iCell.setBackgroundColor(bgColor);
     iCell.setBorder(0);
     iCell.addElement(paragraph);
     iCell.setHorizontalAlignment(align);
     iTable.addCell(iCell);
     PdfPCell cell=new PdfPCell(iTable);
     return cell;
    }

 
}

猜你喜欢

转载自premier9527.iteye.com/blog/1573234