Java exports PDF documents, mostly Itext, to create PDF documents and write content to documents.
1 Import Package: Itext-2.0.6.jar Itext must use the package.
Itextasian.jar write the required package to PDF in Chinese.
2 Code:
Package Com.exp.pdf;import Java.awt.color;import Java.io.fileoutputstream;import com.lowagie.text.document;import Com.lowagie.text.font;import Com.lowagie.text.fontfactory;import Com.lowagie.text.pagesize;import Com.lowagie.text.paragraph;import Com.lowagie.text.pdf.basefont;import Com.lowagie.text.pdf.pdfwriter;public Class Pdfutil {public static void main (string[] args) {createpdf ();} public static void CreatePDF () {try {//Create a test document in the C-disk document=new documents (pagesize.a4,50,50,50,50); PDFWriter writer=pdfwriter.getinstance (document, New FileOutputStream ("C:\\test.pdf"))//The font must be specified when writing to Chinese, otherwise it cannot be written in Chinese , Need Itextasian.jarbasefont Bf=basefont.createfont ("Stsong-light", "unigb-ucs2-h", basefont.not_embedded); Font font=new font (bf,12,font.normal);d ocument.open ();d Ocument.add (New Paragraph ("Chinese test! ", font));d Ocument.add (New Paragraph (" some more "));//Specify Font format document.add (new Paragraph (" Some more ", Fontfactory.getfont (fontfactory.courier,14,font.bold,new Color (255,150,200))));d ocument.close (); catch (EXception e) {e.printstacktrace ();}}}
Java Export PDF document