First, use world to draw a template, and leave space for the places to be filled in. Save it as pdf ...... Currently, this PDF cannot be saved! You also need a tool: Adobe LiveCycle Designer ES2
For example, open the pdf template you generated.
You can see some components in the upper right corner, including TextField, CheckBox ...... Wait, you know! You can simply go to the specified location!
Then read and assign values using the JAVA program, and generate a new PDF with values.
You also need a jar package: itext-2.0.3.jar
The Code is as follows:
01
Package com. sun;
02
03
Import java. io. ByteArrayOutputStream;
04
Import java. io. FileNotFoundException;
05
Import java. io. FileOutputStream;
06
Import java. io. IOException;
07
Import java. util. HashMap;
08
Import java. util. Iterator;
09
Import java. util. Set;
10
11
Import com. lowagie. text. Document;
12
Import com. lowagie. text. extends entexception;
13
Import com.lowagie.textdetail. fields;
14
Import com.lowagie.text=. duplicate copy;
15
Import com.lowagie.text.html. PdfImportedPage;
16
Import com.lowagie.text.pdf. PdfReader;
17
Import com.lowagie.text=. datagstamper;
18
19
Public class extends template {
20
/**
21
* Assign values and generate new PDF documents
22
* @ Param templatePDF Template
23
* @ Param outFile: PDF Name output
24
* @ Param hashMap the data corresponding to templatePDF
25
* <A href = "http://my.oschina.net/arthor" class = "referer" target = "_ blank"> @ author </a> JIA-G-Y
26
* August 20, Year 12 17:20:36
27
*/
28
Public static void doSomeThing (String templatePDF, String outFile, HashMap <String, String> hashMap ){
29
Try {
30
FileOutputStream fos = new FileOutputStream (outFile );
31
PdfReader reader = new PdfReader (templatePDF );
32
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
33
PdfStamper stamp = new PdfStamper (reader, baos );
34
Required Fields form = stamp. get1_fields ();
35
Form = setField (form, hashMap );
36
Stamp. setFormFlattening (true );
37
Stamp. close ();
38
Document doc = new Document ();
39
Optional copy upload copy = new upload copy (doc, fos );
40
Doc. open ();
41
PdfImportedPage impPage = effeccopy. getImportedPage (new PdfReader (baos. toByteArray (), 1 );
42
Upload copy. addPage (impPage );
43
Doc. close ();
44
} Catch (FileNotFoundException e ){
45
E. printStackTrace ();
46
} Catch (IOException e ){
47
E. printStackTrace ();
48
} Catch (incluentexception e ){
49
E. printStackTrace ();
50
}
51
52
}
53
@ SuppressWarnings ({"unchecked", "unchecked "})
54
Public static writable fields setField (writable fields form, HashMap <String, String> fieldMap ){
55
Set it = fieldMap. keySet ();
56
Iterator itr = it. iterator ();
57
While (itr. hasNext ()){
58
Try {
59
Object temp = itr. next ();
60
Form. setField (temp. toString (), fieldMap. get (temp. toString ()));
61
} Catch (IOException e ){
62
E. printStackTrace ();
63
} Catch (incluentexception e ){
64
E. printStackTrace ();
65
}
66
}
67
Return form;
68
}
69
}
Test:
01
Package com. sun;
02
03
Import java. util. HashMap;
04
05
Public class Test {
06
Public static void main (String [] args ){
07
HashMap <String, String> hashMap = new HashMap <String, String> ();
08
HashMap. put ("Code", "JIA-G-Y ");
09
10
HashMap. put ("Tel", "400-8888888 ");
11
HashMap. put ("Address", "1859101 ");
12
HashMap. put ("GM", "2 ");
13
Using template. doSomeThing (". \ sunbench", "NewsPDF" + ". pdf", hashMap );
14
}
15
}
Template:
Result: