JSP generate PDF document source code access http://sourceforge.net/project/showfiles.php? Group_id = 15255 & release_id = 167948 download the latest version of the class library. After the download is complete, you will get a. jar package. Add this package to the JDK classpath for use. If the generated PDF file contains Chinese, Japanese, and Korean characters, you also need to download the itextasian. jar package by accessing the http://itext.sourceforge.net/downloads/iTextAsian.jar.
(If it is used in JSP, you just need to put two jar packages under the WEB-INF/lib directory)
Code :(Note: do not contain any characters except the non-code part of the page -- <%>; otherwise, an error will occur) <% @ page contenttype = "charset = gb2312" Language = "Java" Import = "Java. io. *, Java. AWT. color, Com. lowagie. text. *, com.lowagie.text=. * "%> <%
Response. setcontenttype ("application/pdf ");
Document document = new document (pagesize. A4, 36,36, 36,36 );
Bytearrayoutputstream buffer = new bytearrayoutputstream ();
Response writer = Response writer. getinstance (document, buffer );
Document. open ();
// Set the Chinese font
Basefont bfchinese = basefont. createfont ("stsong-light", "UniGB-UCS2-H", basefont. not_embedded );
Font fontchinese = new font (bfchinese, 12, Font. Normal );
String content = "We are pests! I'm bug! ";
Paragraph par = new paragraph (content, fontchinese); Table = new table (2 );
// Set the table border
Table. setborderwidth (1 );
Cell cell = new cell ("Matrix III ");
Cell. setheader (false );
// Column
Cell. setcolspan (2 );
Cell. setbackgroundcolor (color. Red );
Cell2.setheader (true );
Table. addcell (cell );
Image JPEG = image. getinstance ("C: // xxx.jpg ");
// Center the image
JPEG. setalignment (image. align_center );
Document. Add (PAR );
Document. Add (table );
Document. Add (JPEG );
Document. Close ();
Dataoutput output = new dataoutputstream (response. getoutputstream ());
Byte [] bytes = buffer. tobytearray ();
Response. setcontentlength (bytes. Length );
// Send directly to the client
For (INT I = 0; I <bytes. length; I ++) {output. writebyte (Bytes [I]);}
%>