iText pdf在使用NOTO字体或Source Hans时不显示中文字符(iText pdf not displaying Chinese characters when using NOTO f

编程入门 行业动态 更新时间:2024-10-10 05:16:41
iText pdf在使用NOTO字体或Source Hans时不显示中文字符(iText pdf not displaying Chinese characters when using NOTO fonts or Source Hans)

我正在尝试使用NOTO字体( https://www.google.com/get/noto/ )来显示中文字符。 这是我的示例代码,来自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(); }

我已经在我的机器上下载了字体文件。 我正在使用两种方法

在Adobe中使用等效的字体系列

将otf文件嵌入pdf中

使用方法1,我希望中文字符以pdf格式显示,但显示英文文本,中文字符为空白。

使用方法2,当我尝试使用pdf嵌入字体时,这不是我想要的路径,打开pdf时出错。

更新:如果我看一下这个例子http://itextpdf.com/examples/iia.php?id=214

并在此代码中

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

我用c:/temp/fonts/NotoSansCJKtc-Thin.otf替换c:/windows/fonts/arialuni.ttf,我没看到中文字符。 要转换的文本是

public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n" + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), " + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), " + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";

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:\n" + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), " + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), " + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";

最满意答案

显然你使用的是错误的字体。 我已从您发布的链接下载了字体。 您正在使用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 = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n" + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), " + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), " + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky)."; public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f"; public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044"; public static final String KOREAN = "\ube48\uc9d1"; 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(); }

您声称Adobe Reader XI不显示中文字形,而是显示“无法提取嵌入字体”消息。 我不能重现这个[*] 。 我甚至在Adobe Acrobat中使用了Preflight,如下所示,但没有发现错误:

在此处输入图像描述

[*]更新:如果您使用iText群集NV未知的版本iText 4.2.x,则可以重现此问题。 请仅使用高于5的iText版本。

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:

enter image description here

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:\n" + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), " + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), " + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky)."; public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f"; public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044"; public static final String KOREAN = "\ube48\uc9d1"; 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:

enter image description here

[*] 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.

更多推荐

本文发布于:2023-07-17 14:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中文   字符   字体   Source   pdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!