iText pdf 在使用 NOTO 字体或 Source Hans 时不显示汉字 [英] iText pdf not displaying Chinese characters when using NOTO fonts or Source Hans
问题描述
我正在尝试使用 NOTO 字体 (
在这段代码中
public void createPdf(字符串文件名,布尔外观,布尔字体)抛出 IOException,文档异常 {//步骤1文档文档 = 新文档();//第2步PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));//第 3 步文档.open();//步骤4writer.getAcroForm().setNeedAppearances(appearances);TextField text = new TextField(writer, new Rectangle(36, 806, 559, 780), "description");text.setOptions(TextField.MULTILINE);如果(字体){基本字体 unicode =BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);text.setExtensionFont(BaseFont.createFont());ArrayList list = new ArrayList ();list.add(unicode);text.setSubstitutionFonts(列表);BaseFont f= (BaseFont)text.getSubstitutionFonts().get(0);System.out.println(f.getPostscriptFontName());}text.setText(TEXT);writer.addAnnotation(text.getTextField());//第 5 步文档.close();}
我将 c:/windows/fonts/arialuni.ttf 替换为 C:/temp/fonts/NotoSansCJKtc-Thin.otf ,我看不到汉字.现在要转换的文字是
public static final String TEXT = "这些是张艺谋电影《英雄》中的主角:
"+ "u7121u540d (无名), u6b98u528d (断剑), "+ "u98dbu96ea (飞雪), u5982u6708 (月亮), "+ "u79e6u738b(国王)和 u9577u7a7a(天空).";
显然您使用了错误的字体.我已经从您发布的链接中下载了字体.您正在使用 NotoSerif-Bold.ttf,一种不支持中文的字体.但是,ZIP 文件还包含字体名称中带有 CJK 的字体.如您参考的网站所述,CJK 代表中文、日文和韩文.使用其中一种 CJK 字体,您就可以在 PDF 中生成中文文本.
查看 NotoExample,其中我使用了 ZIP 文件中的一种字体你参考.它会创建一个看起来像这样的 PDF:
这是我使用的代码:
public static final String FONT = "resources/fonts/NotoSansCJKsc-Regular.otf";public static final String TEXT = "这些是张艺谋电影《英雄》中的主角:
"+ "u7121u540d (无名), u6b98u528d (断剑), "+ "u98dbu96ea (飞雪), u5982u6708 (月亮), "+ "u79e6u738b(国王)和 u9577u7a7a(天空).";public static final String CHINESE = "u5341u950au57cbu4f0f";public static final String JAPANESE = "u8ab0u3082u77e5u3089u306au3044";public static final String KOREAN = "ube48uc9d1";public void createPdf(String dest) 抛出 IOException,DocumentException {文档文档 = 新文档();PdfWriter.getInstance(document, new FileOutputStream(DEST));文档.open();Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);段落 p = 新段落(文本,字体);文档.add(p);document.add(new Paragraph(CHINESE, font));document.add(new Paragraph(JAPANESE, font));document.add(new Paragraph(KOREAN, font));文档.close();}
您声称 Adobe Reader XI 不显示中文字形,而是显示无法提取嵌入的字体"消息.我无法重现此[*].我什至在 Adobe Acrobat 中使用了 Preflight,如此处所示,但未发现任何错误:
[*] 更新:如果您使用 iText 4.2.x,此问题可以重现,该版本由 iText Group NV 不认识的人发布.请仅使用高于 5 的 iText 版本.
I am trying to use NOTO fonts (https://www.google.com/get/noto/) to display Chinese characters. Here is my sample code,a modified sample code from iText.
public void createPdf(String filename) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
//This is simple English Font
FontFactory.register("c:/temp/fonts/NotoSerif-Bold.ttf", "my_nato_font");
Font myBoldFont = FontFactory.getFont("my_nato_font");
BaseFont bf = myBoldFont.getBaseFont();
document.add(new Paragraph(bf.getPostscriptFontName(), myBoldFont));
//This is Chinese font
//Option 1 :
Font myAdobeTypekit = FontFactory.getFont("SourceHanSansSC-Regular", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//Option 2 :
/*FontFactory.register("C:/temp/AdobeFonts/source-han-sans-1.001R/OTF/SimplifiedChinese/SourceHanSansSC-Regular.otf", "my_hans_font");
Font myAdobeTypekit = FontFactory.getFont("my_hans_font", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);*/
document.add(Chunk.NEWLINE);
document.add(new Paragraph("高興", myAdobeTypekit));
document.add(Chunk.NEWLINE);
//simplified chinese
document.add(new Paragraph("朝辞白帝彩云间", myAdobeTypekit));
document.add(Chunk.NEWLINE);
document.add(new Paragraph("高兴", myAdobeTypekit));
document.add(new Paragraph("The Source Han Sans Traditional Chinese ", myAdobeTypekit));
document.close();
}
I have downloaded the fonts files on my machine. I am using two approaches
To use the equivalent font family in Adobe
Embed the otf file in pdf
Using approach 1, I would expect the Chinese characters to be displayed in pdf but English text is displayed and it is blank for Chinese characters.
Using approach 2, when I try embedding the fonts with pdf, which is not the path I would like to take, there is error in opening pdf.
Update : If I look at this example http://itextpdf.com/examples/iia.php?id=214
and in this code
public void createPdf(String filename, boolean appearances, boolean font)
throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
writer.getAcroForm().setNeedAppearances(appearances);
TextField text = new TextField(writer, new Rectangle(36, 806, 559, 780), "description");
text.setOptions(TextField.MULTILINE);
if (font) {
BaseFont unicode =
BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
text.setExtensionFont(BaseFont.createFont());
ArrayList<BaseFont> list = new ArrayList<BaseFont>();
list.add(unicode);
text.setSubstitutionFonts(list);
BaseFont f= (BaseFont)text.getSubstitutionFonts().get(0);
System.out.println(f.getPostscriptFontName());
}
text.setText(TEXT);
writer.addAnnotation(text.getTextField());
// step 5
document.close();
}
I substitute, c:/windows/fonts/arialuni.ttf with C:/temp/fonts/NotoSansCJKtc-Thin.otf , I do not see the Chinese characters. The text to convert now is
public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:
"
+ "u7121u540d (Nameless), u6b98u528d (Broken Sword), "
+ "u98dbu96ea (Flying Snow), u5982u6708 (Moon), "
+ "u79e6u738b (the King), and u9577u7a7a (Sky).";
Clearly you are using the wrong font. I have downloaded the fonts from the link you posted. You are using NotoSerif-Bold.ttf, a font that does not support Chinese. However, the ZIP file also contains fonts with CJK in the font name. As described on the site you refer to, CJK stands for Chinese, Japanese and Korean. Use one of those CJK fonts and you'll be able to product Chinese text in your PDF.
Take a look at the NotoExample in which I use one of the fonts from the ZIP file you refer to. It creates a PDF that looks like this:
This is the code I used:
public static final String FONT = "resources/fonts/NotoSansCJKsc-Regular.otf";
public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:
"
+ "u7121u540d (Nameless), u6b98u528d (Broken Sword), "
+ "u98dbu96ea (Flying Snow), u5982u6708 (Moon), "
+ "u79e6u738b (the King), and u9577u7a7a (Sky).";
public static final String CHINESE = "u5341u950au57cbu4f0f";
public static final String JAPANESE = "u8ab0u3082u77e5u3089u306au3044";
public static final String KOREAN = "ube48uc9d1";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph p = new Paragraph(TEXT, font);
document.add(p);
document.add(new Paragraph(CHINESE, font));
document.add(new Paragraph(JAPANESE, font));
document.add(new Paragraph(KOREAN, font));
document.close();
}
You claim that Adobe Reader XI doesn't show the Chinese glyphs, but instead shows a "Cannot extract the embedded Font" message. I can not reproduce this [*]. I have even used Preflight in Adobe Acrobat as indicated here, but no errors were found:
[*] Update: this problem can be reproduced if you use iText 4.2.x, a version that was released by somebody unknown to iText Group NV. Please use iText versions higher than 5 only.
这篇关于iText pdf 在使用 NOTO 字体或 Source Hans 时不显示汉字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!