Java export pdf document

Source: Internet
Author: User

Java export pdf document

Java exports pdf documents, mostly implemented by iText. You can create pdf documents and write content to them.

1 import package: A required package for the itext-2.0.6.jar itext.

ITextAsian. jar writes a required Chinese package to pdf.

 

2 code:

 

Package com.exp;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=. baseFont; import com.lowagie.text.pdf. extends writer; public class PDFUtil {public static void main (String [] args) {createPdf ();} public stati C void createPdf () {try {// create a test Document document Document = new document (PageSize. a4, 50,50, 50,50); valid writer = valid writer. getInstance (document, new FileOutputStream (c: \ test.pdf); // you must specify the font when writing data to the Chinese language. Otherwise, the data cannot be written to the Chinese language and iTextAsian is required. jarBaseFont bf = BaseFont. createFont (STSong-Light, UniGB-UCS2-H, BaseFont. NOT_EMBEDDED); Font font = new Font (bf, 12, Font. NORMAL); document. open (); document. add (new Paragraph (Chinese test !, Font); document. add (new Paragraph (some more); // specify the font format document. add (new Paragraph (some more, FontFactory. getFont (FontFactory. COURIER, 14, Font. BOLD, new Color (255,150,200); document. close ();} catch (Exception e) {e. printStackTrace ();}}}

 

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.