当前位置: 首页 > ops >正文

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);   

示例代码:

第一种方式
    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");
 }

}

http://www.xdnf.cn/news/11227.html

相关文章:

  • 常用邮箱的 IMAP/POP3/SMTP 设置
  • 回溯经典-m图着色问题(和地图4色问题的区别)
  • php+html+mysql实现购物商城在线购物系统服装购物系统计算机源码获取php+mysql电子商务系统电商系统php毕业设计课程设计大作业
  • VC皮肤库SkinSharp 1 0 6 6的使用
  • Servlet基础详解)
  • 共享打印机无法打印?常见的12种解决方法【详解】
  • 防不胜防 这些游戏被外挂活生生地毁了
  • YTM32的Flash控制器EFM模块详解
  • Apache启动失败解决办法
  • 中国顶级黑客Top10,混得最好的你猜是谁
  • 十大MySQL开发工具
  • Linux打怪通关攻略(1)Linux用户管理
  • 最简单明了的QQ在线客服代码
  • 【MySQL】MySQL中的四大约束(非空约束、唯一性约束、主键约束、外键约束)详解【附源码】
  • shlwapi.h所包含的函数
  • 我理解的组件和控件的区别
  • Linux服务器搭建
  • Hibernate初学教程
  • java基础总结(三十)--java 2和java有什么区别
  • 什么是SOA
  • MySQL--sql查询练手
  • android studio基本使用
  • Java信天超市管理系统(源码+开题)
  • 51单片机的特殊寄存器
  • Linux命令(65)之zip
  • DirectoryInfo 类(总结)
  • SAP学习网站
  • fsdfdsfsdf
  • Android自定义View(三)自定义属性AttributeSet
  • 中国重大铁路事故一览,90年代以前基本都是爆炸事故,90年代以后基本都是追尾事故