Java IText - 用汉字生成PDF(简体中文)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21577944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IText - Generating PDF with Chinese characters (Chinese Simplified)
提问by Estev?o Jord?o
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.
我正在使用 iText 生成一些 PDF,这些 pdf 有一些中文字符(简体中文 - GB2312),但是我无法生成带有这些字符的 pdf。
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
}
采纳答案by Bruno Lowagie
You have the iText jar in your CLASSPATH, but you forgot to add the (correct) itext-asian.jar.
您的 CLASSPATH 中有 iText jar,但您忘记添加(正确的)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/
请下载 2.3 版的额外 jars ZIP 文件,可在此处获得: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.
这个 jar 包含中国字形的度量。这种字体永远不会被嵌入。当您在 Adobe Reader 中使用此类字体打开文档时,可能会要求您安装额外的字体包。
回答by Estev?o Jord?o
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);