打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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");
 }

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
(转)CSS3 @font-face
Fonts in Qt/Embedded
IReport PDF 中文不显示问题
如何制作TTF文件2
高分作图:circos圈图之软件安装篇
Java 导出 word - J2EE,飞诺网社区-大型IT技术论坛社区
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服