前一段时间,我做的一个项目中要求用itext来填写pdf中的 formField 和 checkBox ,以及多个 pdf 文件合并成一个 pdf 文件。现在我把它整理出来,敬请大虾们指点指点,关于 checkBox 的填写,我费了很长的时间才搞定,上网查了很多资料,包括 itext 的官方网站, 关于这个类中的drawCheckBox,因为之前我没有找到能够填写 checkBox 的方法,我就想首先我扫描出整个 pdf 中的checkBox,每扫描到一个,就记录下它的position,然后将它从原pdf中抹掉,再根据记录下的position 重新画上一个带勾的checkBox,这个就是我最初的想法,不过后来我找到了其他方法,这个方法很简单,一个checkBox ,它有 name 和 value,你只需要使用 form.setField( name , value) ,这个name 和 value 就是这个checkBox 在pdf 中的name 和 value ,只要你这样做了,那么这个 checkBox 也就画上勾了,如果这是个 text Field ,那么这个text Field中显示的就是value这个值了,也就是说 checkBox 和 text Field的区别就是,text Field 是以name 对号入坐,如果name 对上了,那么它显示的就是value 的值;而checkBox 就是 name 和value 必须两个同时对上号,那么这个checkBox就华上了勾。
以下就是我写的类:
public class PdfUtil {
public PdfUtil() {
}
public static File fillTemplatePDF(Hashtable table, String pdfTemplatePath) throws
IOException, DocumentException {
PdfStamper stamp = null;
File file = new File("c:/test.pdf ");
PdfReader reader = new PdfReader(pdfTemplatePath);
FileOutputStream outputstream = new FileOutputStream(file);
stamp = new PdfStamper(reader, outputstream);
AcroFields form = stamp.getAcroFields();
Collection collection = table.keySet();
Object[] keyArray = collection.toArray();
for (Iterator i = reader.getAcroForm().getFields().iterator(); i.hasNext(); ) {
FieldInformation field = (FieldInformation) i.next();
String fieldName = field.getName();
for (int j = 0; j < keyArray.length; j++) {
String key = (String) keyArray[j];
String value = (String) table.get(key);
if (fieldName.equals(key)) {
form.setField(fieldName, value);
break;
}
// set the field read only
form.setFieldProperty(fieldName, "fflags", PdfFormField.FF_READ_ONLY, null);
}
}
stamp.close();
return file;
}
public static String merge(String firstName,
String mergeName) throws
FileNotFoundException, DocumentException, IOException {
return merge(firstName, null, mergeName);
}
public static String merge(String firstName, String secondName,
String mergeName) throws
FileNotFoundException, DocumentException, IOException {
PdfCopyFields copyFields = new PdfCopyFields(new FileOutputStream(
mergeName));
PdfReader firstReader = new PdfReader(new FileInputStream(firstName));
PdfReader seceondReader = null;
if (secondName != null) {
seceondReader = new PdfReader(new FileInputStream(secondName));
}
copyFields.open();
copyFields.addDocument(firstReader);
if(seceondReader!=null)
{
copyFields.addDocument(seceondReader);
}
copyFields.close();
return mergeName;
}
public static String merge(List filePathList,
String mergeName) throws
FileNotFoundException, DocumentException, IOException {
PdfCopyFields copyFields = new PdfCopyFields(new FileOutputStream(
mergeName));
copyFields.open();
for (int i = 0; i < filePathList.size(); i++) {
PdfReader reader = new PdfReader(new FileInputStream( (String)
filePathList.get(i)));
copyFields.addDocument(reader);
}
copyFields.close();
return mergeName;
}
public static void drawCheckBox(PdfStamper stamp, String fieldName,
String[] fieldValues,
float[] position, int k) {
PdfAcroForm paf = null;
PdfWriter writer = null;
int num = position.length / 5;
int j = 0;
AcroFields form = stamp.getAcroFields();
for (int i = 0; i < num; i++) {
boolean status = false;
if (i + 1 == k)
status = true;
int l = i * 5 + 1;
int b = i * 5 + 2;
int r = i * 5 + 3;
int t = i * 5 + 4;
float currentPageNum = position[5 * i];
Float temp = new Float(currentPageNum);
int pageNum = temp.intValue();
float left = position[l];
float bottom = position[b];
float right = position[r];
float top = position[t];
System.out.println("draw " + fieldName + "begin ....");
System.out.println("page number " + pageNum);
System.out.println("left " + left);
System.out.println("botton " + bottom);
System.out.println("right " + right);
System.out.println("top " + top);
writer = stamp.getWriter();
PdfFormField field = PdfFormField.createCheckBox(writer);
field.setFieldFlags(0);
// PdfBorderArray pb = new PdfBorderArray(1, 1, 1);
// field.setBorder(pb);
PdfBorderDictionary pbd = new PdfBorderDictionary(0,
PdfBorderDictionary.STYLE_INSET);
field.setBorderStyle(pbd);
PdfBorderDictionary border = new PdfBorderDictionary(0, 0);
field.setBorderStyle(border);
paf = writer.getAcroForm();
if (fieldValues[j].equals("Off")) {
j++;
}
paf.setCheckBoxParams(field, fieldName, fieldValues[j], status, left,
bottom, right, top);
paf.drawCheckBoxAppearences(field, fieldValues[j], left, bottom, right,
top);
stamp.addAnnotation(field, pageNum);
j++;
}
}
}
注:类中所用的 itext 类库,请在 itext 的官方网站http://www.lowagie.com/iText/下载。
在其网站上还有很多的例子,有兴趣的朋友可以下载下来研究研究。