java使用iText生成pdf:解决中文

  1. /**  
  2.  * AsianTest.java  
  3.  */  
  4.   
  5. import java.io.FileOutputStream;   
  6. import java.io.IOException;   
  7.   
  8. import com.lowagie.text.*;   
  9. import com.lowagie.text.pdf.PdfWriter;   
  10. import com.lowagie.text.pdf.BaseFont;   
  11. import com.lowagie.text.Font;   
  12. import java.awt.Color;   
  13.   
  14. public class AsianTest {   
  15.   
  16.     public static void main(String[] args) {   
  17.   
  18.         // 创建一个Document对象   
  19.         Document document = new Document();   
  20.   
  21.         try {   
  22.   
  23.             // 生成名为 AsianTest.pdf 的文档   
  24.             PdfWriter.getInstance(document, new FileOutputStream(   
  25.                     "c://AsianTest.pdf"));   
  26.   
  27.             /**  
  28.              * 新建一个字体,iText的方法 STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀  
  29.              * UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀 H 代表文字版式是 横版, 相应的 V  
  30.              * 代表竖版  
  31.              */  
  32.             BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",   
  33.                     "UniGB-UCS2-H"false);   
  34.   
  35.             Font bold_fontChinese = new Font(bfChinese, 12, Font.BOLD,   
  36.                     Color.BLACK);   
  37.             Font italic_fontChinese = new Font(bfChinese, 12, Font.ITALIC,   
  38.                     Color.BLACK);   
  39.             Font impressFont = new Font(bfChinese, 16, Font.BOLDITALIC,   
  40.                     Color.BLACK);   
  41.             // 打开文档,将要写入内容   
  42.             document.open();   
  43.   
  44.             // 插入一个段落   
  45.             // Paragraph par = new Paragraph("我们", fontChinese);   
  46.   
  47.             // document.add(par);   
  48.             //   
  49.             document.add(new Paragraph(" ", bold_fontChinese));   
  50.             document.add(new Paragraph(" ", bold_fontChinese));   
  51.             document.add(new Paragraph(" ", bold_fontChinese));   
  52.             String[] Trainspotting1 = { "选择生命,选择工作,选择职业,选择家庭,",   
  53.                     "选择可恶的大彩电,选择洗衣机、汽车、雷射碟机,""选择健康、低胆固醇和牙医保险,选择楼宇按揭,",   
  54.                     "选择你的朋友,选择套装、便服和行李,选择分期付款和三件套西装,",   
  55.                     "选择收看无聊的游戏节目,边看边吃零食……选择你的未来,选择生命……""太多选择,你选择什么,我选择不选择。" };   
  56.             String[] Trainspotting2 = { "这是电影《猜火车》开头的旁白。""这是一个关于“选择”的故事。" };   
  57.             String[] Benjamin1 = { "有些人就在河边出生长大,""有些人被闪电击中,",   
  58.                     "有些人对音乐有着非凡的天赋,""有些人是艺术家,""有人会游泳,""有人懂得做纽扣,",   
  59.                     "有人会背莎士比亚,""而有些人。。。是母亲,""也有些人,可以翩翩起舞。",   
  60.                     "Goodnight  Daisy""Goodnight  Benjamin" };   
  61.             String[] Benjamin2 = { "这是电影《本杰明传奇》结尾的旁白。""这是一个关于“错过”的故事。" };   
  62.             String[] text1 = { "我想说的是,""我们选择,同时,我们错过。" };   
  63.             String[] text2 = { "抛去无可选择的选择,抑或不选择的选择,",   
  64.                     "很有趣的一件事:当面临(太多的)选择,人们会如何选择;""同时,人们又会如何看待错过。" };   
  65.             String[] text3 = { "在开始和结束之间,选择了什么,又会错过什么,我还不知道。" };   
  66.             String[] text4 = { "你会知道么?" };   
  67.             //   
  68.             for (String s : Trainspotting1) {   
  69.                 document.add(new Paragraph(s, italic_fontChinese));   
  70.                 document.add(new Paragraph(" ", italic_fontChinese));   
  71.             }   
  72.             for (String s : Trainspotting2) {   
  73.                 document.add(new Paragraph(s, bold_fontChinese));   
  74.             }   
  75.             document.add(new Paragraph(" ", bold_fontChinese));   
  76.             document.add(new Paragraph(" ", bold_fontChinese));   
  77.             document.add(new Paragraph(" ", bold_fontChinese));   
  78.             for (String s : Benjamin1) {   
  79.                 document.add(new Paragraph(s, italic_fontChinese));   
  80.                 document.add(new Paragraph(" ", italic_fontChinese));   
  81.             }   
  82.             for (String s : Benjamin2) {   
  83.                 document.add(new Paragraph(s, bold_fontChinese));   
  84.             }   
  85.             document.add(new Paragraph(" ", bold_fontChinese));   
  86.             document.add(new Paragraph(" ", bold_fontChinese));   
  87.             document.add(new Paragraph(" ", bold_fontChinese));   
  88.             for (String s : text1) {   
  89.                 document.add(new Paragraph(s, bold_fontChinese));   
  90.             }   
  91.             document.add(new Paragraph(" ", bold_fontChinese));   
  92.             for (String s : text2) {   
  93.                 document.add(new Paragraph(s, bold_fontChinese));   
  94.             }   
  95.             document.add(new Paragraph(" ", bold_fontChinese));   
  96.             for (String s : text3) {   
  97.                 document.add(new Paragraph(s, bold_fontChinese));   
  98.             }   
  99.             document.add(new Paragraph(" ", bold_fontChinese));   
  100.             for (String s : text4) {   
  101.                 document.add(new Paragraph(s, bold_fontChinese));   
  102.             }   
  103.             document.add(new Paragraph(" ", bold_fontChinese));   
  104.             //   
  105.             String[] end = { "Some people were born to sit by a river...",   
  106.                     "Some get struck by light...",   
  107.                     "Some have an ear for music...""Some are artists...",   
  108.                     "Some swim...""Some know buttons...",   
  109.                     "Some know Shakespeare...""Some are mothers...",   
  110.                     "And some people can dance..." };   
  111.             for (String s : end) {   
  112.                 document.add(new Paragraph(s, bold_fontChinese));   
  113.             }   
  114.             document.add(new Paragraph(   
  115.                     "by the way, some people can write code.你", impressFont));   
  116.   
  117.             // Chapter   
  118.             Paragraph title1 = new Paragraph("Chapter 1", italic_fontChinese);   
  119.             Chapter chapter1 = new Chapter(title1, 1);   
  120.             chapter1.setNumberDepth(0);   
  121.             Paragraph title11 = new Paragraph(   
  122.                     "This is Section 1 in Chapter 1中文", italic_fontChinese);   
  123.             Section section1 = chapter1.addSection(title11);   
  124.             Paragraph someSectionText = new Paragraph(   
  125.                     "This text comes as part of section 1 of chapter 1.");   
  126.             section1.add(someSectionText);   
  127.             someSectionText = new Paragraph("Following is a 3 X 2 table.");   
  128.             section1.add(someSectionText);   
  129.             //   
  130.             document.add(chapter1);   
  131.             //   
  132.             // 定义一个图片   
  133.   
  134.             Image jpeg = Image.getInstance("E:/01.jpg");   
  135.   
  136.             // 图片居中   
  137.             jpeg.setAlignment(Image.ALIGN_CENTER);   
  138.             document.add(jpeg);   
  139.         } catch (DocumentException de) {   
  140.             System.err.println(de.getMessage());   
  141.         } catch (IOException ioe) {   
  142.             System.err.println(ioe.getMessage());   
  143.         }   
  144.   
  145.         // 关闭打开的文档   
  146.         document.close();   
  147.     }   
  148. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值