itext Text to PDF: iText 5 versus iText 7 Text2Pdf.java (iText 5)


Example

Suppose that we have the following text file: jekyll_hyde.txt

How do we convert it to a PDF that looks like this:

enter image description here

When using iText 5, we'd use the following code:

public void createPdf(String dest)
throws DocumentException, IOException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    BufferedReader br = new BufferedReader(new FileReader(TEXT));
    String line;
    Paragraph p;
    Font normal = new Font(FontFamily.TIMES_ROMAN, 12);
    Font bold = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    boolean title = true;
    while ((line = br.readLine()) != null) {
        p = new Paragraph(line, title ? bold : normal);
        p.setAlignment(Element.ALIGN_JUSTIFIED);
        title = line.isEmpty();
        document.add(p);
    }
    document.close();
}

Source: developers.itextpdf.com