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;
/**
* PCell Event
* @author donald
*
*/
public class CheckboxCellEvent implements PdfPCellEvent{
// The name of the check box field
protected String name;
protected boolean flag ;
private Integer hookSize = 5;
private Integer edgeSize = 10;
// We create a cell event
public CheckboxCellEvent(String name, boolean flag) {
this.name = name;
this.flag = flag;
}
public CheckboxCellEvent(String name, boolean flag , Integer hookSize, Integer edgeSize ) {
this.name = name;
this.flag = flag;
this.hookSize = hookSize;
this.edgeSize = edgeSize;
}
// 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 - hookSize, y - hookSize, x + hookSize, y + hookSize);
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 - edgeSize, y - edgeSize, edgeSize+edgeSize,edgeSize+edgeSize);
canvas.stroke();

try {
writer.addAnnotation(checkbox.getCheckField());
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}

2.工具类:
package util;

import java.util.List;
import java.util.Map;

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 volatile PdfPTableUtils instance = null;
public static synchronized PdfPTableUtils getInstance(){
if(null==instance){
instance = new PdfPTableUtils();
}
return instance;
}
/**
*
* @param font 字体
* @param columnwidth 列宽
* @param checkValues 复选框值
* @param checkeds check状态
* @param hookSize √ size
* @param edgeSize checkBox size
* @return
*/
public PdfPCell getCheckTable(Font font,int[] columnwidth, String[] checkValues, List<Boolean> checkeds, Integer hookSize, Integer edgeSize){
return getCheckTable(font,columnwidth, null, null, checkValues, checkeds,true, hookSize, edgeSize);
}
/**
*
* @param font 字体
* @param columnwidth 列宽
* @param checkValues 复选框值
* @param checkeds check状态
* @param isBorder 是否有边框
* @param hookSize √ size
* @param edgeSize checkBox size
* @return
*/
public PdfPCell getCheckTable(Font font,int[] columnwidth, String[] checkValues, List<Boolean> checkeds,Boolean isBorder, Integer hookSize, Integer edgeSize){
return getCheckTable(font,columnwidth, null, null, checkValues, checkeds,isBorder, hookSize, edgeSize);
}
/**
*
* @param font 字体
* @param columnwidth 列宽
* @param minColumnHeight 列最小高度
* @param checkValues 复选框值
* @param checkeds check状态
* @param isBorder 是否有边框
* @return
*/
public PdfPCell getCheckTable(Font font,int[] columnwidth, Integer minColumnHeight, String[] checkValues, List<Boolean> checkeds,Boolean isBorder){
return getCheckTable(font,columnwidth, minColumnHeight, null, checkValues, checkeds, isBorder, null, null);
}
/**
*
* @param font 字体
* @param columnwidth 列宽
* @param minColumnHeight 列最小高度
* @param checkValues 复选框值
* @param checkeds check状态
* @param isBorder 是否有边框
* @param hookSize √ size
* @param edgeSize checkBox size
* @return
*/
public PdfPCell getCheckTable(Font font,int[] columnwidth, Integer minColumnHeight, String[] checkValues, List<Boolean> checkeds,Boolean isBorder,Integer hookSize, Integer edgeSize){
return getCheckTable(font,columnwidth, minColumnHeight, null, checkValues, checkeds, isBorder, hookSize, edgeSize);
}
/**
*
* @param font 字体
* @param columnwidth 列宽
* @param minColumnHeight 列最小高度
* @param title 标题
* @param checkValues 复选框值
* @param checkeds check状态
* @param isBorder 是否有边框
* @return
*/
public PdfPCell getCheckTable(Font font,int[] columnwidth, Integer minColumnHeight, String title, String[] checkValues, List<Boolean> checkeds,Boolean isBorder){
return getCheckTable(font,columnwidth, minColumnHeight, title, checkValues, checkeds, isBorder, null, null);
}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and checkbox size
* @param minColumnHeight 列最小高度
* @param title 标题
* @param checkValues 复选框值
* @param checkeds check状态
* @param isBorder 是否有边框
* @param hookSize √ size
* @param edgeSize checkBox size
* @return
*/

public PdfPCell getCheckTable(Font font,int[] columnwidth, Integer minColumnHeight, String title, String[] checkValues, List<Boolean> checkeds,Boolean isBorder, Integer hookSize, Integer edgeSize){
PdfPCell checkCell =null;
PdfPTable table = new PdfPTable(columnwidth.length);
try {
table.setWidths(columnwidth);
} catch (DocumentException e) {
e.printStackTrace();
}
if(!isBorder){
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);
if(!isBorder){
head.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
head.setMinimumHeight(minColumnHeight);
}
table.addCell(head);
}
//checkBox
int i=0;
for(String checkValue: checkValues){
//checkBox
PdfPCell checkBox = new PdfPCell();
if(null != hookSize && null != edgeSize){
checkBox.setCellEvent(new CheckboxCellEvent(checkValue, checkeds.get(i) , hookSize, edgeSize));
}
else{
checkBox.setCellEvent(new CheckboxCellEvent(checkValue, checkeds.get(i)));
}
checkBox.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
checkBox.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
if(!isBorder){
checkBox.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
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);
if(!isBorder){
checkDescr.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
checkDescr.setMinimumHeight(minColumnHeight);
}
table.addCell(checkDescr);
i++;
}
checkCell = new PdfPCell(table);
return checkCell;

}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and checkbox values size
* @param checkValues 复选框值
* @param checkeds check状态
* @param hookSize √ size
* @param edgeSize checkBox size
* @param cellDescr List cell
* @param cellValue List cell value
* @return
*/
public PdfPCell getCheckCell(Font font,int[] columnwidth, String[] checkValues, List<Boolean> checkeds, Integer hookSize, Integer edgeSize, List<String> cellDescr, List<String> cellValue){
return getCheckCell( font,columnwidth, null, null, checkValues, checkeds, true, hookSize, edgeSize, cellDescr, cellValue);
}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and checkbox values size
* @param checkValues 复选框值
* @param checkeds check状态
* @param isBorder 是否有边框
* @param hookSize √ size
* @param edgeSize checkBox size
* @param cellDescr List cell
* @param cellValue List cell value
* @return
*/
public PdfPCell getCheckCell(Font font,int[] columnwidth, String[] checkValues, List<Boolean> checkeds,Boolean isBorder, Integer hookSize, Integer edgeSize, List<String> cellDescr, List<String> cellValue){
return getCheckCell( font,columnwidth, null, null, checkValues, checkeds, isBorder, hookSize, edgeSize, cellDescr, cellValue);
}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and checkbox values size
* @param minColumnHeight 列最小高度
* @param checkValues 复选框值
* @param checkeds check状态
* @param isBorder 是否有边框
* @param cellDescr List cell
* @param cellValue List cell value
* @return
*/
public PdfPCell getCheckCell(Font font,int[] columnwidth, Integer minColumnHeight, String[] checkValues, List<Boolean> checkeds,Boolean isBorder, List<String> cellDescr, List<String> cellValue){
return getCheckCell( font,columnwidth, minColumnHeight, null, checkValues, checkeds, isBorder, null, null, cellDescr, cellValue);
}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and checkbox values size
* @param minColumnHeight 列最小高度
* @param checkValues 复选框值
* @param checkeds check状态
* @param isBorder 是否有边框
* @param hookSize √ size
* @param edgeSize checkBox size
* @param cellDescr List cell
* @param cellValue List cell value
* @return
*/
public PdfPCell getCheckCell(Font font,int[] columnwidth, Integer minColumnHeight, String[] checkValues, List<Boolean> checkeds,Boolean isBorder, Integer hookSize, Integer edgeSize, List<String> cellDescr, List<String> cellValue){
return getCheckCell( font,columnwidth, minColumnHeight, null, checkValues, checkeds, isBorder, hookSize, edgeSize, cellDescr, cellValue);
}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and checkbox values size
* @param minColumnHeight 列最小高度
* @param title 标题
* @param checkValues 复选框值
* @param checkeds check状态
* @param isBorder 是否有边框
* @param hookSize √ size
* @param edgeSize checkBox size
* @param cellDescr List cell
* @param cellValue List cell value
* @return
*/
public PdfPCell getCheckCell(Font font,int[] columnwidth, Integer minColumnHeight, String title, String[] checkValues, List<Boolean> checkeds,Boolean isBorder, Integer hookSize, Integer edgeSize, List<String> cellDescr, List<String> cellValue){
PdfPCell checkCell =null;
PdfPTable table = new PdfPTable(columnwidth.length);
try {
table.setWidths(columnwidth);
} catch (DocumentException e) {
e.printStackTrace();
}
if(!isBorder){
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);
if(!isBorder){
head.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
head.setMinimumHeight(minColumnHeight);
}
table.addCell(head);
}
//checkBox
int i=0;
for(String checkValue: checkValues){
//checkBox
PdfPCell checkBox = new PdfPCell();
if(null != hookSize && null != edgeSize){
checkBox.setCellEvent(new CheckboxCellEvent(checkValue, checkeds.get(i) , hookSize, edgeSize));
}
else{
checkBox.setCellEvent(new CheckboxCellEvent(checkValue, checkeds.get(i)));
}
checkBox.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
checkBox.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
if(!isBorder){
checkBox.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
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);
if(!isBorder){
checkDescr.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
checkDescr.setMinimumHeight(minColumnHeight);
}
table.addCell(checkDescr);
i++;
}
//values
i=0;
for(String desc : cellDescr){
PdfPCell dcell = new PdfPCell(new Phrase(desc,font));
dcell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
dcell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
if(!isBorder){
dcell.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
dcell.setMinimumHeight(minColumnHeight);
}
table.addCell(dcell);
PdfPCell vcell = new PdfPCell(new Phrase(cellValue.get(i),font));
vcell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
vcell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
if(!isBorder){
vcell.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
vcell.setMinimumHeight(minColumnHeight);
}
table.addCell(vcell);
i++;
}
checkCell = new PdfPCell(table);
return checkCell;

}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and values size
* @param cellDescr List cell
* @param cellValue List cell value
* @return
*/
public PdfPCell getCells(Font font,int[] columnwidth, List<String> cellDescr, List<String> cellValue){
return getCells( font,columnwidth, null, null, true, cellDescr, cellValue);
}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and values size
* @param minColumnHeight 列最小高度
* @param cellDescr List cell
* @param cellValue List cell value
* @return
*/
public PdfPCell getCells(Font font,int[] columnwidth, Integer minColumnHeigh,List<String> cellDescr, List<String> cellValue){
return getCells( font,columnwidth, minColumnHeigh, null, true, cellDescr, cellValue);
}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and values size
* @param minColumnHeight 列最小高度
* @param isBorder 是否有边框
* @param cellDescr List cell
* @param cellValue List cell value
* @return
*/
public PdfPCell getCells(Font font,int[] columnwidth, Integer minColumnHeight,Boolean isBorder, List<String> cellDescr, List<String> cellValue){
return getCells( font,columnwidth, minColumnHeight, null, isBorder, cellDescr, cellValue);
}
/**
*
* @param font 字体
* @param columnwidth 列宽 title and values size
* @param minColumnHeight 列最小高度
* @param title 标题
* @param isBorder 是否有边框
* @param cellDescr List cell
* @param cellValue List cell value
* @return
*/
public PdfPCell getCells(Font font,int[] columnwidth, Integer minColumnHeight, String title,Boolean isBorder, List<String> cellDescr, List<String> cellValue){
PdfPCell checkCell =null;
PdfPTable table = new PdfPTable(columnwidth.length);
try {
table.setWidths(columnwidth);
} catch (DocumentException e) {
e.printStackTrace();
}
if(!isBorder){
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);
if(!isBorder){
head.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
head.setMinimumHeight(minColumnHeight);
}
table.addCell(head);
}
//values
int i=0;
for(String desc : cellDescr){
PdfPCell dcell = new PdfPCell(new Phrase(desc,font));
dcell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
dcell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
if(!isBorder){
dcell.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
dcell.setMinimumHeight(minColumnHeight);
}
table.addCell(dcell);
PdfPCell vcell = new PdfPCell(new Phrase(cellValue.get(i),font));
vcell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
vcell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
if(!isBorder){
vcell.setBorder(PdfPCell.NO_BORDER);
}
if(null != minColumnHeight){
vcell.setMinimumHeight(minColumnHeight);
}
table.addCell(vcell);
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 util.PdfPTableUtils;

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[] columnWidthxx = new int[]{60,30,60,30,60,30,60,30,60};
int[] columnWidthxxx = new int[]{30,60,30,60,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);
List<String> cellDescr = new ArrayList<String>();
cellDescr.add("金额:");
cellDescr.add("日期:");
List<String> cellValue = new ArrayList<String>();
cellValue.add("2000.00");
cellValue.add("2016-06-20");
//有title
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckTable(font,columnWidth,columnHeight,title,checkValues,checkeds,false,6,11);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckTable(font,columnWidth,columnHeight,title,checkValues,checkeds,false);
//无title
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckTable(font,columnWidthx,columnHeight,null,checkValues,checkeds,false);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckTable(font,columnWidthx,checkValues,checkeds,3,6);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckTable(font,columnWidthx,checkValues,checkeds,false,3,6);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckCell(font,columnWidthxx,columnHeight,title,checkValues,checkeds,false,6,11,cellDescr,cellValue);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckCell(font,columnWidthxxx,columnHeight,checkValues,checkeds,false,6,11,cellDescr,cellValue);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckCell(font,columnWidthxxx,columnHeight,checkValues,checkeds,false,cellDescr,cellValue);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckCell(font,columnWidthxxx,columnHeight,checkValues,checkeds,false,cellDescr,cellValue);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckCell(font,columnWidthxxx,checkValues,checkeds,false,3,6,cellDescr,cellValue);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCheckCell(font,columnWidthxxx,checkValues,checkeds,3,6,cellDescr,cellValue);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCells(font,columnWidth,columnHeight,title,false,cellDescr,cellValue);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCells(font,columnWidthx,columnHeight,false,cellDescr,cellValue);
// PdfPCell checkbox = PdfPTableUtils.getInstance().getCells(font,columnWidthx,columnHeight,cellDescr,cellValue);
PdfPCell checkbox = PdfPTableUtils.getInstance().getCells(font,columnWidthx,cellDescr,cellValue);
table.addCell(checkbox);
document.add(table);
document.close();
System.out.println("===========:end");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值