1, import the required jar package: Itext-asian-5.2.0.jar Itextpdf-5.5.11.jar.
2. New Word document, create template, save file as PDF, open edit with Adobe Acrobat DC, click "Prepare form" on the right and click "Start"
3. In the space where you need to insert the data, right click, click "Text Field", drag and drop the text field to the location you want, change the domain name as the variable name you passed in.
4. Save the file and put the file in the project. Generate the PDF code as follows:
1 Public Static voidCreatpdf (map<string, object>map,string FilePath) {2 Try {3Basefont bf = Basefont.createfont ("c://windows//fonts//simsun.ttc,1", Basefont.identity_h,4 basefont.embedded);5FileOutputStream out =NewFileOutputStream (FilePath);//output Stream6Pdfreader reader =NewPdfreader (Templatetoword.class. GetResource ("/com/cn/business/templates/report.pdf"));//Read PDF Template7Bytearrayoutputstream BOS =NewBytearrayoutputstream ();8Pdfstamper Stamper =Newpdfstamper (reader, BOS);9Stamper.setformflattening (true);TenAcrofields form =stamper.getacrofields (); One //content processing for text classes Amap<string, string> Datemap = (map<string, string>) map.get ("Datemap"); - Form.addsubstitutionfont (BF); - for(String key:datemap.keySet ()) { theString value =Datemap.get (key); - Form.setfield (key, value); - } - //content processing for picture classes +map<string, string> Imgmap = (map<string, string>) map.get ("Imgmap"); - for(String key:imgmap.keySet ()) { +String value =Imgmap.get (key); AString Imgpath =value; at intPageNo = Form.getfieldpositions (key). Get (0). Page; -Rectangle signrect = form.getfieldpositions (key). Get (0). Position; - floatx =signrect.getleft (); - floaty =Signrect.getbottom (); - //to read a picture from a path -Image image =image.getinstance (imgpath); in //Get Picture Page -Pdfcontentbyte under =stamper.getovercontent (pageno); to //Image size Adaptive + Image.scaletofit (Signrect.getwidth (), Signrect.getheight ()); - //Add a picture the image.setabsoluteposition (x, y); * under.addimage (image); $ }Panax Notoginseng //Table Class -map<string, list<list<string>>> Listmap = (map<string, list<list<string>>>) Map.get ("list"); the for(String key:listMap.keySet ()) { +list<list<string>> lists =Listmap.get (key); A intPageNo = Form.getfieldpositions (key). Get (0). Page; thePdfcontentbyte PCB =stamper.getovercontent (pageno); +Rectangle signrect = form.getfieldpositions (key). Get (0). Position; - //Table Position $ intColumn = Lists.get (0). Size (); $ introw =lists.size (); -pdfptable table =Newpdfptable (column); - floatTatalwidth = Signrect.getright ()-Signrect.getleft ()-1; the intSize = Lists.get (0). Size (); - floatWidth[] =New float[size];Wuyi for(inti=0;i<size;i++){ the if(i==0){ -width[i]=60f; Wu}Else{ -width[i]= (tatalWidth-60)/(size-1); About } $ } - table.settotalwidth (width); -Table.setlockedwidth (true); -Table.setkeeptogether (true); ATable.setsplitlate (false); +Table.setsplitrows (true); theFont Fontprove =NewFont (BF, 10, 0); - //form data Filling $ for(inti=0;i<row;i++){ thelist<string> list =Lists.get (i); the for(intj=0;j<column;j++){ theParagraph Paragraph =NewParagraph (String.valueof (List.get (j)), Fontprove); thePdfpcell cell =NewPdfpcell (paragraph); -Cell.setborderwidth (1); in cell.setverticalalignment (element.align_center); the cell.sethorizontalalignment (element.align_center); theCell.setleading (0, (float) 1.4); About Table.addcell (cell); the } the } theTable.writeselectedrows (0,-1, Signrect.getleft (), Signrect.gettop (), PCB); + } -Stamper.setformflattening (true);//if False, the resulting PDF file can be edited and if true, the resulting PDF file cannot be edited the stamper.close ();BayiDocument doc =NewDocument (); thePdfcopy copy =NewPdfcopy (Doc, out); the Doc.open (); - intPagenum =reader.getnumberofpages (); - for(inti = 1;i <= pagenum;i++){ thePdfimportedpage importpage = Copy.getimportedpage (NewPdfreader (Bos.tobytearray ()), i); the copy.addpage (importpage); the } the doc.close (); -}Catch(IOException e) { the System.out.println (e); the}Catch(documentexception e) { the System.out.println (e);94 } the the}
View Code
Parameters:
1 //Text Class2map<string, string> DataMap =NewHashmap<string, string>();3Datamap.put ("title", Title+Time );4 5 //Image6String Knowimgpath = "D:\\upload\\report\\knowimg.png";7map<string, string> imgmap =NewHashmap<string, string>();8Imgmap.put ("Knowimg", Knowimgpath);9 Ten //a table row of data is a list Onelist<string> list =NewArraylist<string>(); AList.add ("Date"); -List.add ("Amount"); - theList<string> List2 =NewArraylist<string>(); -List2.add ("2018-01-01")); -List2.add ("100"); - +List<list<string>> List =NewArraylist<list<string>>(); - List.add (List); + List.add (list2); A atmap<string, list<list<string>>> listmap =NewHashmap<string, list<list<string>>>(); -Listmap.put ("EventList", List); - -map<string, object> o =NewHashmap<string, object>(); -O.put ("Datemap", DATAMAP); -O.put ("Imgmap", imgmap); inO.put ("list", listmap); - toString Knowimgpath = "D:\\upload\\report\\report.pdf"; +Creatpdf (O,filepath);
View Code
"Java" Itext generate PDFs from templates (including pictures and tables)