I. Use the pdf component iText to output text
/*** Use iText to output text (including Chinese) * @ author Administrator **/public class internal test {public static void main (String [] args) {Document document = new Document (PageSize. a4); // generate a document with parameters of no try {// Output d:/itext;pdfpdfpdfwriter = export writer. getInstance (document, new FileOutputStream ("d:/itext#"); // open the document. open (); // by default, only English documents are supported for writing text in pdf documents. add (new Paragraph ("hello itext"); // add Chinese characters, // 1. Directly reference the font in the system path to directly locate the system Reference the font position in the system // BaseFont bfChinese = BaseFont. createFont ("C:/WINDOWS/Fonts/SIMSUN. TTC, 1 ", BaseFont. IDENTITY_H, BaseFont. EMBEDDED); // 2. Create a Chinese font, the iTextAsian package encapsulates the system font. // use baseFont to create a font. The first parameter is the font name (the attribute file name under cmaps in the asian package) the second parameter is the encoding format (the encoding format corresponding to the attribute file name) // The third parameter is the three font selection methods for outputting Chinese characters/*** 1. Use iTextAsian. the font BaseFont. createFont ("STSong-Light", "UniGB-UCS2-H", BaseFont. NOT_EMBEDDED); 2. Use the Windows system font (TrueType) BaseFont. c ReateFont ("C:/WINDOWS/Fonts/SIMYOU. TTF ", BaseFont. IDENTITY_H, BaseFont. NOT_EMBEDDED); 3. Use the resource font (ClassPath) BaseFont. createFont ("/SIMYOU. TTF ", BaseFont. IDENTITY_H, BaseFont. NOT_EMBEDDED); */BaseFont bfChinese = BaseFont. createFont ("STSong-Light", "UniGB-UCS2-H", BaseFont. NOT_EMBEDDED); Font fontChinese = new Font (bfChinese, 12, Font. NORMAL); // Add the Chinese paragraph text document. add (new Paragraph ("first pdf example ", fontChinese); // add Document title document. addTitle ("iText exercise"); // Add document. addAuthor ("Zhu guozhi ");//... Document. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();}}}
Ii. Output table with iText
Public class MyPdfTable {public static void main (String [] args) {Document document = new Document (); // create a Document try {author writer = author writer. getInstance (document, new FileOutputStream ("d: // itexttabletables"); // create a three-row and three-column table PdfPTable = new PdfPTable (3 ); // The number of columns in the table export pcell cell = new partition pcell (new Paragraph ("header with colspan3"); // a cell is created. setColspan (3); // merge the cell table. addCell (cell); // Add the created cell to the table. addCell ("1.1"); // Add a cell to the "1.1" table. addCell ("2.1"); table. addCell ("3.1"); table. addCell ("1.2"); table. addCell ("2.2"); table. addCell ("3.2"); table. addCell ("1.3"); table. addCell ("2.3"); table. addCell ("3.3"); // open the document. open (); // Add the table to the document. add (table); // close document. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();}}}
3. iText outputs images and sets document attributes
/*** Use iText to output the image and set the document attribute * @ author Administrator **/public class my‑image {public static void main (String [] args) {// create an A4 document. By default, the Document size of the printer is A4Document document = new Document (PageSize. a4); try {// create a pdf output stream using writer = Using writer. getInstance (document, new FileOutputStream ("d :\\ itextImage.pdf"); // sets the document Author's document. addAuthor ("zhangguoqiang"); // sets the document title document. addTitle ("My itext output pdf"); // sets the topic document. addSubject ("Image Pdf"); // you can specify the keyword document. addKeywords ("itext"); // open the document. open (); // write the text document to the document. add (new Paragraph ("hello iText pdf"); // create an Image object. The parameter is the Image file name. Image bmp = Image. getInstance ("d: \ image1.jpg"); // The image size percentage, that is, the image size percentage relative to the document. bmp. scalePercent (100f); // Add the image to the document. add (bmp); // close document. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();}}}
4. output pdf with servlet
Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// create a Document object, a4 size document Document = new Document (PageSize. a4); // create a byte array output stream ByteArrayOutputStream stream = new ByteArrayOutputStream (); try {// create a pdf output stream using writer = Using writer. getInstance (document, stream); // open the document. open (); // write text document to pdf. add (new Paragraph ("Hello world, Hello iText ! "); // Close the document. close ();} catch (Exception e) {e. printStackTrace ();} // set the response document type to pdfresponse. setContentType ("application/pdf"); // set the response data size response. setContentLength (stream. size (); // the size of the output stream // obtain the response data ServletOutputStream out = response. getOutputStream (); // get the output stream in response // write the pdf data stream to the response stream. writeTo (out); out. flush (); out. close ();}