前一段時間,我做的一個項目中要求用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/下載。
在其網站上還有很多的例子,有興趣的朋友可以下載下來研究研究。