HelloWorldInternational.java(iText 7)
在这个 iText 7 示例中,我们将使用不同的字体创建不同语言的 Hello World 示例:
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
try (Document document = new Document(pdf)) {
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
PdfFont russian = PdfFontFactory.createFont(
"src/main/resources/fonts/FreeSans.ttf",
"CP1251", true);
PdfFont cjk = PdfFontFactory.createFont(
"src/main/resources/fonts/NotoSansCJKsc-Regular.otf",
PdfEncodings.IDENTITY_H, true);
document.setFont(font);
Paragraph p = new Paragraph("Hello World! ")
.add(new Text("Hallo Wereld! ").setFontSize(14))
.add(new Text("Bonjour le monde! ").setFontSize(10).setTextRise(4))
.add(
new Text("\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u043b\u0442\u0435 \u043c\u0438\u0440! ")
.setFont(russian))
.add(new Text("\u4f60\u597d\u4e16\u754c! ")
.setFont(cjk))
.add(new Text("\uc5ec\ubcf4\uc138\uc694 \uc138\uacc4!")
.setFont(cjk));
document.add(p);
}
}
资料来源: developers.itextpdf.com 和 iText 7:Building Blocks 教程。