iText制作PDFCheckbox-2

本文介绍如何使用iText库在PDF文件中创建CheckBox,并通过不同的样式和状态展示。利用PdfPCellEvent事件类来实现自定义单元格,通过RadioCheckField设置CheckBox的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PDF中checkBox的制作,需要依赖于PdfPCellEvent事件类,实例如下:
package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
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 CheckboxCell2 {
public static final String DEST = "E:\\checkbox_in_cell2.pdf";

public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CheckboxCell2().createPdf(DEST);
}

public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
// We create a table with 6 columns
PdfPTable table = new PdfPTable(6);
table.setWidths(new int[]{50,50,50,50,50,50});
PdfPCell cell;
// We add 6 cells
for (int i = 0; i < 6; i++) {
cell = new PdfPCell();
cell.setCellEvent(new CheckboxCellEvent("cb" + i, i));
// We create cell with height 50
cell.setMinimumHeight(50);
table.addCell(cell);
}
document.add(table);
document.close();
}

class CheckboxCellEvent implements PdfPCellEvent {
// The name of the check box field
protected String name;
protected int i;
// We create a cell event
public CheckboxCellEvent(String name, int i) {
this.name = name;
this.i = i;
}
// 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 - 10, y - 10, x + 10, y + 10);
// define the check box
RadioCheckField checkbox = null;
if(i%2==0){
//On,Yes,True都可以,表示选中,首选On
/*checkbox = new RadioCheckField(
writer, rect, name, "True");*/
/* checkbox = new RadioCheckField(
writer, rect, name, "Yes");*/
checkbox = new RadioCheckField(
writer, rect, name, "On");
}
else
{
checkbox = new RadioCheckField(
writer, rect, name, "Off");
}
switch(i) {
case 0:
checkbox.setCheckType(RadioCheckField.TYPE_CHECK);
break;
case 1:
checkbox.setCheckType(RadioCheckField.TYPE_CIRCLE);
break;
case 2:
checkbox.setCheckType(RadioCheckField.TYPE_CROSS);
break;
case 3:
checkbox.setCheckType(RadioCheckField.TYPE_DIAMOND);
break;
case 4:
checkbox.setCheckType(RadioCheckField.TYPE_SQUARE);
break;
case 5:
checkbox.setCheckType(RadioCheckField.TYPE_STAR);
break;
}
checkbox.setChecked(true);
// add the check box as a field
try {
writer.addAnnotation(checkbox.getCheckField());
System.out.println("===========:end");
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
}

结果如下:
[img]http://dl2.iteye.com/upload/attachment/0118/0972/f1fe4305-6e55-3e65-9183-6f86e214d926.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值