现在的位置: 首页 > 综合 > 正文

iText 中写Word RTF 文档 中文字体设置 [转]

2013年09月09日 ⁄ 综合 ⁄ 共 2805字 ⁄ 字号 评论关闭

http://www.kaifajie.cn/kecheng/java/8421.html

传统使用iTextAsian.jar中定义的字体

 

Java代码
BaseFont   bfChinese   =   BaseFont.createFont("STSong-Light",   "UniGB-UCS2-H",   BaseFont.NOT_EMBEDDED);    
com.lowagie.text.Font   FontChinese   =   new   com.lowagie.text.Font(bfChinese,   12,   com.lowagie.text.Font.NORMAL);   
BaseFont   bfChinese   =   BaseFont.createFont("STSong-Light",   "UniGB-UCS2-H",   BaseFont.NOT_EMBEDDED); 
com.lowagie.text.Font   FontChinese   =   new   com.lowagie.text.Font(bfChinese,   12,   com.lowagie.text.Font.NORMAL); 

 

但是这种办法只能时是在亚洲语言包中定义的

 

2、网上查到的引用windows字体的方式,但度rtf格式不支持,显示的是英文名称的字体

 

Java代码
BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL) 
BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); 
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL)

 

 

3 经过反复测试,下面这种办法支持word

 

 

 

 

Java代码
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
// step 1: 定义  
Document document = new Document();          
try {  
    // step 2:  
    // 建立一个rtf文档  
    RtfWriter2.getInstance(document, new FileOutputStream(filePath + file));  
      
    // step 3: we open the document  
    document.open();  
    //设置字体  字体名称是中文的,在中间的中文字符前后加空格,  
    //这种写法是实验多次后的结果,直接写在word中体现为 "华?行?楷",这种写法感觉很怪异。  
    //在写字板中打开和word中打开不一样,见图  
    RtfFont font = new RtfFont("华 文 行 楷", 36, Font.BOLD, Color.BLACK);  
      
    String text = "这是中文字体测试!this is a test";  
    document.add(new Paragraph(text, font));   
      
    System.out.println(font.getFamilyname());  
}  
catch(DocumentException de) {  
    System.err.println(de.getMessage());  
}  
catch(IOException ioe) {  
    System.err.println(ioe.getMessage());  
}finally{  
      
    document.close();  
}  
// step 5: we close the document 
        // step 1: 定义
        Document document = new Document();       
        try {
            // step 2:
            // 建立一个rtf文档
         RtfWriter2.getInstance(document, new FileOutputStream(filePath + file));
           
            // step 3: we open the document
            document.open();
            //设置字体  字体名称是中文的,在中间的中文字符前后加空格,
            //这种写法是实验多次后的结果,直接写在word中体现为 "华?行?楷",这种写法感觉很怪异。
            //在写字板中打开和word中打开不一样,见图
            RtfFont font = new RtfFont("华 文 行 楷", 36, Font.BOLD, Color.BLACK);
           
            String text = "这是中文字体测试!this is a test";
            document.add(new Paragraph(text, font));
           
            System.out.println(font.getFamilyname());
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }finally{
         
            document.close();
        }
        // step 5: we close the document

【上篇】
【下篇】

抱歉!评论已关闭.