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中自带的字体为例说明如何输出中文:

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

一个完整的例子:

  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. import java.awt.Color;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.util.Date;
  20. import com.lowagie.text.Cell;
  21. import com.lowagie.text.Document;
  22. import com.lowagie.text.Element;
  23. import com.lowagie.text.Font;
  24. import com.lowagie.text.FontFactory;
  25. import com.lowagie.text.PageSize;
  26. import com.lowagie.text.Paragraph;
  27. import com.lowagie.text.Phrase;
  28. import com.lowagie.text.Rectangle;
  29. import com.lowagie.text.Table;
  30. import com.lowagie.text.pdf.BaseFont;
  31. import com.lowagie.text.pdf.PdfWriter;
  32. /**
  33. * Shows how a table is split if it doesn't fit the page.
  34. */
  35. public class RepeatingTable {
  36. /**
  37. * Shows how a table is split if it doesn't fit the page.
  38. *
  39. * @param args
  40. *            no arguments needed
  41. */
  42. public static void main(String[] args) {
  43. System.out.println("table splitting");
  44. // creation of the document with a certain size and certain margins
  45. Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
  46. try {
  47. // creation of the different writers
  48. String filePath = "d:" + File.separator + "temp" + File.separator
  49. + "iText_Generated_pdf" + File.separator + "table"
  50. + File.separator;
  51. File file = new File(filePath);
  52. if (!file.exists()) {
  53. file.mkdirs();
  54. }
  55. PdfWriter.getInstance(document, new FileOutputStream(filePath
  56. + "repeatingtable.pdf"));
  57. // we add some meta information to the document
  58. document.addAuthor("chenzwei@cn.ibm.com,CTE WAC,GBSC,CDL,IBM");
  59. document.addSubject("This is a sample of iText in CTE.");
  60. document.open();
  61. Table datatable = new Table(10);
  62. int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 };
  63. datatable.setWidths(headerwidths);
  64. datatable.setWidth(100);
  65. datatable.setPadding(3);
  66. // the first cell spans 10 columns
  67. Cell cell = new Cell(new Phrase(
  68. "Administration -System Users Report", FontFactory.getFont(
  69. FontFactory.HELVETICA, 24, Font.BOLD)));
  70. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  71. cell.setLeading(30);
  72. cell.setColspan(10);
  73. cell.setBorder(Rectangle.NO_BORDER);
  74. cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
  75. datatable.addCell(cell);
  76. // These cells span 2 rows
  77. datatable.setBorderWidth(2);
  78. datatable.setAlignment(1);
  79. datatable.addCell("User Id");
  80. datatable.addCell("Name\nAddress");
  81. datatable.addCell("Company");
  82. datatable.addCell("Department");
  83. datatable.addCell("Admin");
  84. datatable.addCell("Data");
  85. datatable.addCell("Expl");
  86. datatable.addCell("Prod");
  87. datatable.addCell("Proj");
  88. datatable.addCell("Online");
  89. // this is the end of the table header
  90. datatable.endHeaders();
  91. datatable.setBorderWidth(1);
  92. for (int i = 1; i < 30; i++) {
  93. datatable.setAlignment(Element.ALIGN_LEFT);
  94. datatable.addCell("myUserId");
  95. datatable
  96. .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");
  97. datatable.addCell("No Name Company");
  98. datatable.addCell("D" + i);
  99. datatable.setAlignment(Element.ALIGN_CENTER);
  100. datatable.addCell("No");
  101. datatable.addCell("Yes");
  102. datatable.addCell("No");
  103. datatable.addCell("Yes");
  104. datatable.addCell("No");
  105. datatable.addCell("Yes");
  106. }
  107. BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  108. Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
  109. document.add(new Paragraph(" 产生的报告",FontChinese));
  110. document.add(datatable);
  111. document.newPage();
  112. document.add(new Paragraph(
  113. "com.lowagie.text.pdf.PdfPTable - Cells split\n\n"));
  114. datatable.setConvert2pdfptable(true);
  115. document.add(datatable);
  116. document.newPage();
  117. document.add(new Paragraph(
  118. "com.lowagie.text.Table - Cells kept together"));
  119. datatable.setConvert2pdfptable(false);
  120. datatable.setCellsFitPage(true);
  121. document.add(datatable);
  122. document.newPage();
  123. document
  124. .add(new Paragraph(
  125. "com.lowagie.text.pdf.PdfPTable - Cells kept together\n\n"));
  126. datatable.setConvert2pdfptable(true);
  127. document.add(datatable);
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. }
  131. // we close the document
  132. document.close();
  133. }
  134. }

附录:

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 包文件)

iText中输出 中文的更多相关文章

  1. iText中输出中文

    原文链接 http://hintcnuie.iteye.com/blog/183690 转载内容 iText中输出中文,有三种方式: 1.使用iTextAsian.jar中的字体 BaseFont.c ...

  2. 关于Python中输出中文的一点疑问

    #encoding=gb2312 import urllib import re def getHtml(url): page = urllib.urlopen(url) html = page.re ...

  3. scrapy中输出中文保存中文

    1.json文件中文解码: #!/usr/bin/python #coding=utf-8 #author=dahu import json with open('huxiu.json','r') a ...

  4. C++输出中文字符(转)

    C++输出中文字符 1. cout 场景1: 在源文件中定义 const char* str = "中文" 在 VC++ 编译器上,由于Windows环境用 GBK编码,所以字符串 ...

  5. java web中请求和响应中包含中文出现乱码解析

    说明:在计算机中保存的一切文本信息是以一定的编码表(0,1,0,1)来保存我们所认识的字符(汉字或英文字符),由字符到计算机存储的二进制过程是编码,由读取二进制到文本的过程称为解码.而字符编码有多种不 ...

  6. 在Servlet中出现一个输出中文乱码的问题(已经解)。

    在Servlet中出现一个输出中文乱码的问题,已经解. @Override public void doPost(HttpServletRequest reqeust, HttpServletResp ...

  7. Python源码文件中带有中文时,输出乱码

    Python源码文件中带有中文时,文件头应加注释: #!/usr/bin/env python # -*- coding: utf-8 -*- 第一行注释是为了告诉Linux/OS X系统,这是一个P ...

  8. 【C++】解决c++中cout输出中文乱码问题

    问题:cout输出中文乱码.例如下面的代码输出会乱码. cout << "成功!" << endl; 输出结果: 解决方案: 控制台还原旧版即可,打开程序- ...

  9. asp.net core输出中文乱码的问题

    摘要 在学习asp.net core的时候,尝试在控制台,或者页面上输出中文,会出现乱码的问题. 问题重现 新建控制台和站点 public class Program { public static ...

随机推荐

  1. Tensorflow中的数据对象Dataset

    基础概念 在tensorflow的官方文档是这样介绍Dataset数据对象的: Dataset可以用来表示输入管道元素集合(张量的嵌套结构)和"逻辑计划"对这些元素的转换操作.在D ...

  2. JSP中实现网页访问统计的方法【转】

    我采用的是jsp网页,但是不管采用什么语言,原理是一样的. 第一种,单页面统计.就是说,只要点击这个页面就会统计一次. <body> <%!//在这种标记中定义的变量为全局变量 in ...

  3. 设置 mysql允许外网访问

    mysql的root账户,我在连接时通常用的是localhost或127.0.0.1,公司的测试服务器上的mysql也是localhost所以我想访问无法访问,测试暂停. 解决方法如下: 1,修改表, ...

  4. 深入理解JavaScript系列(结局篇)

    介绍 最近几个月忙得实在是不可开交,终于把<深入理解JavaScript系列>的最后两篇“补全”了,所谓的全是不准确的,因为很多内容都没有写呢,比如高性能.Ajax安全.DOM详解.Jav ...

  5. 在WPF中UserControl

    在这里我们将将打造一个UserControl(用户控件)来逐步讲解如何在WPF中自定义控件,并将WPF的一些新特性引入到自定义控件中来.我们制作了一个带语音报时功能的钟表控件, 效果如下: 在VS中右 ...

  6. 阿里云服务器(ECS)购买及配置总结

    云服务器是一种简单高效.安全可靠.处理能力可弹性伸缩的计算服务.其管理方式比物理服务器更简单高效.用户无需提前购买硬件,即可迅速创建或释放任意多台云服务器. 目前比较知名的与服务器提供商有:阿里云.百 ...

  7. bnu Game 博弈。

    Game Time Limit: 10000ms Case Time Limit: 10000ms Memory Limit: 65536KB   64-bit integer IO format:  ...

  8. poj 1947 树形背包 (删边)

    http://blog.csdn.net/woshi250hua/article/details/7632785 这道题我一开始想的dp[i][j],i是节点,j是删除的点数,dp是最少删边的个数,然 ...

  9. Oracle自定义函数&加密

    在sql中频繁使用的功能(逻辑.加密等)可以写成自定义函数进行封装,之后再调用即可. CREATE OR REPLACE FUNCTION "函数名" (参数名 参数类型 参数数据 ...

  10. PAT 1071. Speech Patterns

    又是考输入输出 #include <cstdio> #include <cstdlib> #include <string> #include <vector ...