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)有标题
[img]http://dl2.iteye.com/upload/attachment/0118/1145/2c2b8c27-35aa-3e3f-a803-a571385e346f.png[/img]
2)无标题

[img]http://dl2.iteye.com/upload/attachment/0118/1147/8120fb9a-6813-3e35-b43e-d1c1f74c683a.png[/img]

如有什么疑问请留言!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用itextpdf类在pdf指定位置写入字符串的示例代码: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import java.io.FileOutputStream; import java.io.IOException; public class PdfWriterExample { public static void main(String[] args) { try { // 读取原始PDF文件 PdfReader reader = new PdfReader("input.pdf"); // 创建输出PDF文件 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf")); // 获取指定页面的内容流 PdfContentByte content = stamper.getOverContent(1); // 设置字体 BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font font = new Font(baseFont, 12, Font.NORMAL); // 在指定位置添加文本 content.beginText(); content.setFontAndSize(baseFont, 12); content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Hello, World!", 100, 100, 0); content.endText(); // 关闭PDF文件 stamper.close(); reader.close(); } catch (IOException | DocumentException e) { e.printStackTrace(); } } } ``` 这段代码使用itextpdf库来读取原始的PDF文件,并在指定位置添加文本。首先,我们创建一个PdfReader对象来读取原始PDF文件。然后,我们创建一个PdfStamper对象来写入输出PDF文件。通过调用`getOverContent()`方法,我们可以获取指定页面的内容流。接下来,我们设置字体并使用`showTextAligned()`方法在指定位置添加文本。最后,我们关闭PDF文件。 请注意,这只是一个示例代码,你需要根据你的实际需求进行适当的修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值