Itextpdf + Adobe Acrobat DC filling template generation PDF Quick Start
There are many ways to generate PDF, such as using freemarker or using itextpdf. This document uses itextpdf to generate a PDF file
1. Download and install Adobe Acrobat DC
Download from http://get.adobe.com/cn/reader/otherversions/
Or download the green version.
After the installation is complete, it is shown as follows:
2. Use Adobe Acrobat DC to generate a PDF template for a Word file
To fill in the name and identity information, for example, set the attribute to realname and IDNO.
3. The running result is as follows:
4. Fill in the code
1/** 2 * Project name: MK-project <br> 3 * package name: com.suns.pdf <br> 4*5 * @ author MK <br> 6 * Date: <br> 7 */8 9 Package com.suns.pdf; 10 11 import com.itext;.text=. required Fields; 12 Import com.itext;.text=. basefont; 13 Import com.itext;.text=. pdfreader; 14 Import com.itext;.text=. optional Stamper; 15 Import Org. apache. commons. io. fileutils; 16 Import Org. apache. CO Mmons. io. ioutils; 17 18 Import Java. io. file; 19 Import Java. io. fileoutputstream; 20 Import Java. io. ioexception; 21 import Java. util. hashmap; 22 Import Java. util. map; 23 24/** 25 * Description: pdfutils <br> 26 * dependent packages: itextpdf itext-Asian 27 * commons-Io, commons-codec 28 * @ author MK 29 * @ date 8-8-11-2 14:32 <br> 30 * @ Param 31 * @ return 32 */33 public class pdfutils {34 35 36 public static Vo Id main (string [] ARGs) throws ioexception {37 hashmap map = new hashmap <string, string> (); 38 map. put ("realname", "corresponding to the form name realname in pdf"); 39 map. put ("IDNO", "corresponding to the form name IDNO in pdf"); 40 string Path = pdfutils. class. getresource ("/template "). getpath (); 41 system. out. println ("Path:" + path); 42 string sourcefile = path + file. separator + "testator"; 43 string targetfile = "D:/pdf/test_fill.pdf"; 44 genpdf (Map, Sourcefile, targetfile); 45} 46 47 Private Static void genpdf (hashmap map, string sourcefile, string targetfile) throws ioexception {48 file templatefile = new file (sourcefile ); 49 fillparam (MAP, fileutils. readfiletobytearray (templatefile), targetfile); 50} 51 52/** 53 * Description: Fill the PDF with parameters in map, the key in map corresponds to the field in the PDF form <br> 54 * @ author MK 55 * @ date 8-8-11-2 <br> 56 * @ Param 57 * @ r Eturn 58 */59 Public static void fillparam (Map <string, string> fieldvaluemap, byte [] File, string contractfilename) {60 fileoutputstream Fos = NULL; 61 try {62 Fos = new fileoutputstream (contractfilename); 63 pdfreader reader = NULL; 64 bytes Stamper = NULL; 65 basefont base = NULL; 66 try {67 reader = new pdfreader (File); 68 Stamper = new pdfstamper (reader, FOS); 69 Stamper. setformflatte Ning (true); 70 base = basefont. createfont ("stsong-light", "UniGB-UCS2-H", basefont. not_embedded); 71 then fields = Stamper. getaskfields (); 72 for (string key: Required Fields. getfields (). keyset () {73 fields. setfieldproperty (key, "textfont", base, null); 74 fields. setfieldproperty (key, "textsize", new float (9), null); 75} 76 if (fieldvaluemap! = NULL) {77 for (string fieldname: fieldvaluemap. keyset () {78 seconds fields. setfield (fieldname, fieldvaluemap. get (fieldname); 79} 80} 81} catch (exception e) {82 E. printstacktrace (); 83} finally {84 If (Stamper! = NULL) {85 try {86 Stamper. Close (); 87} catch (exception e) {88 E. printstacktrace (); 89} 90} 91 If (reader! = NULL) {92 reader. close (); 93} 94} 95 96} catch (exception e) {97 system. out. println ("fill parameter exception"); 98 E. printstacktrace (); 99} finally {100 ioutils. closequietly (FOS); 101} 102} 103}
Itextpdf + Adobe Acrobat DC filling template generation PDF Quick Start