iText中输出中文
原文链接
http://hintcnuie.iteye.com/blog/183690
转载内容
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中自带的字体为例说明如何输出中文:
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
document.add(new Paragraph(" 产生的报告",FontChinese));
一个完整的例子:
/*
* $Id: RepeatingTable.java,v 1.5 2005/05/09 11:52:47 blowagie Exp $
* $Name: $
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
package com.lowagie.examples.objects.tables.alternatives;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
/**
* Shows how a table is split if it doesn't fit the page.
*/
public class RepeatingTable {
/**
* Shows how a table is split if it doesn't fit the page.
*
* @param args
* no arguments needed
*/
public static void main(String[] args) {
System.out.println("table splitting");
// creation of the document with a certain size and certain margins
Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
try {
// creation of the different writers
String filePath = "d:" + File.separator + "temp" + File.separator
+ "iText_Generated_pdf" + File.separator + "table"
+ File.separator;
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
PdfWriter.getInstance(document, new FileOutputStream(filePath
+ "repeatingtable.pdf"));
// we add some meta information to the document
document.addAuthor("chenzwei@cn.ibm.com,CTE WAC,GBSC,CDL,IBM");
document.addSubject("This is a sample of iText in CTE.");
document.open();
Table datatable = new Table(10);
int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 };
datatable.setWidths(headerwidths);
datatable.setWidth(100);
datatable.setPadding(3);
// the first cell spans 10 columns
Cell cell = new Cell(new Phrase(
"Administration -System Users Report", FontFactory.getFont(
FontFactory.HELVETICA, 24, Font.BOLD)));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(30);
cell.setColspan(10);
cell.setBorder(Rectangle.NO_BORDER);
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
datatable.addCell(cell);
// These cells span 2 rows
datatable.setBorderWidth(2);
datatable.setAlignment(1);
datatable.addCell("User Id");
datatable.addCell("Name\nAddress");
datatable.addCell("Company");
datatable.addCell("Department");
datatable.addCell("Admin");
datatable.addCell("Data");
datatable.addCell("Expl");
datatable.addCell("Prod");
datatable.addCell("Proj");
datatable.addCell("Online");
// this is the end of the table header
datatable.endHeaders();
datatable.setBorderWidth(1);
for (int i = 1; i < 30; i++) {
datatable.setAlignment(Element.ALIGN_LEFT);
datatable.addCell("myUserId");
datatable
.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");
datatable.addCell("No Name Company");
datatable.addCell("D" + i);
datatable.setAlignment(Element.ALIGN_CENTER);
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
}
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
document.add(new Paragraph(" 产生的报告",FontChinese));
document.add(datatable);
document.newPage();
document.add(new Paragraph(
"com.lowagie.text.pdf.PdfPTable - Cells split\n\n"));
datatable.setConvert2pdfptable(true);
document.add(datatable);
document.newPage();
document.add(new Paragraph(
"com.lowagie.text.Table - Cells kept together"));
datatable.setConvert2pdfptable(false);
datatable.setCellsFitPage(true);
document.add(datatable);
document.newPage();
document
.add(new Paragraph(
"com.lowagie.text.pdf.PdfPTable - Cells kept together\n\n"));
datatable.setConvert2pdfptable(true);
document.add(datatable);
} catch (Exception e) {
e.printStackTrace();
}
// we close the document
document.close();
}
}
附录:
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中输出中文的更多相关文章
- iText中输出 中文
iText中输出中文,有三种方式: 1.使用iTextAsian.jar中的字体 BaseFont.createFont("STSong-Light", "UniG ...
- 关于Python中输出中文的一点疑问
#encoding=gb2312 import urllib import re def getHtml(url): page = urllib.urlopen(url) html = page.re ...
- scrapy中输出中文保存中文
1.json文件中文解码: #!/usr/bin/python #coding=utf-8 #author=dahu import json with open('huxiu.json','r') a ...
- C++输出中文字符(转)
C++输出中文字符 1. cout 场景1: 在源文件中定义 const char* str = "中文" 在 VC++ 编译器上,由于Windows环境用 GBK编码,所以字符串 ...
- java web中请求和响应中包含中文出现乱码解析
说明:在计算机中保存的一切文本信息是以一定的编码表(0,1,0,1)来保存我们所认识的字符(汉字或英文字符),由字符到计算机存储的二进制过程是编码,由读取二进制到文本的过程称为解码.而字符编码有多种不 ...
- 在Servlet中出现一个输出中文乱码的问题(已经解)。
在Servlet中出现一个输出中文乱码的问题,已经解. @Override public void doPost(HttpServletRequest reqeust, HttpServletResp ...
- Python源码文件中带有中文时,输出乱码
Python源码文件中带有中文时,文件头应加注释: #!/usr/bin/env python # -*- coding: utf-8 -*- 第一行注释是为了告诉Linux/OS X系统,这是一个P ...
- 【C++】解决c++中cout输出中文乱码问题
问题:cout输出中文乱码.例如下面的代码输出会乱码. cout << "成功!" << endl; 输出结果: 解决方案: 控制台还原旧版即可,打开程序- ...
- asp.net core输出中文乱码的问题
摘要 在学习asp.net core的时候,尝试在控制台,或者页面上输出中文,会出现乱码的问题. 问题重现 新建控制台和站点 public class Program { public static ...
随机推荐
- 【驱动】USB驱动实例·串口驱动·键盘驱动
Preface USB体系支持多种类型的设备. 在 Linux内核,所有的USB设备都使用 usb_driver结构描述. 对于不同类型的 USB设备,内核使用传统的设备驱动模型建立设备驱动 ...
- poj2965(位运算压缩+bfs+记忆路径)
题意:有个4*4的开关,里面有着16个小开关 -+-- ---- ---- '+'表示开关是关着的,'-'表示开关是开着的,只有所有的开关全被打开,总开关才会被打开.现在有一种操作,只要改变某个开关, ...
- C#防止内存泄露的方法
一般程序员()都会这样认为:用C#这样的语言编程的一个好处就是无需再考虑内存的分配和释放.你只需创建对象,然后通过一种叫做垃圾收集的机制来处理这 些对象,也就是说:当它们不再被应用程序需要的时候来自动 ...
- 神奇的bug,退出时自动更新时间
遇到一个神奇的bug,用户退出时,上次登录时间会变成退出时的时间. 于是开始跟踪,发现Laravel在退出时,会做一次脏检查,这时会更新rember_token,这时就会有update操作如下. 而粗 ...
- java jar包与配置文件的写法
一个普通的java project,里面引用了config.properties配置文件,将项目打成Runnable jar,然后将config.properties放到打包后的jar路径下,执行该j ...
- cacti监控jvm
jdk环境 java version "1.6.0_37" Java(TM) SE Runtime Environment (build 1.6.0_37-b06) Java Ho ...
- CentOS重置Mysql密码
1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...
- RPC框架与分布式服务框架的区别
第一:RPC框架是点对点的通信方式,即服务消费者与服务提供者是点对点通信 第二:分布式服务框架,不近具有RPC框架的特性,同时,还包括以下特性: 1.提供多台服务器提供服务,具有负载均衡策略 2.服务 ...
- Win10下打开chm文档提示无法显示该页的解决方法
一是检查chm文件属性里最下面是否有个“解除锁定”,如有,点击“解除锁定”按钮就可以了. 如果没有上面提到的“解除锁定”,检查chm文件存放的路径.本例中,由于chm文件的存放路径中不能带有特殊字符“ ...
- openvpn之安装篇
之前整过openvpn,现把一些过程记录下来,方便以后查阅. #!/bin/bash cd /usr/local/srcyum install pam-devel.x86_64 -ywget http ...