iText,PDF,Checkbox工具类

1.Cell事件类
package event;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RadioCheckField;

public class CheckboxCellEvent implements PdfPCellEvent{
	 // The name of the check box field
    protected String name;
    protected boolean flag ;
    // We create a cell event
    public CheckboxCellEvent(String name, boolean flag) {
        this.name = name;
        this.flag = flag;
    }
    // We create and add the check box field
    public void cellLayout(PdfPCell cell, Rectangle position,
        PdfContentByte[] canvases) {
        PdfWriter writer = canvases[0].getPdfWriter(); 
        // define the coordinates of the middle
        float x = (position.getLeft() + position.getRight()) / 2;
        float y = (position.getTop() + position.getBottom()) / 2;
        // define the position of a check box that measures 20 by 20
        //画勾
        Rectangle rect = new Rectangle(x - 5, y - 5, x + 5, y + 5);
        RadioCheckField checkbox = new RadioCheckField(writer, rect, name, "On");
        checkbox.setCheckType(RadioCheckField.TYPE_CHECK);
        
        if(flag){
        	//设置为选中状态
        	checkbox.setChecked(true);
        }
        else{
        	checkbox.setChecked(false);
        }
        //画框
        PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
        canvas.setColorStroke(BaseColor.BLACK);
        canvas.setLineDash(1f);
        canvas.rectangle(x - 10, y - 10, 20,20);
        canvas.stroke();
       
        try {
            writer.addAnnotation(checkbox.getCheckField());
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }
}

2.工具类:
package test;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import event.CheckboxCellEvent;

/**
 * checkbox tools
 * @author donald
 *
 */
public class PdfPTableUtils {
	private static PdfPTableUtils instance = null;
	public  static synchronized  PdfPTableUtils getInstance(){
		if(null==instance){
			instance = new PdfPTableUtils();
		}
		return instance;
	}
	/**
	 * 
	 * @param font 字体
	 * @param columnwidth 列宽
	 * @param minColumnHeight 列最小高度
	 * @param checkValues 复选框值
	 * @param checkeds check转台
	 * @return
	 */
	public  PdfPCell getPdfPTableNoBorder(Font font,int[] columnwidth, int minColumnHeight, String[] checkValues, List<Boolean> checkeds){
		return getPdfPTableNoBorderWithTitle(font,columnwidth, minColumnHeight, null, checkValues, checkeds);
	}
	/**
	 * 
	 * @param font 字体
	 * @param columnwidth 列宽
	 * @param minColumnHeight 列最小高度
	 * @param title 标题
	 * @param checkValues 复选框值
	 * @param checkeds check转台
	 * @return
	 */
	public  PdfPCell getPdfPTableNoBorderWithTitle(Font font,int[] columnwidth, int minColumnHeight, String title, String[] checkValues, List<Boolean> checkeds){
		PdfPCell checkCell =null;
		PdfPTable table = new PdfPTable(columnwidth.length);
        try {
        	table.setWidths(columnwidth);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        //设置标题
		if(!StringUtils.isBlank(title)){
			  PdfPCell head = new PdfPCell(new Phrase(title,font));
		        head.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
		        head.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		        head.setBorder(PdfPCell.NO_BORDER);
		        head.setMinimumHeight(minColumnHeight);
		        table.addCell(head);
		}
		 int i=0;
		 for(String checkValue: checkValues){
		      //checkBox
		      PdfPCell checkBox = new PdfPCell();
		      checkBox.setCellEvent(new CheckboxCellEvent(checkValue, checkeds.get(i)));
		      checkBox.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
		      checkBox.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		      checkBox.setBorder(PdfPCell.NO_BORDER);
		      checkBox.setMinimumHeight(minColumnHeight);
			  table.addCell(checkBox);
			  //checkBox Describle
			  PdfPCell checkDescr = new PdfPCell(new Phrase(checkValue,font));
			  checkDescr.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
			  checkDescr.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
			  checkDescr.setBorder(PdfPCell.NO_BORDER);
			  checkDescr.setMinimumHeight(minColumnHeight);
			  table.addCell(checkDescr);
			  i++;
		 }
		 checkCell = new PdfPCell(table);
		 return checkCell;
				  
	}
}

3.测试类
package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestpdfPCellUtils {
	public static final String DEST = "E:\\Check.pdf";
	 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TestpdfPCellUtils().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
  	  //定义字体
        BaseFont bfChinese = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false );
        Font font = new Font(bfChinese, 11, Font.NORMAL);
        PdfPTable table = new PdfPTable(1);
        //定义每列的宽度
        table.setWidths(new int[]{240});
        int[] columnWidth = new int[]{60,30,60,30,60};
        int[] columnWidthx = new int[]{30,60,30,60};
        int columnHeight = 30;
        String title = "状态";
        String[] checkValues = new String[]{"是","否"};
        List<Boolean> checkeds = new ArrayList<Boolean>();
        checkeds.add(true);
        checkeds.add(false);
        //有title
		PdfPCell checkbox = PdfPTableUtils.getInstance().getPdfPTableNoBorderWithTitle(font,columnWidth,columnHeight,title,checkValues,checkeds);
		//无title
//		PdfPCell checkbox = PdfPTableUtils.getInstance().getPdfPTableNoBorderWithTitle(font,columnWidthx,columnHeight,null,checkValues,checkeds);
        table.addCell(checkbox);
        document.add(table);
        document.close();
        System.out.println("===========:end");
    }
}

4.结果如下
1)有标题

2)无标题



如有什么疑问请留言!

你可能感兴趣的:(iText,PDF,Checkbox工具类)