Package Snake;import Java.io.file;import Java.io.fileinputstream;import java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Com.lowagie.text.document;import Com.lowagie.text.documentexception;import Com.lowagie.text.font;import Com.lowagie.text.paragraph;import Com.lowagie.text.pdf.basefont;import Com.lowagie.text.pdf.pdfwriter;public class Pdfutil {public static void PrintPDF (String content) throws Documentexception,ioexception {Document document = new document (); Pdfwriter.getinstance (document, New FileOutputStream ("D:/demo.pdf"));//Basefont Chinese = basefont.createfont (" Stsong-light ",//" Unigb-ucs2-h ", basefont.not_embedded);//Use a font that comes with windows Basefont Chinese = basefont.createfont (" C:/ windows/fonts/simsun.ttc,1 ", Basefont.identity_h, basefont.embedded); Font font = new Font (Chinese, font.normal);d ocument.addauthor ("Westdream");d ocument.addtitle ("Test iText"); Document.addsubject ("This was an IText demo");d ocument.addkeywords ("iText keywords");d ocumeNt.addcreator ("Using iText");d Ocument.open ();d Ocument.add (new Paragraph (content, font));d ocument.close (); public static void Main (string[] args) throws Exception {File File = new file ("C:\\users\\zan\\desktop\\demo.txt"); byte[] bytes = new byte[(int) file.length ()];inputstream input = new FileInputStream (file); input.read (bytes, 0, (int) file.lengt H ());p rintpdf (New String (bytes, "GBK"));}}
Java output PDF