itext Fonts: iText 5 versus iText 7 HelloWorldInternational.java (iText 7)


Example

In this iText 7 example, we will create a Hello World example in different languages, using different fonts:

enter image description here

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

Source: developers.itextpdf.com and the iText 7: Building Blocks tutorial.