0

I am using iText to generate some PDFs, these pdfs have some Chinese characters (Simplified Chinese - GB2312), however I am unable to generate a pdf with these characters.

Anyone could tell me where I am wrong?

I tried using various forms of creation but did not succeed:

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

Could someone help me?

Adding question, this is my current code for testing:

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
}
Estevão Jordão
  • 189
  • 1
  • 5
  • 20

2 Answers2

2

The solution adopted:

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);
Estevão Jordão
  • 189
  • 1
  • 5
  • 20
0

You have the iText jar in your CLASSPATH, but you forgot to add the (correct) itext-asian.jar.

Please download version 2.3 of the extra jars ZIP-file that is available here: http://sourceforge.net/projects/itext/files/extrajars/

This jar contains metrics for Chinese glyphs. Such a font will never be embedded. When you open a document using such a font in Adobe Reader, you may be asked to install an extra font pack.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • My current work we have many bureaucracies to add new API, is there another solution? Without the need to add the itext-asian? – Estevão Jordão Feb 05 '14 at 14:10
  • Your assumption that you're adding a new API is wrong. If you open itext-asian.jar, you will discover that it contains metrics files (data), not executable code. Also: if your work environment is bureaucratic, your company has probably bought a commercial license for your use of iText. If so, they have already accepted all iText related jars. – Bruno Lowagie Feb 05 '14 at 14:17
  • As for your question: if your specific requirement is to use CJK fonts (the ones that aren't embedded and require a font pack), then you need the metrics in itext-asian.jar. If your requirement is to create Chinese text, then you have other options. These are described in my book. If you don't own a copy of the book, take a look at the examples of chapter 11: http://itextpdf.com/book/chapter.php?id=11 (For instance: you can use arialuni.ttf as a font that contains information telling iText how to **embed** Chinese glyphs.) – Bruno Lowagie Feb 05 '14 at 14:22
  • I tried to use the font that showed me: BaseFont bfComic = BaseFont.createFont("C:\\Windows\\Fonts\\carialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); But the document still does not show me the characters in Chinese. The Chinese characters exist within the Paragraph object, but disappear in Document, was missing something? – Estevão Jordão Feb 05 '14 at 15:14
  • What have you tried? (also: your code says carialuni.ttf instead of arialuni.ttf) – Bruno Lowagie Feb 05 '14 at 15:15
  • I am using the ARIALUNI.TTF, sorry for the mistake: BaseFont bfComic = BaseFont.createFont("C:\\Windows\\Fonts\\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); – Estevão Jordão Feb 05 '14 at 15:18
  • OK, but do you have a SSCCE that proves that "it doesn't work"? http://lowagie.com/doesntwork – Bruno Lowagie Feb 05 '14 at 15:24
  • For instance: this PDF shows that iText can draw Chinese fonts: http://examples.itextpdf.com/results/part3/chapter11/font_selection.pdf (you can find the code here: http://itextpdf.com/examples/iia.php?id=214 ). So does this PDF: http://examples.itextpdf.com/results/part3/chapter11/peace.pdf (you can find the code here: http://itextpdf.com/examples/iia.php?id=215 ) – Bruno Lowagie Feb 05 '14 at 15:41
  • I did a java project only to test the creation of PDF. http://itextpdf.com/examples/iia.php?id=214 In the project I imported the following jars: itext-asian.jar iText-5.0.5.jar And I modified the class of text so you can open a test file with the following contents: ¸£ÌØÆû³µ½ðÈÚ£¨Öйú£©ÓÐÏÞ¹«Ë¾ (this is chinese) Test pdf The pdf that was generated just have written my second sentence: Test pdf What could be wrong? Many thanks for your help and patience – Estevão Jordão Feb 05 '14 at 16:41
  • Your version of iText is much too old. Please read the changelog of 5.2.0: http://itextpdf.com/changelog/520 where it says: **If you use CJK fonts in your existing code, you will need to update the itext-asian.jar.** If you're using the latest itext-asian.jar with that old version, it's only normal that the font metrics aren't found. These jars aren't compatible. Finally: you're using iText 5.0.5 which means a commercial license may be required: http://itextpdf.com/salesfaq – Bruno Lowagie Feb 05 '14 at 19:46
  • I performed a search here and now worked in ARIALUNI.TTF source I found at first was wrong (she had just 5MB), after some testing and research found the correct (22.7 MB). Now is working perfectly my impression. Unfortunately I can not change the version of itext or add itext-asian. Thank you so much for all your help, you really helped me a lot. Thank you for everything and for your patience with the subject. – Estevão Jordão Feb 06 '14 at 13:20
  • Please confirm that your employer is a customer of iText. If he isn't, please get me in touch with your employer. – Bruno Lowagie Feb 06 '14 at 13:23
  • Probably we have a commercial license, but I work in a multinational, here they use a centralized API in a framework, so to change some version of API we demand a lot of time for this and my project not have this time. I appreciate the help :). – Estevão Jordão Feb 06 '14 at 13:31