博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iText 解决中文问字体问题 显示中文
阅读量:3565 次
发布时间:2019-05-20

本文共 1725 字,大约阅读时间需要 5 分钟。

总结一下,基本上有三种方法解决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;  /**  * PDF2Chinese。  * 

详细说明:

*
* 三种方法解决iText中文问题。 *

修改列表:

*
*
*
*
* *
序号 作者 修改日期 修改内容
1 Oliver 2010-10-8 上午09:40:44 建立类型
* @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看到中文了!

 

转载地址:http://athrj.baihongyu.com/

你可能感兴趣的文章
Omap138开发板下以uboot2012.04.01为例分析uboot执行(八)
查看>>
Java发送邮件 注册成功发送邮件
查看>>
Mybatis的简单使用(增删改查),解决数据库字段名和实体类映射属性名不一致的问题
查看>>
Mybatis配置log4j文件 分页查询(limit,rowBounds)
查看>>
Mysql利用注解进行开发
查看>>
Mybatis一对多查询,多对一查询
查看>>
Spring配置bean.xml文件的头目录模板
查看>>
代理模式之------动态代理
查看>>
Spring实现AOP的三种方式
查看>>
Mybatis-Spring简单的配置和使用,配置事务
查看>>
SpringMVC和Mybatis整合使用的配置文件
查看>>
将字符串 “k:1|k1:2|k2:3|k3:4” 转换成字典{“k”:1,”k1”:2,”k2”:3,”k3”:4}
查看>>
AttributeError: 'tuple' object has no attribute 'decode'
查看>>
node爬虫(牛刀小试)
查看>>
关于vue的seo优化
查看>>
字符串在html中的页面中的换行
查看>>
react父子组件间的通信和传值
查看>>
vue-cli3.0设置环境变量
查看>>
vue父组件直接操作子组件的方法(不通过$emit和$on)
查看>>
vue上传文件到UCloud
查看>>