Original: https://www.cnblogs.com/wangpeng00700/p/8418594.html
On the Internet to read some Java generated PDF files, write a little messy, some do not support writing Chinese fonts, some do not support the template, some just put the data inside the generated files, completely regardless of how the data placed and later maintenance, think or summarize a complete version of the tool to export PDF, Summarize the methods on the Internet, plus their own perfect.
This perfect comprehensive characteristic:
One-to-one, point-to-point to the corresponding place to write values, such as the template put a name tag, in the program to the "Zhang San" assigned to name, then the output of the PDF name of the place becomes Zhang San, accurate and convenient
Support Chinese, you can use the fonts you downloaded.
Support Image: The size of the image can be arbitrarily adjusted in the template, the resulting image will not exceed the range. And does not need to calculate according to the coordinates, the program inside calculates automatically.
Support for multi-page templates, even several pages of the template, as long as each variable corresponding to the range determined, the resulting format will not be confused.
Detailed steps and complete code are below.
First put a:
Here are the detailed steps:
1. First, create a new Word document with the following content, save as PDF, my name: Mytest.pdf.
2. Open the PDF file you just created with Adobe Acrobat Pro. Such as:
3. Click Create-->pdf form--Use current document--use current document, always click Next to finish. (Some versions of Adobe Acrobat Pro software have a ready form on the right, just click to prepare the form for the same effect)
4. The above box, double-click the black box to modify the name, you can also set the font size, such as
5. There is no black box in the picture, we can click the right button in the blank, select the Text field, and name the domain of the image img.
6. If you create a PDF template, save it directly. Note: The name inside the black box is the program to use later, the program according to the name of the corresponding location assignment. No matter what name you go to, just guarantee and the name of the program is always there.
7.pom.xml file:
<dependency> <groupId>com.itextpdf</groupId> <artifactid>itextpdf</ artifactid> <version>5.4.3</version> </dependency>
8. Tool class Files:
My template is placed under the C drive, so the path is c:/mytest.pdf, in addition, I also put a picture under the C disk c:/50336.jpg, the font is using the system font, you can also download their favorite font files on the Internet, the path to their own downloaded font file path can be
Package Wp.util;import Java.io.bytearrayoutputstream;import Java.io.fileoutputstream;import java.io.IOException; Import Java.util.hashmap;import java.util.map;import com.itextpdf.text.*;import com.itextpdf.text.pdf.*;/** * Created by Wangpeng on 2018/02/01. */public class Pdfutils {//Use template to generate PDF public static void Pdfout (Map<string,object> o) {//template path String TemplatePath = "C:/mytest.pdf"; The new file path generated by String Newpdfpath = "C:/testout1.pdf"; Pdfreader reader; FileOutputStream out; Bytearrayoutputstream Bos; Pdfstamper Stamper; try {basefont bf = Basefont.createfont ("c://windows//fonts//simsun.ttc,1", Basefont.identity_h, BaseFont.EMBE dded); Font Fontchinese = new Font (BF, 5, Font.normal); out = new FileOutputStream (newpdfpath);//output stream reader = new Pdfreader (templatepath);//Read PDF template Bos = new Bytearrayoutputstream (); Stamper = new Pdfstamper(reader, BOS); Acrofields form = Stamper.getacrofields (); The content processing of the text class map<string,string> Datemap = (map<string,string>) o.get ("Datemap"); Form.addsubstitutionfont (BF); For (String Key:datemap.keySet ()) {String value = Datemap.get (key); Form.setfield (Key,value); }//Picture class content processing map<string,string> Imgmap = (map<string,string>) o.get ("Imgmap"); For (String Key:imgmap.keySet ()) {String value = Imgmap.get (key); String Imgpath = value; int pageno = form.getfieldpositions (key). Get (0). Page; Rectangle signrect = form.getfieldpositions (key). Get (0). Position; float x = Signrect.getleft (); Float y = signrect.getbottom (); Reads a picture based on the path image image = Image.getinstance (Imgpath); Get Picture page Pdfcontentbyte under = Stamper.getovercontent (PageNo); Picture size Adaptive Image.scaletofit (Signrect.getwidth (), Signrect.getheight ()); Add picture image.setabsoluteposition (x, y); Under.addimage (image); } stamper.setformflattening (TRUE);//If False, the generated PDF file can be edited and if True, the generated PDF file cannot be edited Stamper.close (); Document doc = new document (); Font font = new Font (BF, 32); Pdfcopy copy = new Pdfcopy (doc, out); Doc.open (); Pdfimportedpage importpage = copy.getimportedpage (New Pdfreader (Bos.tobytearray ()), 1); Copy.addpage (Importpage); Doc.close (); } catch (IOException e) {System.out.println (e); } catch (Documentexception e) {System.out.println (e); }} public static void Main (string[] args) {map<string,string> Map = new HashMap (); Map.put ("name", "Zhang San"); Map.put ("Creatdate", "2018January 1 "); Map.put ("Weather", "Sunny"); Map.put ("Sports", "playing badminton"); map<string,string> map2 = new HashMap (); Map2.put ("img", "c:/50336.jpg"); Map<string,object> o=new HashMap (); O.put ("Datemap", map); O.put ("Imgmap", MAP2); Pdfout (o); }}
9. as follows:
Java export PDF based on template file