For a few more time, I do it in a project that requires IText to fill in the FormField and Checkbox in the PDF, and multiple PDF files merge into a PDF file. Now I have tied it out, and I would like to give pointers about the poison. About Checkbox, I took a long time to get a lot of information, including the official website of IText, About Drawcheckbox in this class, because I didn't find the way to fill in the checkbox. I would like to first scan the Checkbox in the PDF. Each scan, record it under its position, then remove it from the original PDF, then according to record Posion reorganizes a hook, this is my original idea, but later I found other ways, this method is very simple, a checkbox, it has Name and Value, you only need to use form.setfield (name, value This name and value are this checkbox Name and Value in the PDF, as long as you do this, then this checkbox will draw, if this is a text field, then this text field is Value this value. That is to say, the difference between Checkbox and Text Field is that the text field is in the Name pair, if the name is on, then it is displayed by the value of the value; the checkbox is Name and Value must be two simultaneous pairs. No., then this checkbox has hooked in China. Here is the class I wrote:
Public class pdfutil {
Public pdfutil () {
}
Public Static File FillTemplatePdf (Hashtable Table, String PdftemplatePath) throws oews oException, 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 ();
ITerator I = Reader.GetAcroform (). getfields (). Iterator (); i.hasnext () {
FieldInformation Field = (FieldInformation) i.next (); string fieldName = Field.getName ();
For (int J = 0; 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", PDFMField.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 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); 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 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 write = null; Int num = position.length / 5; int J = 0; Acrofields form = stamp.getacrofields (); For (int i = 0; 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.drawcheckboxappearence (Field, FieldValues [J], Left, Bottom, Right, TOP); Stamp.Addannotation (Field, Pagenum); J ; } } Note: Use the ITEXT class library used in the class, please download at ITTP: //www.lowagie.com/itext/. There are still many examples on its website, interested friends can download research research.