Suppose that we have the following text file: jekyll_hyde.txt
How do we convert it to a PDF that looks like this:
When using iText 5, you'd need code like this:
public void createPdf(String dest)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
ColumnText ct = new ColumnText(writer.getDirectContent());
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();
ct.addElement(p);
}
Rectangle[] columns = {
new Rectangle(36, 36, 290, 806), new Rectangle(305, 36, 559, 806)
};
int c = 0;
int status = ColumnText.START_COLUMN;
while (ColumnText.hasMoreText(status)) {
ct.setSimpleColumn(columns[c]);
status = ct.go();
if (++c == 2) {
document.newPage();
c = 0;
}
}
document.close();
}
Source: developers.itextpdf.com