iText中输出 中文

iText中输出中文,有三种方式:


1、使用iTextAsian.jar中的字体
    BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
2、使用Windows系统字体(TrueType)
        BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    
3、使用资源字体(ClassPath)
    BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

第2、三种方式使用的字体多一些,但是需要和实际资源绑定,在实际项目中可以将一些字体库和项目打包在一起,下面我们以iTextAsian中自带的字体为例说明如何输出中文:

Java代码  收藏代码
  1. BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",   
  2.             BaseFont.NOT_EMBEDDED);   
  3.               
  4. Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);  
  5.         document.add(new Paragraph(" 产生的报告",FontChinese));  

一个完整的例子:

 

Java代码  收藏代码
  1. /* 
  2.  * $Id: RepeatingTable.java,v 1.5 2005/05/09 11:52:47 blowagie Exp $ 
  3.  * $Name:  $ 
  4.  * 
  5.  * This code is part of the 'iText Tutorial'. 
  6.  * You can find the complete tutorial at the following address: 
  7.  * http://itextdocs.lowagie.com/tutorial/ 
  8.  * 
  9.  * This code is distributed in the hope that it will be useful, 
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  12.  * 
  13.  * itext-questions@lists.sourceforge.net 
  14.  */  
  15. package com.lowagie.examples.objects.tables.alternatives;  
  16.   
  17. import java.awt.Color;  
  18. import java.io.File;  
  19. import java.io.FileOutputStream;  
  20. import java.util.Date;  
  21.   
  22. import com.lowagie.text.Cell;  
  23. import com.lowagie.text.Document;  
  24. import com.lowagie.text.Element;  
  25. import com.lowagie.text.Font;  
  26. import com.lowagie.text.FontFactory;  
  27. import com.lowagie.text.PageSize;  
  28. import com.lowagie.text.Paragraph;  
  29. import com.lowagie.text.Phrase;  
  30. import com.lowagie.text.Rectangle;  
  31. import com.lowagie.text.Table;  
  32. import com.lowagie.text.pdf.BaseFont;  
  33. import com.lowagie.text.pdf.PdfWriter;  
  34.   
  35. /** 
  36.  * Shows how a table is split if it doesn't fit the page. 
  37.  */  
  38. public class RepeatingTable {  
  39.   
  40.     /** 
  41.      * Shows how a table is split if it doesn't fit the page. 
  42.      *  
  43.      * @param args 
  44.      *            no arguments needed 
  45.      */  
  46.     public static void main(String[] args) {  
  47.         System.out.println("table splitting");  
  48.         // creation of the document with a certain size and certain margins  
  49.         Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);  
  50.   
  51.         try {  
  52.             // creation of the different writers  
  53.             String filePath = "d:" + File.separator + "temp" + File.separator  
  54.                     + "iText_Generated_pdf" + File.separator + "table"  
  55.                     + File.separator;  
  56.             File file = new File(filePath);  
  57.             if (!file.exists()) {  
  58.                 file.mkdirs();  
  59.             }  
  60.             PdfWriter.getInstance(document, new FileOutputStream(filePath  
  61.                     + "repeatingtable.pdf"));  
  62.   
  63.             // we add some meta information to the document  
  64.             document.addAuthor("chenzwei@cn.ibm.com,CTE WAC,GBSC,CDL,IBM");  
  65.             document.addSubject("This is a sample of iText in CTE.");  
  66.   
  67.             document.open();  
  68.   
  69.             Table datatable = new Table(10);  
  70.   
  71.             int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 };  
  72.             datatable.setWidths(headerwidths);  
  73.             datatable.setWidth(100);  
  74.             datatable.setPadding(3);  
  75.   
  76.             // the first cell spans 10 columns  
  77.             Cell cell = new Cell(new Phrase(  
  78.                     "Administration -System Users Report", FontFactory.getFont(  
  79.                             FontFactory.HELVETICA, 24, Font.BOLD)));  
  80.             cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
  81.             cell.setLeading(30);  
  82.             cell.setColspan(10);  
  83.             cell.setBorder(Rectangle.NO_BORDER);  
  84.             cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));  
  85.             datatable.addCell(cell);  
  86.   
  87.             // These cells span 2 rows  
  88.             datatable.setBorderWidth(2);  
  89.             datatable.setAlignment(1);  
  90.             datatable.addCell("User Id");  
  91.             datatable.addCell("Name\nAddress");  
  92.             datatable.addCell("Company");  
  93.             datatable.addCell("Department");  
  94.             datatable.addCell("Admin");  
  95.             datatable.addCell("Data");  
  96.             datatable.addCell("Expl");  
  97.             datatable.addCell("Prod");  
  98.             datatable.addCell("Proj");  
  99.             datatable.addCell("Online");  
  100.   
  101.             // this is the end of the table header  
  102.             datatable.endHeaders();  
  103.   
  104.             datatable.setBorderWidth(1);  
  105.   
  106.             for (int i = 1; i < 30; i++) {  
  107.   
  108.                 datatable.setAlignment(Element.ALIGN_LEFT);  
  109.   
  110.                 datatable.addCell("myUserId");  
  111.                 datatable  
  112.                         .addCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");  
  113.                 datatable.addCell("No Name Company");  
  114.                 datatable.addCell("D" + i);  
  115.   
  116.                 datatable.setAlignment(Element.ALIGN_CENTER);  
  117.                 datatable.addCell("No");  
  118.                 datatable.addCell("Yes");  
  119.                 datatable.addCell("No");  
  120.                 datatable.addCell("Yes");  
  121.                 datatable.addCell("No");  
  122.                 datatable.addCell("Yes");  
  123.   
  124.             }  
  125.             BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);   
  126.             Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);  
  127.             document.add(new Paragraph(" 产生的报告",FontChinese));  
  128.             document.add(datatable);  
  129.             document.newPage();  
  130.             document.add(new Paragraph(  
  131.                     "com.lowagie.text.pdf.PdfPTable - Cells split\n\n"));  
  132.             datatable.setConvert2pdfptable(true);  
  133.             document.add(datatable);  
  134.             document.newPage();  
  135.             document.add(new Paragraph(  
  136.                     "com.lowagie.text.Table - Cells kept together"));  
  137.             datatable.setConvert2pdfptable(false);  
  138.             datatable.setCellsFitPage(true);  
  139.             document.add(datatable);  
  140.             document.newPage();  
  141.             document  
  142.                     .add(new Paragraph(  
  143.                             "com.lowagie.text.pdf.PdfPTable - Cells kept together\n\n"));  
  144.             datatable.setConvert2pdfptable(true);  
  145.             document.add(datatable);  
  146.         } catch (Exception e) {  
  147.             e.printStackTrace();  
  148.         }  
  149.   
  150.         // we close the document  
  151.         document.close();  
  152.     }  
  153. }  

 

 

附录: 

http://www.lowagie.com/iText/tutorial/index.html (iText教程)   http://www.lowagie.com/iText/download.html (iText核心包文件)   http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948 (iTextArea 包文件)

posted @ 2016-04-06 09:23  疯子110  阅读(508)  评论(1编辑  收藏  举报