Recent projects need to generate a PDF file according to the information that the customer has filled out in accordance with the template.
If the format is a fixed type, you can use Adobe Acrobat DC to convert the template's PDF file into a type that can be edited, and then insert the data into it based on the name of the variable that was taken when editing.
First, you need to import the Itextpdf jar package.
Template path
String TemplatePath = "E:/test.pdf";
The new file path generated
String Newpdfpath = "E:/ceshi.pdf";
Pdfreader reader;
FileOutputStream out;
Bytearrayoutputstream Bos;
Pdfstamper Stamper;
try {
Basefont bf = Basefont.createfont ("C:/windows/fonts/simyou. TTF ", basefont.identity_h,basefont.not_embedded); Set Chinese font
Font font = new Font (BF, n, 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 (); Get form
string[] str = {"2017-05-10", "12345678", "LC123456", "2017-05-10", "Shenzhen XXXXX Co., Ltd.", "4567890132", "Shenzhen xxxx Room", "34267687", "123344", "Shenzhen xxxxx Room"};
int i = 0;
Java.util.iterator<string> it = Form.getfields (). KeySet (). Iterator ();
Assign value
while (It.hasnext ()) {
String name = It.next (). toString ();
Form.setfieldproperty (name, "Textfont", BF, NULL);
Form.setfield (name, str[i++]);
}
Stamper.setformflattening (TRUE);//If it is false then the resulting PDF file can also be edited and must be set to True
Stamper.close ();
Document doc = new document ();
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) {
E.printstacktrace ();
} catch (Documentexception e) {
E.printstacktrace ();
}
However, a corresponding table is generated dynamically based on the information entered by the user. So finally, a new PDF is generated directly from the template.
This is mainly the use of paragraphs, tables for layout, the middle of the splicing of some data is OK. (The last file did not look good, or to be modified on the font and layout)
Instantiating a Document object
Document document = new Document (PAGESIZE.A4, 50, 50, 50, 50);
Set Chinese font
Basefont Bfchinese =
Basefont.createfont ("C:/windows/fonts/simhei.ttf", Basefont.identity_h, basefont.not_embedded);
Font
Font Titlechinese = new Font (Bfchinese, font.bold);
Font Secondtitlechinese = new Font (Bfchinese, +, font.bold);
Font Fontchinese = new Font (Bfchinese, n, Font.normal);
Create a PDFWriter object
PDFWriter writer = pdfwriter.getinstance (document,new fileoutputstream ("c:\\itexttest.pdf"));
Document.open ();
Paragraph title=new Paragraph ("main title", Titlechinese);
Title.setalignment (Element.align_center);
Paragraph title2=new Paragraph ("(subtitle)", Fontchinese);
Title2.setalignment (Element.align_center);
Set paragraph spacing
Title2.setspacingafter (30);
Document.add (title);
Document.add (Title2);
String str= "The body of a class of things .... ";
Paragraph content=new Paragraph (Str,fontchinese);
First Line Indent
Content.setfirstlineindent (20);
Set line spacing
Content.setleading (20);
Content.setspacingafter (30);
Document.add (content);
Paragraph title3=new Paragraph ("Supplier Accounts receivable transfer List", Secondtitlechinese);
Title3.setalignment (Element.align_center);
Document.add (TITLE3);
Table number and date
Paragraph id=new Paragraph ();
Chunk chunk=new Chunk ("No.: Abcdefgh", Fontchinese);
Chunk chunk2=new Chunk ("Date: June 29, 2017", Fontchinese);
Id.add (chunk);
Id.add (CHUNK2);
Id.setspacingafter (5);
Document.add (ID);
Create a PDF table
Float[] widths = {25f, 30f, 35f, 25f, 25f, 25f, 30f, 30f};
pdfptable table = new pdfptable (widths);
Pdfpcell cell = null;
First line
Cell = new Pdfpcell (New Paragraph ("Supplier", Fontchinese));
Cell.setverticalalignment (Element.align_middle);
Table.addcell (cell);
Cell = new Pdfpcell (New Paragraph ("Invoice number", Fontchinese));
Cell.setverticalalignment (Element.align_middle);
Table.addcell (cell);
Cell = new Pdfpcell (New Paragraph ("Currency", Fontchinese));
Cell.setverticalalignment (Element.align_middle);
Table.addcell (cell);
Cell = new Pdfpcell (New Paragraph ("Invoice Date", Fontchinese));
Cell.setverticalalignment (Element.align_middle);
Table.addcell (cell);
Cell = new Pdfpcell (New Paragraph ("Invoice coupon Amount", Fontchinese));
Cell.setverticalalignment (Element.align_middle);
Table.addcell (cell);
Cell = new Pdfpcell ("Vendor Receivables Amount (committed payment amount)", Fontchinese));
Cell.setverticalalignment (Element.align_middle);
Table.addcell (cell);
Cell = new Pdfpcell (New Paragraph ("committed payment date", Fontchinese));
Cell.setverticalalignment (Element.align_middle);
Table.addcell (cell);
Cell = new Pdfpcell (New Paragraph ("Remarks", Fontchinese));
Cell.setverticalalignment (Element.align_middle);
Table.addcell (cell);
To populate the table with data, you must populate the cell with an integer multiple of the number of columns, and none fills the empty
String[] danju=new string[]{"1", "2", "3", "4", "5", "6", "7", "8"};
for (int i = 0; I <danju.length; i++) {
Table.addcell (New Paragraph (Danju[i],fontchinese));
}
Table.addcell (New Paragraph ("Total", Fontchinese));
Table.addcell ("");
Table.addcell ("");
Table.addcell ("");
Table.addcell ("");
Table.addcell ("");
Table.addcell ("");
Table.addcell ("");
Document.add (table);
Signature
Pdfptable table2=new pdfptable (2);
Table2.setspacingbefore (30);
Pdfpcell sgin1 = new Pdfpcell (New Paragraph ("XX Enterprise Electronic signature:", Fontchinese));
Sgin1.setborder (0);
Pdfpcell sgin2 = new Pdfpcell (New Paragraph ("XX platform electronic signature:", Fontchinese));
Sgin2.setborder (0);
Table2.addcell (sgin1);
Table2.addcell (sgin2);
Pdfpcell image=new Pdfpcell (New Paragraph ("Signature area", Fontchinese));
Image.setfixedheight (50);
Image.setborder (0);
Table2.addcell (image);
Table2.addcell (image);
Chunk underline = new Chunk ("2017");
Underline.setunderline (0.1f, -1f);
Chunk underline2 = new Chunk ("6");
Underline2.setunderline (0.1f, -1f);
Chunk underline3 = new Chunk ("29");
Underline3.setunderline (0.1f, -1f);
Paragraph date=new Paragraph ();
Date.add (underline);
Date.add (New Chunk ("year", Fontchinese));
Date.add (Underline2);
Date.add (New Chunk ("month", Fontchinese));
Date.add (UNDERLINE3);
Date.add (New Chunk ("Day", Fontchinese));
Pdfpcell date1 = new Pdfpcell (date);
Date1.setborder (0);
Date1.sethorizontalalignment (Element.align_center);
Table2.addcell (date1);
Table2.addcell (date1);
Document.add (table2);
Document.close ();
Still think this method is not very good, how many templates will write how many, can not directly use the template.
Java Generate PDF