• 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");
     }

    }

  • 相关阅读:
    SSLZYC 洛谷P2055 假期的宿舍
    SSLZYC 2601 (洛谷P1756)【24题】飞行员配对方案问题
    SSLZYC POJ 3264 平衡的阵容
    SSLZYC 2432 面积最大
    SSLZYC 2433 文件名排序
    Structure of a C program: Preprocessor directives (#include <stdlib.h>, #define)
    Basic vim Commands
    UNIX Copying Files Remotely Examples(scp/pscp)
    ssh command in Linux with Example
    UNIX Copying a File
  • 原文地址:https://www.cnblogs.com/vofill/p/7365822.html
Copyright © 2020-2023  润新知