iText 解決中文問字型問題 顯示中文

來源:互聯網
上載者:User

總結一下,基本上有三種方法解決iText顯示中文問題。

 

方法一:使用Windows系統字型(TrueType)

方法二:使用iTextAsian.jar中的字型

方法三:使用資源字型(ClassPath)

由於比較簡單,直接上代碼了。

package oliver.itext.demo;  import java.io.File;  import java.io.FileOutputStream;  import java.io.IOException;  import java.io.OutputStream;  import com.itextpdf.text.Document;  import com.itextpdf.text.DocumentException;  import com.itextpdf.text.Font;  import com.itextpdf.text.Paragraph;  import com.itextpdf.text.pdf.BaseFont;  import com.itextpdf.text.pdf.PdfWriter;  /**  * <b>PDF2Chinese。</b>  * <p><b>詳細說明:</b></p>  * <!-- 在此添加詳細說明 -->  * 三種方法解決iText中文問題。  * <p><b>修改列表:</b></p>  * <table width="100%" cellSpacing=1 cellPadding=3 border=1>  * <tr bgcolor="#CCCCFF"><td>序號</td><td>作者</td><td>修改日期</td><td>修改內容</td></tr>  * <!-- 在此添加修改列表,參考第一行內容 -->  * <tr><td>1</td><td>Oliver</td><td>2010-10-8 上午09:40:44</td><td>建立類型</td></tr>  *   * </table>  * @version 1.0  * @author Oliver  * @since 1.0  */  public class PDF2Chinese  {      public static void main(String[] args) throws DocumentException, IOException      {          Document document = new Document();          OutputStream os = new FileOutputStream(new File("chinese.pdf"));          PdfWriter.getInstance(document,os);          document.open();          //方法一:使用Windows系統字型(TrueType)          BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);                    //方法二:使用iTextAsian.jar中的字型          //BaseFont baseFont = BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);                    //方法三:使用資源字型(ClassPath)          ////BaseFont baseFont = BaseFont.createFont("/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);                    Font font = new Font(baseFont);          document.add(new Paragraph("解決中文問題了!",font));          document.close();      }  }  

 

好了,現在可以開啟產生的chinese.pdf看到中文了!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.