Say less nonsense, do not understand itext do what use directly to Baidu Bar.
Make Template *******************
1. First use Word to make an interface
2. Convert to PDF format
3. Open the PDF you just converted into Word with Adobe Acrobat
The following interface will appear
Next
Click to browse and select the PDF you just converted
Next
4. Open it will automatically detect and name the form field, right-click on the form field, clicking on Properties, the Text Field Properties dialog box, some people said to change to Chinese font, but I did not change the same success
5. Generally do not need to modify something, at least I did not modify OH
6. Just save it as soon as you can.
On the program ********************************
Preparation: Itext Jar Bag: official website: http://sourceforge.net/projects/itext/files/
Because of the use of templates, there is no need to create fonts to solve the problem of non-display in Chinese
public void Filltemplate () {//Use template to generate PDF//template path string templatepath = "Pdffolder/template_demo.pdf"; The new file path generated by string Newpdfpath = "Pdffolder/newpdf.pdf"; Pdfreader reader; FileOutputStream out; Bytearrayoutputstream Bos; Pdfstamper Stamper;try {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 (); string[] str = {"123456789", "Xiao Lu", "Male", "1994-00-00", "130222111133338888", "Hebei Tangshan"};int i = 0;java.util.iterator< String> it = Form.getfields (). KeySet (). iterator (); while (It.hasnext ()) {String name = It.next (). toString (); SYSTEM.OUT.PRINTLN (name); Form.setfield (name, str[i++]); } stamper.setformflattening (TRUE);//If it is false then the resulting PDF file can also be edited, it 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) {System.out.println (1);} catch (Documentexception e) {System.out.println (2);}}
Template
Program Results
Can see, less a lu ... So I changed the form field of the template to the font of the song, the result of Chinese one also does not show, so I decided that his font support of the Chinese relatively few, first of all, regardless of this
Now it's two o'clock more (not two o'clock in the afternoon more ...) )
At this point, the use of template generated PDF has over, I say the introduction of the Hello Word level of the program, anyway idle is also idle
Program One:
1 Public voidTest1 () {//Generate PDF2Document document =NewDocument ();3 Try {4Pdfwriter.getinstance (document,NewFileOutputStream ("Pdffolder/1.pdf"));5 Document.open ();6Document.add (NewParagraph ("Hello word"));7 document.close ();8}Catch(FileNotFoundException |documentexception e) {9System.out.println ("File Create exception");Ten } One}
Results:
But if you want to output Chinese, the above is not going to use the language pack.
Latest Asian language Pack: http://sourceforge.net/projects/itext/files/extrajars/
1 Public voidtest1_1 () {2 Basefont BF;3Font font =NULL;4 Try {5BF = Basefont.createfont ("Stsong-light", "Unigb-ucs2-h",6 basefont.not_embedded);//Create a font7Font =NewFont (bf,12);//Use Fonts8}Catch(Documentexception |IOException E) {9 e.printstacktrace ();Ten } OneDocument document =NewDocument (); A Try { -Pdfwriter.getinstance (document,NewFileOutputStream ("Pdffolder/2.pdf")); - Document.open (); theDocument.add (NewParagraph ("Hello Word Hi World", font)); /reference Font - document.close (); -}Catch(FileNotFoundException |documentexception e) { -System.out.println ("File Create exception"); + } -}
The results are as follows:
Another way: I don't use a third-party language pack:
I created a new font folder in the project directory, and then copied the font files of the Arial into this folder.
On the program:
1 Public voidtest1_2 () {2 Basefont BF;3Font font =NULL;4 Try {5BF = Basefont.createfont ("font/simsun.ttc,1",//Note there is a, 16 Basefont.identity_h, basefont.not_embedded);7Font =NewFont (bf,12);8}Catch(Documentexception |IOException E) {9 e.printstacktrace ();Ten } OneDocument document =NewDocument (); A Try { -Pdfwriter.getinstance (document,NewFileOutputStream ("Pdffolder/3.pdf")); - Document.open (); theDocument.add (NewParagraph ("Another way to use Chinese", font)); - document.close (); -}Catch(FileNotFoundException |documentexception e) { -System.out.println ("File Create exception"); + } -}
Result ":
If I replaced: Hua Kang maiden text W5 (P). TTC, i.e.
BF = Basefont.createfont ("font/Hua Kang Maiden text W5 (P). ttc,1 ",//SIMSUN.TTC
Basefont.identity_h, basefont.not_embedded);
Haha, I like the Illustrated tutorial 02:44:58
What's NEW: Java uses Itext to fill out PDF templates and export (fight yesterday night four o'clock, know that today two points finally understand)