簡體   English   中英

IText - 生成帶有中文字符的PDF(簡體中文)

[英]IText - Generating PDF with Chinese characters (Chinese Simplified)

我使用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
}

采用的解決方案:

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

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

請下載2.3版本的額外罐子ZIP文件: http//sourceforge.net/projects/itext/files/extrajars/

此jar包含中文字形的度量標准。 永遠不會嵌入這樣的字體。 在Adobe Reader中使用此類字體打開文檔時,可能會要求您安裝額外的字體包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM