A
A
Anton @ Lyalin2017-04-07 18:11:21
Java
Anton @ Lyalin, 2017-04-07 18:11:21

How to make iText handle Cyrillic?

How to make iTextPdf write Cyrillic to pdf file?

PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();

            BaseFont baseFont = BaseFont.createFont("src/Components/font/cyr.ttf", BaseFont.CP1250, BaseFont.EMBEDDED);
            Font font = new Font(baseFont, 14, Font.NORMAL);
            document.add(new com.itextpdf.text.Paragraph(text, font));

Tried many ways, none of them work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-04-07
@toxa_1995

Font font = FontFactory.getFont("/fonts/DejaVuSans.ttf", "cp1251", BaseFont.EMBEDDED, 10);
try (FileOutputStream fs = new FileOutputStream(file)) {
    Document document = new Document();
    PdfWriter.getInstance(document, fs);
    document.open();
    Paragraph paragraph = new Paragraph();
    paragraph.add(new Paragraph("Привет", normalFont));
    document.add(paragraph);
    document.close();
}
catch (DocumentException exc) {}
catch (IOException exc) {}

Just don't forget to add the font to the resources and compile the program with the encoding of the source code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question