First, export the PDF tool class:
package pdf;import java.io.ByteArrayOutputStream;import java.io.File;import Java.io.filenotfoundexception;import java.io.fileoutputstream;import java.io.ioexception;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Iterator;import java.util.map;import com.itextpdf.text.documentexception;import com.itextpdf.text.pdf.acrofields; import com.itextpdf.text.pdf.basefont;import com.itextpdf.text.pdf.pdfreader;import com.itextpdf.text.pdf.pdfstamper;/** * Export PDF Tool class <br> * export PDF files based on a given PDF template, and replace the input fields in the PDF with the given content * @author wangtl * */public class exportpdfutil { /** * * Export PDF Tool Class <br> * Export PDF Tool Class * * @param outpath Output Path * @param fontPath font path * @param templatename pdf template file path name * @param content need to populate content * @return */ public static string exportpdf (String outpath, string templatename, string fontpath, map<string, string> content) { // Get current Time Date Now = new date (); simpledateformat Dataformat = new simpledateformat ("Yyyy-mm-dd-hh-mm-ss"); string t = dataformat.format (now); / / get a random number &nbsP; string ran = math.random () + ""; // add a random number to the current time to obtain the downloaded file to ensure that no duplicate name string filename = t + "-" + ran; String savepath = outpath + file.separator + filename + ". pdf"; Pdfreader reader = null; bytearrayoutputstream bos = null; pdfstamper ps = null ; fileoutputstream fos = null; try { // Create Font basefont chinesesong = basefont.createfont ( fontpath, basefont.identity_h, basefont.embedded); // Read pdf reader = new pdfreader (templatename); bos = new bytearrayoutputstream (); ps = new pdfstamper (Reader, bos); acrofields s = ps.getacrofields (); // Add the fonts that you created &Nbsp; s.addsubstitutionfont (Chinesesong); //find the input field in the PDF and replace it with the content iterator<string> it = s.getfields (). KeySet (). Iterator (); while (It.hasnext ()) { string name = (String) it.next (); s.setfield ("" + name.trim (), content.get (Name.trim ())); } //These two steps must have, otherwise PDF generation fails ps.setformflatTening (True); ps.close (); //Output pdf fos = new fileoutputstream (Savepath); fos.write (Bos.tobytearray ()); } catch (filenotfoundexception e) { system.out.println ("FileNotFoundException"); e.printstacktrace (); } catch (Exception e) { system.out.println ("Exception"); e.printstacktrace (); } finally { if (Null != reader) { reader.close () ; } try { if (Null != bos) { bos.close (); } } catch (ioexception e) { system.out.println ("failed to Close bytearrayoutputstream "); } try { if (NULL&NBSP;!=&NBSP;PS) { ps.close (); } } catch (documentexception e) { system.out.println ("failed to close PdfStamper "); } catch (ioexception e) { systeM.out.println ("failed to close pdfstamper "); } try { if (Null != fos) { fos.close (); } } catch (ioexception e) { system.out.println ("failed to close fileoutputstream "); } } return filename; }}
Second, the test class:
Package Test;import Java.util.hashmap;import java.util.map;import PDF. Exportpdfutil;public class Testpdf {public static void main (string[] args) {map<string,string> content=new Hashma P<string,string> (); Content.put ("name", "Hello");//The name of the input field defined by the template (for example: name), filled with the value exportpdfutil.exportpdf ("c:\\", "C:\\template.pdf", "c:// windows//fonts//simsun.ttc,1 ", content); }}
Export PDF files with Itext