JSP generate PDF document source code

Source: Internet
Author: User

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]);}
%>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.