Java iText pdf在使用NOTO字体或源代码时不显示中文字符
我正在尝试使用NOTO fonts()来显示汉字。这是我的示例代码,一个来自iText的修改过的示例代码Java iText pdf在使用NOTO字体或源代码时不显示中文字符,java,pdf,itext,Java,Pdf,Itext,我正在尝试使用NOTO fonts()来显示汉字。这是我的示例代码,一个来自iText的修改过的示例代码 public void createPdf(String filename) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(filename)); docu
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();
}
我已经在我的机器上下载了字体文件。我使用两种方法
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();
}
很明显,您使用了错误的字体。我已经从你发布的链接下载了字体。您使用的是NOTSERIF-Bold.ttf,一种不支持中文的字体。但是,ZIP文件还包含字体名称中带有CJK的字体如您所指的网站所述,CJK代表中文、日文和韩文。使用其中一种CJK字体,您将能够在PDF中生成中文文本 请看一看我在其中使用了您提到的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席没有显示中文字形,而是显示了“不能提取嵌入字体”的消息。我无法复制此[*]。如图所示,我甚至在AdobeAcrobat中使用了Preflight,但未发现任何错误:
[*]更新:如果您使用iText 4.2.x,这个版本是由iText Group NV未知的人发布的,则会重现此问题。请仅使用高于5的iText版本。1。您正在代码中按原样添加中文字形。这是糟糕的编程。改用Unicode表示法。2.你为什么认为NOTO字体支持中文?3.您定义了一个别名“my_nato_font”,但您尝试使用别名“SourceHanSansSC Regular”获取您的字体您已经修改了您的问题,但我的答案仍然有效。我使用官方文档中的代码(不是你的次优代码示例)测试了谷歌的CJK字体,中文测试显示正确。我尝试了你提到的示例,并更新了我的问题。我看不出这方面的中国特色。是的,这是填写表格的一个例子,但我关注的是汉字,而不是表格。从我回答中的屏幕截图可以看出,使用NOTO字体创建包含汉字的PDF非常简单,前提是您阅读文档而不是仅复制/粘贴。感谢您的详细回复。问题是iText的版本不正确。如果您使用maven,请确保使用新版本com.itextpdf itextpdf 5.5.5 com.itextpdf itext asian 5.2.0如果我们用SourceHanSansSc Regular替换NotoSanCJKsc Regular,则同一示例不起作用。事实上,我用源代码hansans和Serif字体尝试了相同的例子。CJK字符在生成的pdf上不可见。iText与源韩家族有什么问题吗?@VaibhavRaj您已经试过iText 7了吗?你是iText的客户吗?如果源汉字系列中存在错误,您应该向源汉字字体提供商寻求支持。如果您想让我们查看它,您应该与我们签订一份支持合同。我们目前正在使用iText 5.5。我尝试了“NotoSansCJKsc Regular.otf”,它对我们来说运行良好。但它的大小约为14.4 MB。你还有比这更好的选择吗@布鲁诺洛瓦吉
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();
}