IText - 生成带有中文字符的PDF(简体中文)

时间:2014-02-05 12:56:28

标签: java pdf itext chinese-locale

我使用iText生成一些PDF,这些pdf有一些中文字符(简体中文 - GB2312),但是我无法生成带有这些字符的pdf。

任何人都可以告诉我哪里错了?

我尝试过使用各种形式的创作,但没有成功:

BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 


com.itextpdf.text.DocumentException: Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.
    at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:699)
    at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:606)
    at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:441)
    at com.ford.fc.frc.render.wsltopdf.PDFDefaultWriter.printText(PDFDefaultWriter.java:176)
    at com.ford.fc.frc.render.wsltopdf.PDFDefaultConverter.convertFile(PDFDefaultConverter.java:122)
    at com.ford.fc.frc.render.wsltopdf.PDFDefaultConverter.convert(PDFDefaultConverter.java:234)
    at com.ford.fc.frc.render.plugins.PDFDefaultRenderer.render(PDFDefaultRenderer.java:41)
    at com.ford.fc.frc.report.ReportManager.executeRenderer(ReportManager.java:1113)
    at com.ford.fc.frc.report.ReportManager.reportCompleted(ReportManager.java:596)
    at com.ford.fc.roc.ReportOutputControl.reportCompleted(ReportOutputControl.java:87)
    at LoadFRC.main(LoadFRC.java:69)



BaseFont bfComic = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont,  AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
       Font fontbold = new Font(bfComic, 8);


 BaseFont bfComic = BaseFont.createFont("C:\\Windows\\Fonts\\cour.ttf",  BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
       Font fontbold = new Font(bfComic, 8);

有人可以帮助我吗?

添加问题,这是我目前的测试代码:

while(null != (line = reader.readLine())) {
    document.open();

    FontSelector selector = new FontSelector();
    /*FontFactory.getFont("MSung-Light","UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED);*/
    Font f2 = FontFactory.getFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseTraditionalEncoding_H, BaseFont.NOT_EMBEDDED);
    f2.setColor(BaseColor.RED);
    selector.addFont(f2);
    Phrase ph = selector.process(line);
    document.add(new Paragraph(ph));


    BaseFont bfComic = BaseFont.createFont("C:\\Windows\\Fonts\\arialuni.ttf",  BaseFont.IDENTITY_V, BaseFont.EMBEDDED);
    Font fontbold = new Font(bfComic, 8);
    Paragraph p = new Paragraph(line, fontbold);
    document.add(p);

    // step 5: we close the document
}

2 个答案:

答案 0 :(得分:2)

采用的解决方案:

private static final String PATH_FONT_ARIALUNI = "C:\\Windows\\Fonts\\arialuni.ttf";
      private static final String PATH_FONT_COUR = "C:\\Windows\\Fonts\\cour.ttf";



       // FOR Chinese
       BaseFont baseFont = BaseFont.createFont(PATH_FONT_ARIALUNI, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
       Font font = new Font(baseFont, 6.8f);

       BaseFont baseFontNormal = BaseFont.createFont(PATH_FONT_COUR, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
       Font fontNormal = new Font(baseFontNormal, 6.8f);

       Paragraph par = new Paragraph();
       par.setLeading(9);

       char[] aa = line.toCharArray();
       boolean isLastChineseChar = false;

       System.out.println(line);

       StringBuilder newLine = new StringBuilder();
       for (int j = 0; j < line.length(); j++) {

           if((Character.UnicodeBlock.of(aa[j]) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)){
               if(!isLastChineseChar) {
                   par.add(new Phrase(newLine.toString(), fontNormal));
                   newLine.delete(0, newLine.length());
               }
               newLine.append(aa[j]);
               isLastChineseChar = true;
               /*System.out.println("Is CHINESE: " + aa[j]);*/
           } else {
               if(isLastChineseChar) {
                   par.add(new Phrase(newLine.toString(), font));
                   newLine.delete(0, newLine.length());
                   isLastChineseChar = false;
               }
               newLine.append(aa[j]);
               /*System.out.println("NOT IS CHINESE: " + aa[j]);*/
           }
       }

       if(isLastChineseChar){
           par.add(new Phrase(newLine.toString(), font));
       } else {
           par.add(new Phrase(newLine.toString(), fontNormal));
       }       

       if(line.contains(BREAK_PAGE)) {
          document.newPage();
       }

       par.setAlignment(Element.ALIGN_LEFT);
       document.add(par);

答案 1 :(得分:0)

你的CLASSPATH中有iText jar,但是你忘了添加(正确的)itext-asian.jar。

请下载2.3版本额外的jars ZIP文件:http://sourceforge.net/projects/itext/files/extrajars/

此jar包含中文字形的指标。永远不会嵌入这样的字体。在Adobe Reader中使用此类字体打开文档时,可能会要求您安装额外的字体包。