The main content of this article is: using Java in the PDF template to add data, pictures.
Needless to say, give a very simple example:
First create a Word document and export the PDF.
Open with software Adobe Acrobat, procedure
Adds a text field at the specified location, saving the exit. The PDF template is created and we save it to the E: disk, called practice.
Next comes the Java content.
In the Pom.xml file join,
<!--itext pictures to PDF-- <dependency> <groupId>com.itextpdf</groupId> < artifactid>itextpdf</artifactid> <version>5.5. Ten</version> </dependency>
Created at the controller layer, save time by attaching code directly
Package Com.boot.controller;import Java.io.outputstream;import java.io.unsupportedencodingexception;import Java.net.urlencoder;import Java.text.simpledateformat;import Java.util.calendar;import Java.util.Date;import Java.util.hashmap;import Java.util.map;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.restcontroller;import Com.itextpdf.text.image;import Com.itextpdf.text.rectangle;import Com.itextpdf.text.pdf.acrofields;import Com.itextpdf.text.pdf.basefont;import Com.itextpdf.text.pdf.pdfcontentbyte;import Com.itextpdf.text.pdf.pdfreader;import Com.itextpdf.text.pdf.PdfStamper; @RestController Public classPdfcontroller {/** * Export PDF * @author Changhai * @param response * @return * @throws unsupportedencodingexception */@RequestMapping (Value={"/exportpdf"}) PublicString exportpdf (httpservletresponse response) throws Unsupportedencodingexception {//specifying the parserSystem.setproperty ("javax.xml.parsers.DocumentBuilderFactory", "Com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); //String Path = Request.getsession (). Getservletcontext (). Getrealpath ("/upload/");String filename="Practice. pdf"; String Path="e:/"; Response.setcontenttype ("application/pdf"); Response.setheader ("content-disposition","attachment;filename="+ urlencoder.encode (filename,"UTF-8")); OutputStream OS=NULL; Pdfstamper PS=NULL; Pdfreader Reader=NULL; Try{OS=Response.getoutputstream (); //2 read PDF formReader =NewPdfreader (path+"/"+filename); //3 Generate a new PDF based on the formPS =Newpdfstamper (reader, OS); //4 getting PDF formsAcrofields form =Ps.getacrofields (); //5 adding Chinese fonts to the form this uses the system font. If not set, Chinese may not displayBasefont BF = Basefont.createfont ("C:/windows/fonts/simsun. ttc,1", Basefont.identity_h, basefont.embedded); Form.addsubstitutionfont (BF); //6 Querying data ================================================map<string, object> data =NewHashmap<string, object>(); Data.put ("name","Little Fella ."); Data.put (" like","Big Babes"); //7 Calendar Data Assign values to PDF form forms for(String key:data.keySet ()) {Form.setfield (key,data.Get(Key). ToString ()); } ps.setformflattening (true); //Add a picture-----------------------------PDF----------------------------------//get the page and coordinates from the domain name, the bottom left corner is the starting pointSystem. out. println ("PDF Add Picture"); String Imgpath="e:/Beautiful. png"; intPageNo = Form.getfieldpositions ("img").Get(0). Page; Rectangle Signrect= Form.getfieldpositions ("img").Get(0). Position; floatx =Signrect.getleft (); floaty =Signrect.getbottom (); //Read the pictureImage image =image.getinstance (Imgpath); //gets the page of the actionPdfcontentbyte under =ps.getovercontent (PageNo); //scale a picture based on the size of the fieldImage.scaletofit (Signrect.getwidth (), Signrect.getheight ()); //Add a pictureimage.setabsoluteposition (x, y); Under.addimage (image); //-------------------------------------------------------------System. out. println ("===============pdf Export Success ============="); } Catch(Exception e) {System. out. println ("===============pdf Export failed ============="); E.printstacktrace (); } finally { Try{ps.close (); Reader.close (); Os.close (); } Catch(Exception e) {e.printstacktrace (); } } return NULL; }}
Access on the browser
Www.localhost:8080/exportpdf
All right, PDF download successful
Thank you for reading
Create PDF templates, Java Add content, export download PDF