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. Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
  4. 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. 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 包文件)

转载于:https://www.cnblogs.com/telwanggs/p/5357907.html

iText中输出 中文相关推荐

  1. vs2012编程中输出中文出现乱码

    关于"vs2012编程中输出中文出现乱码"问题的探究 问题如下: //代码如下// #include<iostream> using namespace std; in ...

  2. 怎么样才能在CODELITE中输出中文呢!!!

    前段时间做了个词法分析器,这两天想做语法分析中的算法优先符的程序的,结果不会了!悲剧,然后我就想着把我的词法分析器再整好看点,结果我用的  CODELITE  这IDE没法正确显示中文,郁闷!prin ...

  3. php 显示中文utf,php adodb 从mysql数据库中输出中文显示到utf编码网页乱码问题

    首先html的编码格式是utf-8, 然后php通过adodb连接后也要设置utf-8的连接方式, 然后查询的时候也要以utf-8的方式查询, 这样就不会造成输出中文乱码的问题了. 代码更改如下 ad ...

  4. python乱码怎么办_python中输出中文乱码怎么解决

    我们在使用python进行编程的时候,往往会面临输出中文的问题,这个时候往往会报错,小编这次与大家分享一下怎么解决. 工具/原料 Pycharm 方法/步骤 1 我们以pycharm为IDE工具,来举 ...

  5. python中如何输出中文_python3如何输出中文

    Python3中输出中文的方法如下: 方法一:在环境变量中,设置PYTHONIOENCODING=utf-8 以centos为例执行:export PYTHONIOENCODING=utf-8 方法二 ...

  6. java下输出中文的一点研究

    网上或者大部分书上都说Java中输出中文使用FileReader类就可以了,但是当你读取一个中文文档时,你会发现,除了乱码,还是乱码.究其原因,这其实是文件流读取时使用的编码方式和文件本身编码方式不同 ...

  7. python 如何输出中文_python3如何输出中文

    Python3中输出中文的方法如下: 方法一:在环境变量中,设置PYTHONIOENCODING=utf-8 以centos为例执行:export PYTHONIOENCODING=utf-8 方法二 ...

  8. putText输出中文方法

    最近有时候需要在图中输出中文,最先想到的是转换成GB2313的方法进行输出,但是不行,无奈只能想别的方法去实现. 先看看如果将utf-8转换成GB2313 std::string UTF8ToGBK( ...

  9. python加中文注释_Python使用中文注释和输出中文(原创)

    刚开始学习python,需要在Python中注释中文和输出中文,现在开始尝试: 仅为初步学习参考,高手请绕行. -------------------------------------------- ...

最新文章

  1. python中递归函数的实例_Python 递归函数详解及实例
  2. Go语言垃圾回收(GC)
  3. 通过销售订单领用到成本中心,FI替代实现不同成本中心记账科目不同
  4. java各种数据库对应的jar包、驱动类名和URL格式
  5. 如何用命令行给mySQL添加用户
  6. 盘点80年前欧美最为匪夷所思的发明
  7. 关于Ajax工作原理
  8. Fast-SCNN 多分支结构共享低级特征的语义分割网络 (一)
  9. C语言之字符串探究(八):strchr、strstr、strtok
  10. kprobe原理解析(一)
  11. LINUX下载编译libxml2
  12. 智能车制作1——编码器
  13. PYTHON混淆器 pyobfuscate
  14. vue实现中英文切换
  15. tf15: 中文语音识别【转】
  16. lzg_ad:EWF原理详解
  17. C语言学习-- 计算机原理及二进制
  18. 好用的一些功能性网站
  19. c语言计算二次函数顶点坐标,C语言编写一个求一元二次方程的实根的程序。 编辑一个小程序去做一元二次方程的求解(b^24ac)...
  20. 旧金山大学数据结构和算法的可视化工具

热门文章

  1. 深入探讨一下如何打断点
  2. 【AD】PCB设计知识整理(持续更新)
  3. 【C语言】关键字volatile之有关__IO、__O、__I的故事
  4. L2TP协议笔记1---L2TP概念及协议流程分析
  5. linux网络代码结构
  6. 崩坏3服务器维护2月8号,《崩坏3》8月29日版本更新,全服维护通知
  7. ip和端口的本质与作用,网络协议栈
  8. linux共享内存 pmu,如何使用gator/streamline 收集PMU perf event计数
  9. 【LeetCode】剑指 Offer 07. 重建二叉树
  10. JavaScript-面试 表单验证