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
}
解决方案
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.