gpt4 book ai didi

java - IText - 生成带有汉字的PDF(简体中文)

转载 作者:行者123 更新时间:2023-12-02 06:14:53 28 4
gpt4 key购买 nike

我正在使用 iText 生成一些 PDF,这些 pdf 有一些中文字符(简体中文 - GB2312),但是我无法生成包含这些字符的 pdf。

谁能告诉我哪里错了?

我尝试使用各种形式的创作但没有成功:

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

有人可以帮助我吗?

添加问题,这是我当前的测试代码:

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
}

最佳答案

采用的解决方案:

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

关于java - IText - 生成带有汉字的PDF(简体中文),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21577944/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com