300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > java写pdf中文不显示_java – iText pdf在使用NOTO字体或Source Hans时不显示中文字符...

java写pdf中文不显示_java – iText pdf在使用NOTO字体或Source Hans时不显示中文字符...

时间:2023-12-11 07:17:45

相关推荐

java写pdf中文不显示_java – iText pdf在使用NOTO字体或Source Hans时不显示中文字符...

我正在尝试使用NOTO字体(

/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时出错.

并在此代码中

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

ArrayListlist = new ArrayList();

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).";

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。