iText输出中文的三种字体选择方式
2007-10-23 15:19
555 查看
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);
示例代码:
第一种方式
ByteArrayOutputStream baos = new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, baos);
writer.setViewerPreferences(PdfWriter.AllowPrinting | PdfWriter.PageLayoutSinglePage);
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 12, Font.NORMAL);
document.open();
Paragraph p = new Paragraph("你好", font);
document.add(p);
document.add(new Paragraph("Test2"));
Table table = new Table(2, 3);
table.addCell(new Phrase("我好", font));
table.addCell("C2R1");
table.addCell("C1R2");
table.addCell("C2R2");
Cell c = (Cell) table.getElement(0, 0);
c.setVerticalAlignment("Middle");
c.setBackgroundColor(new Color(255, 0, 0));
c.setHorizontalAlignment("Center");
document.add(table);
document.close();
baos.writeTo(new FileOutputStream("F://test.pdf"));
这种方式可能遇到的问题是adober的版本不同造成中文不能显示,可以用超星等其他浏览器查看效果
第二种方式
public static void main(String[] args) {
// TODO Auto-generated method stub
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileOutputStream("D://ChinesePDF005_"+new java.util.Date().getTime()+".pdf"));
// step 3: we open the document
document.open();
// step 4: we add content to the document
//楷体字
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//方正舒体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//FZSTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//方正姚体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//FZYTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//仿宋体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//黑体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文彩云
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STCAIYUN.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文仿宋
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STFANGSO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文细黑
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXIHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文新魏
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXINWEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文行楷
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXINGKA.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文中宋
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//隶书
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//宋体&新宋体 (这种字体的输出不了.有问题)
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMSUN.TTC", BaseFont.NOT_EMBEDDED, BaseFont.NOT_EMBEDDED);
//宋体-方正超大字符集
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SURSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//幼圆
BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfComic, 14);
String text1 = " 幼圆幼圆幼圆 This is the quite popular True Type font (繁體字測試VS简体字测试) ==>"+new java.util.Date();
document.add(new Paragraph(text1, font));
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
System.out.println(">>> Export : "+"D://ChinesePDF005__.pdf");
}
}
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);
示例代码:
第一种方式
ByteArrayOutputStream baos = new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, baos);
writer.setViewerPreferences(PdfWriter.AllowPrinting | PdfWriter.PageLayoutSinglePage);
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 12, Font.NORMAL);
document.open();
Paragraph p = new Paragraph("你好", font);
document.add(p);
document.add(new Paragraph("Test2"));
Table table = new Table(2, 3);
table.addCell(new Phrase("我好", font));
table.addCell("C2R1");
table.addCell("C1R2");
table.addCell("C2R2");
Cell c = (Cell) table.getElement(0, 0);
c.setVerticalAlignment("Middle");
c.setBackgroundColor(new Color(255, 0, 0));
c.setHorizontalAlignment("Center");
document.add(table);
document.close();
baos.writeTo(new FileOutputStream("F://test.pdf"));
这种方式可能遇到的问题是adober的版本不同造成中文不能显示,可以用超星等其他浏览器查看效果
第二种方式
public static void main(String[] args) {
// TODO Auto-generated method stub
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileOutputStream("D://ChinesePDF005_"+new java.util.Date().getTime()+".pdf"));
// step 3: we open the document
document.open();
// step 4: we add content to the document
//楷体字
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//方正舒体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//FZSTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//方正姚体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//FZYTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//仿宋体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//黑体
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文彩云
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STCAIYUN.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文仿宋
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STFANGSO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文细黑
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXIHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文新魏
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXINWEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文行楷
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXINGKA.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//华文中宋
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//隶书
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//宋体&新宋体 (这种字体的输出不了.有问题)
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMSUN.TTC", BaseFont.NOT_EMBEDDED, BaseFont.NOT_EMBEDDED);
//宋体-方正超大字符集
//BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SURSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//幼圆
BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfComic, 14);
String text1 = " 幼圆幼圆幼圆 This is the quite popular True Type font (繁體字測試VS简体字测试) ==>"+new java.util.Date();
document.add(new Paragraph(text1, font));
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
System.out.println(">>> Export : "+"D://ChinesePDF005__.pdf");
}
}
相关文章推荐
- iText输出中文的三种字体选择方式
- iText输出中文的三种字体选择方式
- 转载:iText输出中文的三种字体选择方式
- iText输出中文的三种字体选择方式
- (转)iText输出文字的三种字体选择方式
- iText中输出中文的方式
- 如何为ftb3.0编辑器添加选择中文字体的功能
- UCS-2与UTF8之间的选择(3)--windows中各编码字符串的C/C++输出支持及方式比较
- 排序的三种方式(冒泡,插入,和选择)
- Latex中文utf-8编码的三种方式
- JavaScript向页面进行输出的三种方式
- itextpdf html转pdf 中文字体问题
- php实现分页选择按钮的三种方式
- iText 解决中文问字体问题 显示中文
- iText生成pdf中文字体解决方案
- OpenGL 满屏的两种种方式,输出英文字体, FPS, 时间
- MATLAB 显示输出数据的三种方式
- webgl之创建三维文本,重点是实现中文字体的输出
- structs里面的三种传值方式,中文处理问题,还有简单的数据校验
- Latex中文utf-8编码的三种方式